View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common;
20  
21  import java.text.MessageFormat;
22  import java.util.Locale;
23  
24  /**
25   * @author shermann
26   *
27   */
28  public class MCRGeoUtilities {
29  
30      /**
31       * Converts coordinates to decimal degree (as used by google maps).
32       * 
33       */
34      public static double toDecimalDegrees(int degree, int minutes, double seconds) {
35          return (((seconds / 60) + minutes) / 60) + degree;
36      }
37  
38      /**
39       * Converts coordinates in pica format to decimal degree (as used by google maps).
40       *
41       * @return the decimal degree representation of the coordinates
42       */
43      public static double toDecimalDegrees(String picaValue) {
44          if (picaValue == null || picaValue.length() == 0 || (!isValid(picaValue))) {
45              return 0d;
46          }
47          String[] strings = picaValue.split(" ");
48          if (strings.length < 3) {
49              return 0d;
50          }
51  
52          int degree = Integer.valueOf(strings[1]);
53          int minutes = Integer.valueOf(strings[2]);
54          double seconds = 0d;
55  
56          if (strings.length >= 4) {
57              seconds = Double.valueOf(strings[3]);
58          }
59  
60          int factor = "W".equals(strings[0]) || "S".equals(strings[0]) ? -1 : 1;
61          return ((((seconds / 60) + minutes) / 60) + degree) * factor;
62      }
63  
64      /**
65       * Converts decimal degree to ordinary coordinates.
66       * 
67       */
68      public static String toDegreeMinuteSecond(double inDecimalDegree) {
69          int degree = (int) inDecimalDegree;
70          int minutes = (int) ((inDecimalDegree - degree) * 60);
71          double seconds = ((inDecimalDegree - degree) * 60 - minutes) * 60;
72  
73          return new MessageFormat("{0}° {1}'' {2}", Locale.ROOT).format(
74              new Object[] { degree, minutes, Math.round(seconds * 100d / 100d) });
75      }
76  
77      /**
78       * @param picaValue the value as stored in opac/pica
79       * @return a human readable form like 38° 22′ S
80       * 
81       * @see #toDegreeMinuteSecond(double)
82       */
83      public static String toDegreeMinuteSecond(String picaValue) {
84          if (picaValue == null || picaValue.length() == 0 || (!isValid(picaValue))) {
85              return null;
86          }
87          String[] strings = picaValue.split(" ");
88          if (strings.length < 3) {
89              return null;
90          }
91          double seconds = 0d;
92  
93          if (strings.length >= 4) {
94              seconds = Double.valueOf(strings[3]);
95          }
96  
97          return new MessageFormat("{0}° {1}'' {2} {3}", Locale.ROOT).format(
98              new Object[] { Integer.valueOf(strings[1]), Integer.valueOf(strings[2]), Math.round(seconds * 100d / 100d),
99                  strings[0] });
100     }
101 
102     /**
103      * @param picaValue
104      * @return
105      */
106     private static boolean isValid(String picaValue) {
107         String regex = "[EWSN]{1}\\s[0-9]{3}\\s[0-9]{2}(\\s[0-9]*)*";
108         return picaValue.matches(regex);
109     }
110 }