-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoName.java
122 lines (103 loc) · 3.89 KB
/
GeoName.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
/*
The MIT License (MIT)
[OSI Approved License]
The MIT License (MIT)
Copyright (c) 2014 Daniel Glasson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.vedamic.mymap.library.map.geocode.offline;
import com.vedamic.mymap.library.map.geocode.offline.kdtree.KDNodeComparator;
import java.util.Comparator;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.toRadians;
/**
* Created by Daniel Glasson on 18/05/2014.
* This class works with a placenames files from http://download.geonames.org/export/dump/
*/
public class GeoName extends KDNodeComparator<GeoName> {
public String name;
public boolean majorPlace; // Major or minor place
public double latitude;
public double longitude;
public double point[] = new double[3]; // The 3D coordinates of the point
public String country;
GeoName(String data) {
String[] names = data.split("\t");
name = names[1];
majorPlace = names[6].equals("P");
latitude = Double.parseDouble(names[4]);
longitude = Double.parseDouble(names[5]);
setPoint();
country = names[8];
}
GeoName(Double latitude, Double longitude) {
name = country = "Search";
this.latitude = latitude;
this.longitude = longitude;
setPoint();
}
private void setPoint() {
point[0] = cos(toRadians(latitude)) * cos(toRadians(longitude));
point[1] = cos(toRadians(latitude)) * sin(toRadians(longitude));
point[2] = sin(toRadians(latitude));
}
@Override
public String toString() {
return "GeoName{" +
"name='" + name + '\'' +
", majorPlace=" + majorPlace +
", country='" + country + '\'' +
'}';
}
@Override
protected double squaredDistance(GeoName other) {
double x = this.point[0] - other.point[0];
double y = this.point[1] - other.point[1];
double z = this.point[2] - other.point[2];
return (x*x) + (y*y) + (z*z);
}
@Override
protected double axisSquaredDistance(GeoName other, int axis) {
double distance = point[axis] - other.point[axis];
return distance * distance;
}
@Override
protected Comparator<GeoName> getComparator(int axis) {
return GeoNameComparator.values()[axis];
}
protected static enum GeoNameComparator implements Comparator<GeoName> {
x {
@Override
public int compare(GeoName a, GeoName b) {
return Double.compare(a.point[0], b.point[0]);
}
},
y {
@Override
public int compare(GeoName a, GeoName b) {
return Double.compare(a.point[1], b.point[1]);
}
},
z {
@Override
public int compare(GeoName a, GeoName b) {
return Double.compare(a.point[2], b.point[2]);
}
};
}
}