-
Notifications
You must be signed in to change notification settings - Fork 1
/
LLtoGARS.java
313 lines (283 loc) · 12.5 KB
/
LLtoGARS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* -----------------------------------------------------------------------------
* UNCLASSIFIED UNCLASSIFIED UNCLASSIFIED UNCLASSIFIED UNCLASSIFIED
* (C) Copyright 2013, USAREUR G3 MCSD
* ALL RIGHTS RESERVED
* THIS NOTICE DOES NOT IMPLY PUBLICATION
* -------------------------------------------------------------------------- */
package gars;
/**
*
* @author marty
*/
public class LLtoGARS {
/**
* Get the GARS 3 letter longitude band descriptor for a given longitude
* (-180.0..180.0)
* <p>
* The longitude band are the first three characters of the GARS code.<br>
* The bands are numbered from 001 through 720, starting at -180 through 180
* and depict 30-minute longitudinal bands.<br>
* <p>
* @param longitude as double in degrees decimal
* @return String representation of the 3 character longitudinal band
*/
public static String getLongitudeBand(double longitude) {
Double longBand = longitude + 180;
// Normalize to 0.0 <= longBand < 360
while (longBand < 0) {
longBand = longBand + 360;
}
while (longBand > 360) {
longBand = longBand - 360;
}
longBand = Math.floor(longBand * 2.0);
Integer intLongBand = longBand.intValue() + 1; // Start at 001, not 000
String strLongBand = intLongBand.toString();
// Left pad the string with 0's so X becomes 00X
while (strLongBand.length() < 3) {
strLongBand = "0" + strLongBand;
}
return strLongBand;
}//getLongitudeBand
/**
* Get the GARS 2 letter latitude band descriptor for a given latitude
* (-90..90)
* <p>
* The fourth and fifth characters of a GARS code represent 30-minute
* latitudinal bands and are numbered from AA through QZ, starting at -90
* through 90.
* <p>
* @param latitude as double in degrees decimal
* @return String representation of the 2 character latitudinal band
*/
public static String getLatitudeBand(double latitude) {
char[] letterArray = "ABCDEFGHJKLMNPQRSTUVWXYZ".toCharArray();
Double offset = latitude + 90;
// Normalize offset to 0< offset <90
while (offset < 0) {
offset = offset + 180;
}
while (offset > 180) {
offset = offset - 180;
}
offset = Math.floor(offset * 2.0);
Double firstOffest = offset / letterArray.length;
Double secondOffest = offset % letterArray.length;
StringBuilder sb = new StringBuilder();
sb.append(letterArray[firstOffest.intValue()]).append(letterArray[secondOffest.intValue()]);
return sb.toString();
}//getLatitudeBand
/**
* Returns the GARS 15 minute quadrant cell identifier for a latitude,
* longitude pair.This identifier is the 6th character of the GARS
* code.<br>Each 30-minute cell is divided into four 15-minute by 15-minute
* quadrants. The quadrants are numbered sequentially, from west to east,
* starting with the northernmost band. Specifically, the northwest quadrant
* is “1”; the northeast quadrant is “2”; the southwest quadrant is “3”; the
* southeast quadrant is “4”.
* <p>
*
* @param latitude as double in degrees decimal
* @param longitude as double in degrees decimal
* @return String of the quadrant identifier (1..4), returns 0 if lat/lon is
* invalid
*/
public static String getQudarant(double latitude, double longitude) {
Double latBand = (Math.floor((latitude + 90.0) * 4.0) % 2.0);
Double longBand = (Math.floor((longitude + 180.0) * 4.0) % 2.0);
// Return "0" if error occurs
if (latBand < 0 || latBand > 1) {
return "0";
}
if (longBand < 0 || longBand > 1) {
return "0";
}
// Otherwise return the quadrant
if (latBand == 0.0 && longBand == 0.0) {
return "3";
} else if (latBand == 1.0 && longBand == 0.0) {
return "1";
} else if (latBand == 1.0 && longBand == 1.0) {
return "2";
} else if (latBand == 0.0 && longBand == 1.0) {
return "4";
}
return "0";
}//getQudarant
/**
* Returns the GARS 5 minute keypad cell identifier for a latitude,
* longitude pair. This is the seventh character of the GARS code.<br>Each
* 15-minute quadrant is divided into nine 5-minute by 5-minute areas. The
* areas are numbered sequentially, from west to east, starting with the
* northernmost band. The graphical representation of a 15-minute quadrant
* with numbered 5-minute by 5-minute areas resembles a telephone keypad.
* <p>
* This code was ported from the Geotrans 3.5 CCP implementation:
* http://earth-info.nga.mil/GandG/geotrans/
* <p>
* @param latitude as double in degrees decimal
* @param longitude as double in degrees decimal
* @return String of the 5x5 identifier (1..9), returns 0 if lat/lon is
* invalid
*/
public static String getKeyPad(double latitude, double longitude) {
// Check for valid lat/lon range
if (latitude < -90 || latitude > 90) {
return "0";
}
if (longitude < -180 || longitude > 180) {
return "0";
}
/* Convert longitude and latitude from degrees to minutes */
/* longitude assumed in -180 <= long < +180 range */
double long_minutes = (longitude + 180) * 60.0;
double lat_minutes = (latitude + 90) * 60.0;
/* now we have a positive number of minutes */
/* Find 30-min cell indices 0-719 and 0-359 */
long horiz_index_30 = (long) (long_minutes / 30.0);
long vert_index_30 = (long) (lat_minutes / 30.0);
/* Compute remainders 0 <= x < 30.0 */
double long_remainder = long_minutes - (horiz_index_30) * 30.0;
double lat_remainder = lat_minutes - (vert_index_30) * 30.0;
/* Find 15-min cell indices 0 or 1 */
long horiz_index_15 = (long) (long_remainder / 15.0);
long vert_index_15 = (long) (lat_remainder / 15.0);
/* Compute remainders 0 <= x < 15.0 */
long_remainder = long_remainder - (horiz_index_15) * 15.0;
lat_remainder = lat_remainder - (vert_index_15) * 15.0;
/* Find 5-min cell indices 0, 1, or 2 */
long horiz_index_5 = (long) (long_remainder / 5.0);
long vert_index_5 = (long) (lat_remainder / 5.0);
String[][] _5_minute_array = {{"7", "4", "1"}, {"8", "5", "2"}, {"9", "6", "3"}};
String keypad = _5_minute_array[(int) horiz_index_5][(int) vert_index_5];
return keypad;
}//getKeyPad
/**
* Returns a GARS coordinate string for a point (latitude, longitude)
* <p>
* The precision parameter indicates if a 30x30min, 15x15min, or 5x5min GARS
* code is to be returned.
* <p>
* Valid precision values are ["30","15","5"]
* <p>
*
* @param latitude as double in degrees decimal
* @param longitude as double in degrees decimal
* @param precision as String representing the precision of the GARS string
*
* @return String representation of the GARS identifier, returns 0 if
* lat/lon is invalid
*/
public static String getGARS(double latitude, double longitude, String precision) {
/* North pole is an exception, read over and down */
if (latitude == 90.0) {
latitude = 89.99999999999;
}
// Check for valid lat/lon range
if (latitude < -90 || latitude > 90) {
return "0";
}
if (longitude < -180 || longitude > 180) {
return "0";
}
// Get the longitude band ==============================================
Double longBand = longitude + 180;
// Normalize to 0.0 <= longBand < 360
while (longBand < 0) {
longBand = longBand + 360;
}
while (longBand > 360) {
longBand = longBand - 360;
}
longBand = Math.floor(longBand * 2.0);
Integer intLongBand = longBand.intValue() + 1; // Start at 001, not 000
String strLongBand = intLongBand.toString();
// Left pad the string with 0's so X becomes 00X
while (strLongBand.length() < 3) {
strLongBand = "0" + strLongBand;
}
// Get the latitude band ===============================================
char[] letterArray = "ABCDEFGHJKLMNPQRSTUVWXYZ".toCharArray();
Double offset = latitude + 90;
// Normalize offset to 0< offset <90
while (offset < 0) {
offset = offset + 180;
}
while (offset > 180) {
offset = offset - 180;
}
offset = Math.floor(offset * 2.0);
Double firstOffest = offset / letterArray.length;
Double secondOffest = offset % letterArray.length;
StringBuilder sb = new StringBuilder();
String strLatBand = sb.append(letterArray[firstOffest.intValue()]).append(letterArray[secondOffest.intValue()]).toString();
// Id the precision is 30x30min then return the longitudinal and latitudinal bands
if (precision.contains("30")) {
return strLongBand + strLatBand;
}
// Get the quadrant ====================================================
Double latBand = (Math.floor((latitude + 90.0) * 4.0) % 2.0);
longBand = (Math.floor((longitude + 180.0) * 4.0) % 2.0);
String quadrant = "0";
// return "0" if error occurs
if (latBand < 0 || latBand > 1) {
return "0";
}
if (longBand < 0 || longBand > 1) {
return "0";
}
// Otherwise get the quadrant
if (latBand == 0.0 && longBand == 0.0) {
quadrant = "3";
} else if (latBand == 1.0 && longBand == 0.0) {
quadrant = "1";
} else if (latBand == 1.0 && longBand == 1.0) {
quadrant = "2";
} else if (latBand == 0.0 && longBand == 1.0) {
quadrant = "4";
}
// Id the precision is 15x15min then return the longitudinal and latitudinal bands
// plus the quadrant
if (precision.contains("15")) {
return strLongBand + strLatBand + quadrant;
}
// Get the keypad ======================================================
/* Convert longitude and latitude from degrees to minutes */
/* longitude assumed in -180 <= long < +180 range */
double long_minutes = (longitude + 180) * 60.0;
double lat_minutes = (latitude + 90) * 60.0;
/* now we have a positive number of minutes */
/* Find 30-min cell indices 0-719 and 0-359 */
long horiz_index_30 = (long) (long_minutes / 30.0);
long vert_index_30 = (long) (lat_minutes / 30.0);
/* Compute remainders 0 <= x < 30.0 */
double long_remainder = long_minutes - (horiz_index_30) * 30.0;
double lat_remainder = lat_minutes - (vert_index_30) * 30.0;
/* Find 15-min cell indices 0 or 1 */
long horiz_index_15 = (long) (long_remainder / 15.0);
long vert_index_15 = (long) (lat_remainder / 15.0);
/* Compute remainders 0 <= x < 15.0 */
long_remainder = long_remainder - (horiz_index_15) * 15.0;
lat_remainder = lat_remainder - (vert_index_15) * 15.0;
/* Find 5-min cell indices 0, 1, or 2 */
long horiz_index_5 = (long) (long_remainder / 5.0);
long vert_index_5 = (long) (lat_remainder / 5.0);
String[][] _5_minute_array = {{"7", "4", "1"}, {"8", "5", "2"}, {"9", "6", "3"}};
String keypad = _5_minute_array[(int) horiz_index_5][(int) vert_index_5];
return strLongBand + strLatBand + quadrant + keypad;
}//getGARS
/**
* Returns a GARS 5x5 minute coordinate string for a point (latitude,
* longitude)
* <p>
*
* @param latitude as double in degrees decimal
* @param longitude as double in degrees decimal
*
* @return String representation of the GARS identifier, returns 0 if
* lat/lon is invalid
*/
public static String getGARS(double latitude, double longitude) {
return getGARS(latitude, longitude, "5");
}//getGARS
}