GeoSpherePosition.java 11.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
package de.tud.kom.p2psim.impl.util.positioning;

import java.util.Map;
import java.util.Random;
import java.util.WeakHashMap;

import de.tud.kom.p2psim.impl.network.modular.common.GeoToolkit;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.common.Transmitable;
Björn Richerzhagen's avatar
Björn Richerzhagen committed
10
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
11
12
13
14
15
16
17
18

/** Immutable geographical position implementation based on a spherical Earth model.
 *
 * Latitude and Longitude are internally stored in radians to avoid
 * unnecessary conversions between degrees and radians.
 *
 * @author Andreas Hemel
 */
19
public class GeoSpherePosition implements Transmitable, Location {
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

	/** Latitude in radians */
	private final double latitude;

	/** Longitude in radians */
	private final double longitude;

	/** Earth's mean radius in meters according to Wikipedia */
	private static final double earthRadius = 6371000;

	// FIXME: try to set this to zero
	private static final double deviation = 0.0000001;

	//private static final Logger log = SimLogger.getLogger(GeoPosition.class);

	private static final Map<GeoSpherePosition, Map<GeoSpherePosition, Double>> distanceCache =
			new WeakHashMap<GeoSpherePosition, Map<GeoSpherePosition, Double>>();
	private static final Map<GeoSpherePosition, Map<GeoSpherePosition, Double>> bearingCache =
			new WeakHashMap<GeoSpherePosition, Map<GeoSpherePosition, Double>>();

	private static long distanceCacheHits = 0;
	private static long distanceCacheMisses = 0;

	private static long bearingCacheHits = 0;
	private static long bearingCacheMisses = 0;

	private static final boolean enableDistanceCache = true;
	private static final boolean enableBearingCache = false;

	public GeoSpherePosition(double latitude, double longitude, boolean isRadians) {
		if (isRadians) {
			this.latitude = latitude;
			this.longitude = longitude;
		} else {
			if (latitude > 90 || latitude < -90)
				throw new AssertionError("invalid latitude: "+latitude);
			if (longitude > 180 || longitude < -180)
				throw new AssertionError("invalid longitude: "+longitude);
			this.latitude = Math.toRadians(latitude);
			this.longitude = Math.toRadians(longitude);
		}
		if (this.latitude > Math.PI/2 + deviation || this.latitude < -(Math.PI/2) - deviation)
			throw new AssertionError("invalid latitude: "+latitude);
		if (this.longitude > Math.PI + deviation || this.longitude < -Math.PI - deviation)
			throw new AssertionError("invalid longitude: "+longitude);
	}

	/** Constructor for degrees
	 *
	 * @param latitude Latitude in degrees
	 * @param longitude Longitude in degrees
	 */
	public GeoSpherePosition(double latitude, double longitude) {
		this(latitude, longitude, false);
	}

	public static GeoSpherePosition createRandom() {
		return createRandom(0);
	}

	public static GeoSpherePosition createRandom(double poleExclusion) {
		Random rnd = Randoms.getRandom(GeoSpherePosition.class);

		double latRange = 180 - poleExclusion; // leave out the area near the poles
		double longRange = 360;
		double latitude = rnd.nextDouble() * latRange - (latRange / 2);
		double longitude = rnd.nextDouble() * longRange - (longRange / 2);

		return new GeoSpherePosition(latitude, longitude);
	}

	/** Get the latitude in degrees */
Björn Richerzhagen's avatar
Björn Richerzhagen committed
92
	@Override
93
	public double getLatitudeOrY() {
94
95
96
97
		return Math.toDegrees(latitude);
	}

	/** Get the longitude in degrees */
Björn Richerzhagen's avatar
Björn Richerzhagen committed
98
	@Override
99
	public double getLongitudeOrX() {
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
		return Math.toDegrees(longitude);
	}

	/** Get the latitude in radians */
	public double getLatitudeRad() {
		return latitude;
	}

	/** Get the longitude in radians */
	public double getLongitudeRad() {
		return longitude;
	}

	@Override
	public int getTransmissionSize() {
		return 16; // 2 * sizeof(double)
	}

	private double square(double x) { return x*x; }

