forked from IHoffman5214/Calendar-Event-File-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoordinate.java
More file actions
executable file
·384 lines (315 loc) · 12.2 KB
/
Coordinate.java
File metadata and controls
executable file
·384 lines (315 loc) · 12.2 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* http://www.java2s.com/Code/Java/2D-Graphics-GUI/Aclasstorepresentalatitudeandlongitude.htm
*
*
* This file is part of the AusStage Utilities Package
*
* The AusStage Utilities Package is free software: you can redistribute
* it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* The AusStage Utilities Package is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the AusStage Utilities Package.
* If not, see <http://www.gnu.org/licenses/>.
*/
//package au.edu.ausstage.utils;
// import additional libraries
import java.text.DecimalFormat;
/**
* A class to represent a latitude and longitude
*/
public class Coordinate implements Comparable<Coordinate>{
// declare private class level variables
private float latitude;
private float longitude;
private DecimalFormat format;
/**
* Constructor for this class
*
* @param latitude a latitude coordinate in decimal notation
* @param longitude a longitude coordinate in decimal notation
*/
public Coordinate(float latitude, float longitude) {
if(CoordinateManager.isValidLatitude(latitude) == true && CoordinateManager.isValidLongitude(longitude) == true) {
this.latitude = latitude;
this.longitude = longitude;
} else {
throw new IllegalArgumentException("The parameters did not pass validation as defined by the CoordinateManager class");
}
this.format = new DecimalFormat("##.######");
}
/*
* get and set methods
*/
public float getLatitude() {
return latitude;
}
public float getLongitude() {
return longitude;
}
public void setLatitude(float latitude) {
if(CoordinateManager.isValidLatitude(latitude) == true) {
this.latitude = latitude;
} else {
throw new IllegalArgumentException("The parameter did not pass validation as defined by the CoordinateManager class");
}
}
public void setLongitude(float longitude) {
if(CoordinateManager.isValidLongitude(longitude) == true) {
this.longitude = longitude;
} else {
throw new IllegalArgumentException("The parameter did not pass validation as defined by the CoordinateManager class");
}
}
public String getLatitudeAsString() {
return format.format(latitude);
}
public String getLongitudeAsString() {
return format.format(longitude);
}
public String toString() {
return format.format(latitude) + ", " + format.format(longitude);
}
/*
* methods required for ordering in collections
* http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
*/
/**
* A method to determine if one event is the same as another
*
* @param o the object to compare this one to
*
* @return true if they are equal, false if they are not
*/
public boolean equals(Object o) {
// check to make sure the object is an event
if ((o instanceof Coordinate) == false) {
// o is not an event object
return false;
}
// compare these two events
Coordinate c = (Coordinate)o;
// build items for comparison
String me = this.getLatitudeAsString() + this.getLongitudeAsString();
String you = c.getLatitudeAsString() + c.getLongitudeAsString();
return me.equals(you);
} // end equals method
/**
* Overide the default hashcode method
*
* @return a hashcode for this object
*/
public int hashCode() {
String me = this.getLatitudeAsString() + this.getLongitudeAsString();
return 31*me.hashCode();
}
/**
* The compareTo method compares the receiving object with the specified object and returns a
* negative integer, 0, or a positive integer depending on whether the receiving object is
* less than, equal to, or greater than the specified object.
*
* @param c the event to compare this one to
*
* @return an integer indicating comparison result
*/
public int compareTo(Coordinate c) {
String me = this.getLatitudeAsString() + this.getLongitudeAsString();
String you = c.getLatitudeAsString() + c.getLongitudeAsString();
Double meDbl = Double.valueOf(me);
Double youDbl = Double.valueOf(you);
if(meDbl == youDbl) {
return 0;
} else {
Double tmp = Math.floor(meDbl - youDbl);
return tmp.intValue();
}
} // end compareTo method
}
class CoordinateManager {
// declare public constants
/**
* The minimum allowed latitude
*/
public static float MIN_LATITUDE = Float.valueOf("-90.0000");
/**
* The maximum allowed latitude
*/
public static float MAX_LATITUDE = Float.valueOf("90.0000");
/**
* The minimum allowed longitude
*/
public static float MIN_LONGITUDE = Float.valueOf("-180.0000");
/**
* The maximum allowed longitude
*/
public static float MAX_LONGITUDE = Float.valueOf("180.0000");
/**
* The diameter of the Earth used in calculations
*/
public static float EARTH_DIAMETER = Float.valueOf("12756.274");
/**
* A method to validate a latitude value
*
* @param latitude the latitude to check is valid
*
* @return true if, and only if, the latitude is within the MIN and MAX latitude
*/
public static boolean isValidLatitude(float latitude) {
if(latitude >= MIN_LATITUDE && latitude <= MAX_LATITUDE) {
return true;
} else {
return false;
}
}
/**
* A method to validate a longitude value
*
* @param longitude the longitude to check is valid
*
* @return true if, and only if, the longitude is between the MIN and MAX longitude
*/
public static boolean isValidLongitude(float longitude) {
if(longitude >= MIN_LONGITUDE && longitude <= MAX_LONGITUDE) {
return true;
} else {
return false;
}
}
/**
* A private method to calculate the latitude constant
*
* @return a double representing the latitude constant
*/
public static double latitudeConstant() {
return EARTH_DIAMETER * (Math.PI / Float.valueOf("360"));
//return EARTH_DIAMETER * (Float.valueOf("3.14") / Float.valueOf("360"));
}
/**
* A private method to caluclate the longitude constant
*
* @param latitude a latitude coordinate in decimal notation
*
* @return a double representing the longitude constant
*/
public static double longitudeConstant(float latitude) {
//return Math.abs( Math.cos(Math.abs(latitude)));
return EARTH_DIAMETER * Math.PI * Math.abs(Math.cos(Math.abs(latitude))) / Float.valueOf("360");
}
/**
* A method to add distance in a northerly direction to a coordinate
*
* @param latitude a latitude coordinate in decimal notation
* @param longitude a longitude coordinate in decimal notation
* @param distance the distance to add in metres
*
* @return the new coordinate
*/
public static Coordinate addDistanceNorth(float latitude, float longitude, int distance) {
// check on the parameters
if(isValidLatitude(latitude) == false || isValidLongitude(longitude) == false || distance <= 0) {
throw new IllegalArgumentException("All parameters are required and must be valid");
}
// convert the distance from metres to kilometers
float kilometers = distance / new Float(1000);
// calculate the new latitude
double newLat = latitude + (kilometers / latitudeConstant());
return new Coordinate(new Float(newLat).floatValue(), longitude);
}
/**
* A method to add distance in a southerly direction to a coordinate
*
* @param latitude a latitude coordinate in decimal notation
* @param longitude a longitude coordinate in decimal notation
* @param distance the distance to add in metres
*
* @return the new coordinate
*/
public static Coordinate addDistanceSouth(float latitude, float longitude, int distance) {
// check on the parameters
if(isValidLatitude(latitude) == false || isValidLongitude(longitude) == false || distance <= 0) {
throw new IllegalArgumentException("All parameters are required and must be valid");
}
// convert the distance from metres to kilometers
float kilometers = distance / new Float(1000);
// calculate the new latitude
double newLat = latitude - (kilometers / latitudeConstant());
return new Coordinate(new Float(newLat).floatValue(), longitude);
}
/**
* A method to add distance in an easterly direction to a coordinate
*
* @param latitude a latitude coordinate in decimal notation
* @param longitude a longitude coordinate in decimal notation
* @param distance the distance to add in metres
*
* @return the new coordinate
*/
public static Coordinate addDistanceEast(float latitude, float longitude, int distance) {
// check on the parameters
if(isValidLatitude(latitude) == false || isValidLongitude(longitude) == false || distance <= 0) {
throw new IllegalArgumentException("All parameters are required and must be valid");
}
// convert the distance from metres to kilometers
float kilometers = distance / 1000;
// calculate the new longitude
double newLng = longitude + (distance / longitudeConstant(latitude));
return new Coordinate(latitude, new Float(newLng).floatValue());
}
/**
* A method to add distance in an westerly direction to a coordinate
*
* @param latitude a latitude coordinate in decimal notation
* @param longitude a longitude coordinate in decimal notation
* @param distance the distance to add in metres
*
* @return the new coordinate
*/
public static Coordinate addDistanceWest(float latitude, float longitude, int distance) {
// check on the parameters
if(isValidLatitude(latitude) == false || isValidLongitude(longitude) == false || distance <= 0) {
throw new IllegalArgumentException("All parameters are required and must be valid");
}
// convert the distance from metres to kilometers
float kilometers = distance / 1000;
// calculate the new longitude
double newLng = longitude - (distance / longitudeConstant(latitude));
return new Coordinate(latitude, new Float(newLng).floatValue());
}
/**
* A method to build four coordinates representing a bounding box given a start coordinate and a distance
*
* @param latitude a latitude coordinate in decimal notation
* @param longitude a longitude coordinate in decimal notation
* @param distance the distance to add in metres
*
* @return a hashMap representing the bounding box (NE,SE,SW,NW)
*/
public static java.util.HashMap<String, Coordinate> getBoundingBox(float latitude, float longitude, int distance) {
// check on the parameters
if(isValidLatitude(latitude) == false || isValidLongitude(longitude) == false || distance <= 0) {
throw new IllegalArgumentException("All parameters are required and must be valid");
}
// convert the distance from metres to kilometers
float kilometers = distance / 1000;
// declare helper variables
java.util.HashMap<String, Coordinate> boundingBox = new java.util.HashMap<String, Coordinate>();
// calculate the coordinates
Coordinate north = addDistanceNorth(latitude, longitude, distance);
Coordinate south = addDistanceSouth(latitude, longitude, distance);
Coordinate east = addDistanceEast(latitude, longitude, distance);
Coordinate west = addDistanceWest(latitude, longitude, distance);
// build the bounding box object
boundingBox.put("NE", new Coordinate(north.getLatitude(), east.getLongitude()));
boundingBox.put("SE", new Coordinate(south.getLatitude(), east.getLongitude()));
boundingBox.put("SW", new Coordinate(south.getLatitude(), west.getLongitude()));
boundingBox.put("NW", new Coordinate(north.getLatitude(), west.getLongitude()));
// return the bounding box object
return boundingBox;
}
}