1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.rugged.utils;
18
19 import java.text.NumberFormat;
20
21 import org.hipparchus.exception.MathRuntimeException;
22 import org.hipparchus.geometry.euclidean.threed.Vector3D;
23 import org.hipparchus.util.CompositeFormat;
24 import org.hipparchus.util.FastMath;
25 import org.orekit.bodies.GeodeticPoint;
26 import org.orekit.errors.OrekitException;
27
28 public class GeodeticUtilities {
29
30
31
32
33
34
35
36
37
38
39 public static double computeDistanceInMeter(double earthRadius, final double long1, final double lat1,
40 final double long2, final double lat2) {
41
42
43 final Vector3D p1 = new Vector3D(long1, lat1);
44 final Vector3D p2 = new Vector3D(long2, lat2);
45 return earthRadius * Vector3D.angle(p1, p2);
46 }
47
48
49
50
51
52
53
54
55 public static double computeDistanceInMeter(double earthRadius, final GeodeticPoint gp1, final GeodeticPoint gp2) {
56
57 return computeDistanceInMeter(earthRadius, gp1.getLongitude(), gp1.getLatitude(), gp2.getLongitude(), gp2.getLatitude());
58 }
59
60
61
62 public static String toStringDMS(GeodeticPoint gp) {
63
64 final NumberFormat format = CompositeFormat.getDefaultNumberFormat();
65 format.setMaximumFractionDigits(1);
66 DMSangle latDMS = convertLatitudeToDMS(FastMath.toDegrees(gp.getLatitude()));
67 DMSangle lonDMS = convertLongitudeToDMS(FastMath.toDegrees(gp.getLongitude()));
68
69 String latSign = "";
70 if (latDMS.getCardinalPoint() == CardinalDirection.South) latSign = "-";
71 String lonSign = "";
72 if (lonDMS.getCardinalPoint() == CardinalDirection.West) lonSign = "-";
73
74 return "{lat: " + latSign +
75 format.format(latDMS.getDegrees()) + "° " +
76 format.format(latDMS.getMinutes()) + "' " +
77 format.format(latDMS.getSeconds()) + "'' " +
78 " lon: " + lonSign +
79 format.format(lonDMS.getDegrees()) + "° " +
80 format.format(lonDMS.getMinutes()) + "' " +
81 format.format(lonDMS.getSeconds()) + "'' " +
82 "}";
83 }
84
85
86
87
88
89
90 static DMSangle convertLongitudeToDMS(double longitudeInDecimalDegrees) {
91
92 String cardinalDirection;
93
94 if (longitudeInDecimalDegrees >= 0.0){
95 cardinalDirection= "E";
96 } else {
97 cardinalDirection= "W";
98 }
99
100 return convertToDMS(longitudeInDecimalDegrees, cardinalDirection);
101 }
102
103
104
105
106
107
108 public static DMSangle convertLatitudeToDMS(double latitudeInDecimalDegrees){
109
110 String cardinalDirection;
111
112 if (latitudeInDecimalDegrees >= 0.0){
113 cardinalDirection= "N";
114 } else {
115 cardinalDirection= "S";
116 }
117
118 return convertToDMS(latitudeInDecimalDegrees, cardinalDirection);
119
120 }
121
122
123
124
125
126
127
128 private static DMSangle convertToDMS(double angleInDecimalDegrees, String cardinalDirection) {
129
130
131 double angleInDD = FastMath.abs(angleInDecimalDegrees);
132
133 int degreesPart = (int) FastMath.floor(angleInDD);
134
135
136 double minutesInDecimal = 60.*(angleInDD - degreesPart);
137 int minutesPart = (int) FastMath.floor(minutesInDecimal);
138
139
140 double secondsInDecimal = 60.*(minutesInDecimal - minutesPart);
141
142
143 if (secondsInDecimal > (60. - 1.e-8)) {
144 secondsInDecimal = 0.0;
145 minutesPart++;
146 if (minutesPart == 60) {
147 minutesPart = 0;
148 degreesPart++;
149 }
150 }
151
152 return new DMSangle(degreesPart, minutesPart, secondsInDecimal, cardinalDirection);
153 }
154 }
155 class DMSangle {
156
157
158
159
160 private int degrees;
161
162
163
164
165 private int minutes;
166
167
168
169
170 private double seconds;
171
172
173
174
175 private CardinalDirection cardinalPoint;
176
177
178
179
180
181
182
183
184 public DMSangle(int degrees, int minutes, double seconds, String cardinalName) {
185 this.degrees = degrees;
186 this.minutes = minutes;
187 this.seconds = seconds;
188 this.cardinalPoint = CardinalDirection.getCardinalDirectionFromName(cardinalName);
189 if (this.cardinalPoint == null){
190 throw new OrekitException(new MathRuntimeException(null, this.cardinalPoint));
191 }
192 }
193
194
195
196
197 public int getDegrees() {
198 return degrees;
199 }
200
201
202
203 public int getMinutes() {
204 return minutes;
205 }
206
207
208
209 public double getSeconds() {
210 return seconds;
211 }
212
213
214
215 public CardinalDirection getCardinalPoint() {
216 return cardinalPoint;
217 }
218 }
219
220 enum CardinalDirection {
221 North("North","N"),
222 South("South","S"),
223 West("West","W"),
224 East("East","E");
225
226
227
228 private String cardinalFullName = null;
229
230
231
232
233 private String cardinalShortName = null;
234
235
236
237
238
239
240 private CardinalDirection(String fullName, String shortName){
241 this.cardinalFullName = fullName;
242 this.cardinalShortName = shortName;
243 }
244
245
246
247
248
249
250 public static CardinalDirection getCardinalDirectionFromName(String cardinalName){
251
252 for (CardinalDirection currentName : CardinalDirection.values()) {
253 if (currentName.cardinalFullName.equals(cardinalName)) {
254 return currentName;
255 }
256 }
257
258 for (CardinalDirection currentName : CardinalDirection.values()) {
259 if (currentName.cardinalShortName.equals(cardinalName)) {
260 return currentName;
261 }
262 }
263 return null;
264 }
265 }