-
Notifications
You must be signed in to change notification settings - Fork 982
/
GeoPoint.java
344 lines (286 loc) · 11.7 KB
/
GeoPoint.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
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
// Created by plusminus on 21:28:12 - 25.09.2008
package org.osmdroid.util;
import android.location.Location;
import android.os.Parcel;
import android.os.Parcelable;
import org.osmdroid.api.IGeoPoint;
import org.osmdroid.util.constants.GeoConstants;
import org.osmdroid.views.util.constants.MathConstants;
import java.io.Serializable;
/**
* @author Nicolas Gramlich
* @author Theodore Hong
*/
public class GeoPoint implements IGeoPoint, MathConstants, GeoConstants, Parcelable, Serializable, Cloneable {
// ===========================================================
// Constants
// ===========================================================
static final long serialVersionUID = 1L;
// ===========================================================
// Fields
// ===========================================================
private double mLongitude;
private double mLatitude;
private double mAltitude;
// ===========================================================
// Constructors
// ===========================================================
@Deprecated
public GeoPoint(final int aLatitudeE6, final int aLongitudeE6) {
this.mLatitude = aLatitudeE6 / 1E6;
this.mLongitude = aLongitudeE6 / 1E6;
}
@Deprecated
public GeoPoint(final int aLatitudeE6, final int aLongitudeE6, final int aAltitude) {
this.mLatitude = aLatitudeE6 / 1E6;
this.mLongitude = aLongitudeE6 / 1E6;
this.mAltitude = aAltitude;
}
public GeoPoint(final double aLatitude, final double aLongitude) {
this.mLatitude = aLatitude;
this.mLongitude = aLongitude;
}
public GeoPoint(final double aLatitude, final double aLongitude, final double aAltitude) {
this.mLatitude = aLatitude;
this.mLongitude = aLongitude;
this.mAltitude = aAltitude;
}
public GeoPoint(final Location aLocation) {
this(aLocation.getLatitude(), aLocation.getLongitude(), aLocation.getAltitude());
}
public GeoPoint(final GeoPoint aGeopoint) {
this.mLatitude = aGeopoint.mLatitude;
this.mLongitude = aGeopoint.mLongitude;
this.mAltitude = aGeopoint.mAltitude;
}
/**
* @since 6.0.3
*/
public GeoPoint(final IGeoPoint pGeopoint) {
this.mLatitude = pGeopoint.getLatitude();
this.mLongitude = pGeopoint.getLongitude();
}
public static GeoPoint fromDoubleString(final String s, final char spacer) {
final int spacerPos1 = s.indexOf(spacer);
final int spacerPos2 = s.indexOf(spacer, spacerPos1 + 1);
if (spacerPos2 == -1) {
return new GeoPoint(
(Double.parseDouble(s.substring(0, spacerPos1))),
(Double.parseDouble(s.substring(spacerPos1 + 1, s.length()))));
} else {
return new GeoPoint(
(Double.parseDouble(s.substring(0, spacerPos1))),
(Double.parseDouble(s.substring(spacerPos1 + 1, spacerPos2))),
Double.parseDouble(s.substring(spacerPos2 + 1, s.length())));
}
}
public static GeoPoint fromInvertedDoubleString(final String s, final char spacer) {
final int spacerPos1 = s.indexOf(spacer);
final int spacerPos2 = s.indexOf(spacer, spacerPos1 + 1);
if (spacerPos2 == -1) {
return new GeoPoint(
Double.parseDouble(s.substring(spacerPos1 + 1, s.length())),
Double.parseDouble(s.substring(0, spacerPos1)));
} else {
return new GeoPoint(
Double.parseDouble(s.substring(spacerPos1 + 1, spacerPos2)),
Double.parseDouble(s.substring(0, spacerPos1)),
Double.parseDouble(s.substring(spacerPos2 + 1, s.length())));
}
}
@Deprecated
public static GeoPoint fromIntString(final String s) {
final int commaPos1 = s.indexOf(',');
final int commaPos2 = s.indexOf(',', commaPos1 + 1);
if (commaPos2 == -1) {
return new GeoPoint(
Integer.parseInt(s.substring(0, commaPos1)),
Integer.parseInt(s.substring(commaPos1 + 1, s.length())));
} else {
return new GeoPoint(
Integer.parseInt(s.substring(0, commaPos1)),
Integer.parseInt(s.substring(commaPos1 + 1, commaPos2)),
Integer.parseInt(s.substring(commaPos2 + 1, s.length()))
);
}
}
// ===========================================================
// Getter & Setter
// ===========================================================
@Override
public double getLongitude() {
return this.mLongitude;
}
@Override
public double getLatitude() {
return this.mLatitude;
}
public double getAltitude() {
return this.mAltitude;
}
public void setLatitude(final double aLatitude) {
this.mLatitude = aLatitude;
}
public void setLongitude(final double aLongitude) {
this.mLongitude = aLongitude;
}
public void setAltitude(final double aAltitude) {
this.mAltitude = aAltitude;
}
public void setCoords(final double aLatitude, final double aLongitude) {
this.mLatitude = aLatitude;
this.mLongitude = aLongitude;
}
// ===========================================================
// Methods from SuperClass/Interfaces
// ===========================================================
@Override
public GeoPoint clone() {
return new GeoPoint(this.mLatitude, this.mLongitude, this.mAltitude);
}
public String toIntString() {
return new StringBuilder().
append(((int) (this.mLatitude * 1E6))).
append(",").
append(((int) (this.mLongitude * 1E6))).
append(",").
append((int) (this.mAltitude))
.toString();
}
@Override
public String toString() {
return new StringBuilder().append(this.mLatitude).append(",").append(this.mLongitude).append(",").append(this.mAltitude)
.toString();
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (obj == this) {
return true;
}
if (obj.getClass() != getClass()) {
return false;
}
final GeoPoint rhs = (GeoPoint) obj;
return rhs.mLatitude == this.mLatitude && rhs.mLongitude == this.mLongitude && rhs.mAltitude == this.mAltitude;
}
@Override
public int hashCode() {
return 37 * (17 * (int) (mLatitude * 1E-6) + (int) (mLongitude * 1E-6)) + (int) mAltitude;
}
// ===========================================================
// Parcelable
// ===========================================================
private GeoPoint(final Parcel in) {
this.mLatitude = in.readDouble();
this.mLongitude = in.readDouble();
this.mAltitude = in.readDouble();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(final Parcel out, final int flags) {
out.writeDouble(mLatitude);
out.writeDouble(mLongitude);
out.writeDouble(mAltitude);
}
public static final Parcelable.Creator<GeoPoint> CREATOR = new Parcelable.Creator<GeoPoint>() {
@Override
public GeoPoint createFromParcel(final Parcel in) {
return new GeoPoint(in);
}
@Override
public GeoPoint[] newArray(final int size) {
return new GeoPoint[size];
}
};
// ===========================================================
// Methods
// ===========================================================
/**
* @return distance in meters
* @see <a href="https://en.wikipedia.org/wiki/Haversine_formula">Haversine formula</a>
* @see <a href="http://www.movable-type.co.uk/scripts/gis-faq-5.1.html">GIS FAQ</a>
* @since 6.0.0
*/
public double distanceToAsDouble(final IGeoPoint other) {
final double lat1 = DEG2RAD * getLatitude();
final double lat2 = DEG2RAD * other.getLatitude();
final double lon1 = DEG2RAD * getLongitude();
final double lon2 = DEG2RAD * other.getLongitude();
return RADIUS_EARTH_METERS * 2 * Math.asin(Math.min(1, Math.sqrt(
Math.pow(Math.sin((lat2 - lat1) / 2), 2)
+ Math.cos(lat1) * Math.cos(lat2)
* Math.pow(Math.sin((lon2 - lon1) / 2), 2)
)));
}
/**
* @return bearing in degrees
* @see <a href="http://groups.google.com/group/osmdroid/browse_thread/thread/d22c4efeb9188fe9/bc7f9b3111158dd">discussion</a>
*/
public double bearingTo(final IGeoPoint other) {
final double lat1 = Math.toRadians(this.mLatitude);
final double long1 = Math.toRadians(this.mLongitude);
final double lat2 = Math.toRadians(other.getLatitude());
final double long2 = Math.toRadians(other.getLongitude());
final double delta_long = long2 - long1;
final double a = Math.sin(delta_long) * Math.cos(lat2);
final double b = Math.cos(lat1) * Math.sin(lat2) -
Math.sin(lat1) * Math.cos(lat2) * Math.cos(delta_long);
final double bearing = Math.toDegrees(Math.atan2(a, b));
final double bearing_normalized = (bearing + 360) % 360;
return bearing_normalized;
}
/**
* Calculate a point that is the specified distance and bearing away from this point.
*
* @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">latlong.html</a>
* @see <a href="http://www.movable-type.co.uk/scripts/latlon.js">latlon.js</a>
*/
public GeoPoint destinationPoint(final double aDistanceInMeters, final double aBearingInDegrees) {
// convert distance to angular distance
final double dist = aDistanceInMeters / RADIUS_EARTH_METERS;
// convert bearing to radians
final double brng = DEG2RAD * aBearingInDegrees;
// get current location in radians
final double lat1 = DEG2RAD * getLatitude();
final double lon1 = DEG2RAD * getLongitude();
final double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1)
* Math.sin(dist) * Math.cos(brng));
final double lon2 = lon1
+ Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist)
- Math.sin(lat1) * Math.sin(lat2));
final double lat2deg = lat2 / DEG2RAD;
final double lon2deg = lon2 / DEG2RAD;
return new GeoPoint(lat2deg, lon2deg);
}
public static GeoPoint fromCenterBetween(final GeoPoint geoPointA, final GeoPoint geoPointB) {
return new GeoPoint((geoPointA.getLatitude() + geoPointB.getLatitude()) / 2,
(geoPointA.getLongitude() + geoPointB.getLongitude()) / 2);
}
public String toDoubleString() {
return new StringBuilder().append(this.mLatitude).append(",")
.append(this.mLongitude).append(",").append(this.mAltitude).toString();
}
public String toInvertedDoubleString() {
return new StringBuilder().append(this.mLongitude).append(",")
.append(this.mLatitude).append(",").append(this.mAltitude).toString();
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
@Deprecated
@Override
public int getLatitudeE6() {
return (int) (this.getLatitude() * 1E6);
}
@Deprecated
@Override
public int getLongitudeE6() {
return (int) (this.getLongitude() * 1E6);
}
}