	/** Calculate the distance to target on a great circle using the Haversine
	 * Formula.
	 *
	 * This formula assumes a spherical Earth, so this calculation has
	 * a slight error, but it should be consistent with getDestination().
	 *
	 * based on:
	 * {@link GeoToolkit}
	 * http://en.wikipedia.org/wiki/Haversine_formula
	 *
	 * @return The distance in meters.
	 */
	@Override
133
	public double distanceTo(Location destination) {
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
		GeoSpherePosition dest = (GeoSpherePosition) destination;
		if (enableDistanceCache) {
			Double cached = checkDistanceCache(this, dest);
			if (cached != null)
				return cached;
		}

		double lat1 = this.latitude;
		double lat2 = dest.latitude;
		double dlat = lat2 - lat1;
		double dlong = dest.longitude - this.longitude;

		double a =
				square(Math.sin(dlat / 2d)) +
				Math.cos(lat1) * Math.cos(lat2) * square(Math.sin(dlong / 2d));

		// This is the formula from Wikipedia. It is slightly faster for short
		// distances, e.g. mainz -> ffm
		//double distance = Math.asin(Math.sqrt(a)) * 2d * earthRadius;

		// This is the formula from GeoToolkit. It up to two times faster for long
		// distances, e.g. nyc -> tokyo
		double distance = Math.atan2(Math.sqrt(a), Math.sqrt(1d - a)) * 2d * earthRadius;

		if (enableDistanceCache)
			putDistanceCache(this, dest, distance);

		return distance;
	}

	@Override
165
166
	public float bearingTo(Location target) {
		return (float) (-getBearing(target) + 180);
167
168
169
170
171
172
173
174
175
176
177
178
	}

	/** Calculate the initial bearing to target on a great circle in degrees.
	 *
	 * Range is between 0 and 360 degrees.
	 * 0° is north, 90° is east, 180° is south and 270° is west.
	 *
	 * based on:
	 * http://www.movable-type.co.uk/scripts/latlong.html
	 *
	 * @return The initial bearing in degrees.
	 */
179
	public double getBearing(Location target) {
180
181
182
183
184
185
186
187
188
189
		return Math.toDegrees(getBearingRad(target));
	}

	/** Calculate the initial bearing to target on a great circle in radians.
	 *
	 * based on:
	 * http://www.movable-type.co.uk/scripts/latlong.html
	 *
	 * @return The initial bearing in radians.
	 */
190
	public double getBearingRad(Location destination) {
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
		GeoSpherePosition dest = (GeoSpherePosition) destination;
		if (enableBearingCache) {
			Double cached = checkBearingCache(this, dest);
			if (cached != null)
				return cached;
		}

		double lat1 = this.latitude;
		double lat2 = dest.latitude;
		double dLon = dest.longitude - this.longitude;

		double y = Math.sin(dLon) * Math.cos(lat2);
		double x = Math.cos(lat1) * Math.sin(lat2) -
					Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
		double bearing = Math.atan2(y, x);

		bearing = (bearing + 2*Math.PI) % (2*Math.PI);

		if (enableBearingCache)
			putBearingCache(this, dest, bearing);

		return bearing;
	}

	/** Calculate the destination position given a bearing and a distance.
	 *
	 * The formulae used here assume a spherical Earth, so this calculation has
	 * a slight error, but it should be consistent with getDistance().
	 *
	 * based on:
	 * http://www.movable-type.co.uk/scripts/latlong.html
	 *
	 * @param bearing Bearing in degrees
	 * @param distance Distance in meters
	 */
	public GeoSpherePosition getDestination(double bearing, double distance) {
		return getDestinationRad(Math.toRadians(bearing), distance);
	}

	/** Calculate the destination position given a bearing and a distance.
	 *
	 * The formulae used here assume a spherical Earth, so this calculation has
	 * a slight error, but it should be consistent with getDistance().
	 *
	 * based on:
	 * http://www.movable-type.co.uk/scripts/latlong.html
	 *
	 * @param bearing Bearing in radians
	 * @param distance Distance in meters
	 */
	public GeoSpherePosition getDestinationRad(double bearing, double distance) {
		double lat1 = latitude;
		double lon1 = longitude;
		double radDist = distance / earthRadius;

		double lat2 = Math.asin(Math.sin(lat1) * Math.cos(radDist) +
				Math.cos(lat1) * Math.sin(radDist) * Math.cos(bearing));
		double dlon = Math.atan2(
				Math.sin(bearing) * Math.sin(radDist) * Math.cos(lat1),
				Math.cos(radDist) - Math.sin(lat1) * Math.sin(lat2));
		double lon2 = lon1 + dlon;

		if (lon2 > Math.PI)
			lon2 -= 2*Math.PI;
		if (lon2 < -Math.PI)
			lon2 += 2*Math.PI;

		return new GeoSpherePosition(lat2, lon2, true);
	}

