01: package eg.net;
02:
03: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
04: // Released under the terms of the GNU General Public License version 2 or later.
05:
06: // import java.util.regex.*;
07:
08: import java.util.StringTokenizer;
09: import java.text.DecimalFormat;
10:
11: public class GeoCoordinate {
12:
13: float lat;
14: float lon;
15:
16: public GeoCoordinate(float lat, float lon) {
17: this .lat = lat;
18: this .lon = lon;
19: }
20:
21: public static GeoCoordinate parse(String string) {
22: StringTokenizer t = new StringTokenizer(string,
23: "nNsSeEwW \'\",", true);
24: float n[] = { 0, 0, 0, 0, 0, 0 };
25: boolean north = true, east = true;
26: for (int i = 0; i < 6 && t.hasMoreTokens();) {
27: String token = t.nextToken().toLowerCase();
28: char ch = token.charAt(0);
29: if (Character.isDigit(ch) || ch == '-') {
30: n[i++] = Float.parseFloat(token);
31: }
32: if (ch == 's') {
33: north = false;
34: }
35: if (ch == 'w') {
36: east = false;
37: }
38: if (i > 0 && "nsew".indexOf(ch) >= 0) {
39: i = 3;
40: }
41: }
42: float lat = n[0] + n[1] / 60 + n[2] / 3600;
43: float lon = n[3] + n[4] / 60 + n[5] / 3600;
44: return new GeoCoordinate(north ? lat : -lat, east ? lon : -lon);
45: }
46:
47: static double precision = 0.00001;
48:
49: public boolean equals(Object arg) {
50: if (!getClass().isInstance(arg))
51: return false;
52: GeoCoordinate coord = (GeoCoordinate) arg;
53: return ((long) (lat / precision)) == ((long) (coord.lat / precision))
54: && ((long) (lon / precision)) == ((long) (coord.lon / precision));
55: }
56:
57: public long hash() {
58: return ((long) (lat / precision)) + ((long) (lon / precision));
59: }
60:
61: public String toString() {
62: DecimalFormat coord = new DecimalFormat();
63: coord.setMaximumFractionDigits(4);
64: return coord.format(Math.abs(lat)) + (lat >= 0 ? "N " : "S ")
65: + coord.format(Math.abs(lon))
66: + (lon >= 0 ? "E " : "W ");
67: }
68: }
|