1 |
torben |
2585 |
/* |
2 |
|
|
The MIT License (MIT) |
3 |
|
|
[OSI Approved License] |
4 |
|
|
The MIT License (MIT) |
5 |
|
|
|
6 |
|
|
Copyright (c) 2014 Daniel Glasson |
7 |
|
|
|
8 |
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
9 |
|
|
of this software and associated documentation files (the "Software"), to deal |
10 |
|
|
in the Software without restriction, including without limitation the rights |
11 |
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 |
|
|
copies of the Software, and to permit persons to whom the Software is |
13 |
|
|
furnished to do so, subject to the following conditions: |
14 |
|
|
|
15 |
|
|
The above copyright notice and this permission notice shall be included in |
16 |
|
|
all copies or substantial portions of the Software. |
17 |
|
|
|
18 |
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 |
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 |
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 |
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 |
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 |
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 |
|
|
THE SOFTWARE. |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
package geocode; |
28 |
|
|
|
29 |
|
|
import static java.lang.Math.cos; |
30 |
|
|
import static java.lang.Math.sin; |
31 |
|
|
import static java.lang.Math.toRadians; |
32 |
|
|
import geocode.kdtree.KDNodeComparator; |
33 |
|
|
|
34 |
|
|
import java.util.Comparator; |
35 |
|
|
|
36 |
|
|
/** |
37 |
|
|
* Created by Daniel Glasson on 18/05/2014. |
38 |
|
|
* This class works with a placenames files from http://download.geonames.org/export/dump/ |
39 |
|
|
*/ |
40 |
|
|
|
41 |
|
|
public class GeoPoint extends KDNodeComparator<GeoPoint> { |
42 |
|
|
public double latitude; |
43 |
|
|
public double longitude; |
44 |
|
|
public double point[] = new double[3]; // The 3D coordinates of the point |
45 |
|
|
|
46 |
|
|
|
47 |
|
|
public GeoPoint(Double latitude, Double longitude) { |
48 |
|
|
this.latitude = latitude; |
49 |
|
|
this.longitude = longitude; |
50 |
|
|
setPoint(); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
private void setPoint() { |
54 |
|
|
point[0] = cos(toRadians(latitude)) * cos(toRadians(longitude)); |
55 |
|
|
point[1] = cos(toRadians(latitude)) * sin(toRadians(longitude)); |
56 |
|
|
point[2] = sin(toRadians(latitude)); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
@Override |
61 |
|
|
protected boolean isNodeCompatible(GeoPoint other) { |
62 |
|
|
return true; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
@Override |
66 |
|
|
protected double squaredDistance(GeoPoint other) { |
67 |
|
|
double x = this.point[0] - other.point[0]; |
68 |
|
|
double y = this.point[1] - other.point[1]; |
69 |
|
|
double z = this.point[2] - other.point[2]; |
70 |
|
|
return (x*x) + (y*y) + (z*z); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
@Override |
74 |
|
|
protected double axisSquaredDistance(GeoPoint other, int axis) { |
75 |
|
|
double distance = point[axis] - other.point[axis]; |
76 |
|
|
return distance * distance; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
@Override |
80 |
|
|
protected Comparator<GeoPoint> getComparator(int axis) { |
81 |
|
|
return GeoNameComparator.values()[axis]; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
protected static enum GeoNameComparator implements Comparator<GeoPoint> { |
85 |
|
|
x { |
86 |
|
|
@Override |
87 |
|
|
public int compare(GeoPoint a, GeoPoint b) { |
88 |
|
|
return Double.compare(a.point[0], b.point[0]); |
89 |
|
|
} |
90 |
|
|
}, |
91 |
|
|
y { |
92 |
|
|
@Override |
93 |
|
|
public int compare(GeoPoint a, GeoPoint b) { |
94 |
|
|
return Double.compare(a.point[1], b.point[1]); |
95 |
|
|
} |
96 |
|
|
}, |
97 |
|
|
z { |
98 |
|
|
@Override |
99 |
|
|
public int compare(GeoPoint a, GeoPoint b) { |
100 |
|
|
return Double.compare(a.point[2], b.point[2]); |
101 |
|
|
} |
102 |
|
|
}; |
103 |
|
|
} |
104 |
|
|
} |