1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.common;
20
21 import java.text.MessageFormat;
22 import java.util.Locale;
23
24
25
26
27
28 public class MCRGeoUtilities {
29
30
31
32
33
34 public static double toDecimalDegrees(int degree, int minutes, double seconds) {
35 return (((seconds / 60) + minutes) / 60) + degree;
36 }
37
38
39
40
41
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
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
79
80
81
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
104
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 }