	@Override
	public String toString() {
263
		return "GeoSpherePos["+getLatitudeOrY()+";"+getLongitudeOrX()+"]";
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(latitude);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(longitude);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof GeoSpherePosition))
			return false;
		GeoSpherePosition other = (GeoSpherePosition) obj;
		if (Double.doubleToLongBits(latitude) != Double
				.doubleToLongBits(other.latitude))
			return false;
		if (Double.doubleToLongBits(longitude) != Double
				.doubleToLongBits(other.longitude))
			return false;
		return true;
	}

	/** This class is immutable, no clone needed. */
	@Override
	public GeoSpherePosition clone() {
		return this;
	}

	private static Double checkDistanceCache(GeoSpherePosition a, GeoSpherePosition b) {
		GeoSpherePosition first, second;
		if (a.longitude < b.longitude) {
			first = a;
			second = b;
		} else if (a.longitude > b.longitude) {
			first = b;
			second = a;
		} else if (a.latitude < b.latitude) {
			first = a;
			second = b;
		} else {
			first = b;
			second = a;
		}

		Double result;
		Map<GeoSpherePosition, Double> map = distanceCache.get(first);
		if (map == null)
			result = null;
		else
			result = map.get(second);

		if (result != null)
			distanceCacheHits++;
		else
			distanceCacheMisses++;

		return result;
	}

	private static void putDistanceCache(GeoSpherePosition a, GeoSpherePosition b, double dist) {
		GeoSpherePosition first, second;
		if (a.longitude < b.longitude) {
			first = a;
			second = b;
		} else if (a.longitude > b.longitude) {
			first = b;
			second = a;
		} else if (a.latitude < b.latitude) {
			first = a;
			second = b;
		} else {
			first = b;
			second = a;
		}

		Map<GeoSpherePosition, Double> map = distanceCache.get(first);
		if (map == null) {
			map = new WeakHashMap<GeoSpherePosition, Double>();
			distanceCache.put(first, map);
		}
		map.put(second, dist);
	}

	private static Double checkBearingCache(GeoSpherePosition a, GeoSpherePosition b) {
		GeoSpherePosition first, second;
		if (a.longitude < b.longitude) {
			first = a;
			second = b;
		} else if (a.longitude > b.longitude) {
			first = b;
			second = a;
		} else if (a.latitude < b.latitude) {
			first = a;
			second = b;
		} else {
			first = b;
			second = a;
		}

		Double result;
		Map<GeoSpherePosition, Double> map = bearingCache.get(first);
		if (map == null)
			result = null;
		else
			result = map.get(second);

		if (result != null)
			bearingCacheHits++;
		else
			bearingCacheMisses++;

		return result;
	}

	private static void putBearingCache(GeoSpherePosition a, GeoSpherePosition b, double dist) {
		GeoSpherePosition first, second;
		if (a.longitude < b.longitude) {
			first = a;
			second = b;
		} else if (a.longitude > b.longitude) {
			first = b;
			second = a;
		} else if (a.latitude < b.latitude) {
			first = a;
			second = b;
		} else {
			first = b;
			second = a;
		}

		Map<GeoSpherePosition, Double> map = bearingCache.get(first);
		if (map == null) {
			map = new WeakHashMap<GeoSpherePosition, Double>();
			bearingCache.put(first, map);
		}
		map.put(second, dist);
	}

	public static long getDistanceCacheHits() {
		return distanceCacheHits;
	}

	public static long getDistanceCacheMisses() {
		return distanceCacheMisses;
	}

	public static long getBearingCacheHits() {
		return bearingCacheHits;
	}

	public static long getBearingCacheMisses() {
		return bearingCacheMisses;
	}

	public static boolean isDistanceCacheEnabled() {
		return enableDistanceCache;
	}

	public static boolean isBearingCacheEnabled() {
		return enableBearingCache;
	}
Björn Richerzhagen's avatar
Björn Richerzhagen committed
435
436
437
438
439
440
441
442
443
444

	@Override
	public void set(Location l) {
		throw new UnsupportedOperationException();
	}

	@Override
	public long getAgeOfLocation() {
		throw new UnsupportedOperationException();
	}
445
}