-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGpsPoint.java
More file actions
126 lines (109 loc) · 4.07 KB
/
GpsPoint.java
File metadata and controls
126 lines (109 loc) · 4.07 KB
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
/* Copyright (C) 2014,2015 Maximilian Diedrich
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package de.hu_berlin.informatik.spws2014.ImagePositionLocator;
import java.io.Serializable;
/**
* Represents a point defined by latitude
* and longitude.
*/
public class GpsPoint implements Serializable {
private static final long serialVersionUID = 1L;
static private transient double RADIUS = 6371; // earth's mean radius in km
public double longitude;
public double latitude;
public long time;
public GpsPoint(double longitude, double latitude, long time) {
this.latitude = latitude;
this.longitude = longitude;
this.time = time;
}
/**
* The new GpsPoint will be the linear weighted average
* of a and b based on time.
*/
public GpsPoint(GpsPoint a, GpsPoint b, long time) {
this.time = time;
this.longitude = linInterpolation(a.time, b.time, time, a.longitude, b.longitude);
this.latitude = linInterpolation(a.time, b.time, time, a.latitude, b.latitude);
}
/**
* The new GpsPoint will be the average of a and b.
*/
public GpsPoint(GpsPoint a, GpsPoint b) {
this.longitude = (a.longitude + b.longitude) / 2;
this.latitude = (a.latitude+ b.latitude) / 2;
this.time = (a.time + b.time) / 2;
}
/**
* The new GpsPoint will be the average of a, b and c.
*/
public GpsPoint(GpsPoint a, GpsPoint b, GpsPoint c) {
this.longitude = (a.longitude + b.longitude + c.longitude) / 3;
this.latitude = (a.latitude+ b.latitude + c.latitude) / 3;
this.time = (a.time + b.time + c.time) / 3;
}
private double linInterpolation(long atime, long btime, long time, double aval, double bval) {
return (bval-aval)/(btime-atime)*(time-atime)+aval;
}
public GpsPoint getOrthogonal(GpsPoint origin) {
double lon_scale = Math.cos(origin.latitude);
double xnew = origin.longitude - (latitude - origin.latitude) / lon_scale;
double ynew = origin.latitude + (longitude - origin.longitude) * lon_scale;
return new GpsPoint(xnew, ynew, this.time);
}
/**
* @return the distance of this point to a
* assuming both are in a plane
*/
public double getPlanarDistance(GpsPoint a) {
double tmp = Math.sqrt(Math.pow(a.latitude - this.latitude, 2) + Math.pow(a.longitude - this.longitude, 2));
if (tmp != 0)
return tmp;
else
return Double.MIN_NORMAL;
}
/**
* Returns the distance from this point to the supplied point, in km (using
* Haversine formula)
*
* from: Haversine formula - R. W. Sinnott, "Virtues of the Haversine", Sky
* and Telescope, vol 68, no 2, 1984
*
* @this {LatLon} latitude/longitude of origin point
* @param {LatLon} point: latitude/longitude of destination point
* @param {Number} [precision=4]: number of significant digits to use for
* returned value
* @returns {Number} distance in km between this point and destination point
*/
public double getSphericalDistance(GpsPoint point) {
double R = RADIUS;
double phi1 = this.latitude * Math.PI / 180d, lambda1 = this.longitude
* Math.PI / 180d;
double phi2 = point.latitude * Math.PI / 180d, lambda2 = point.longitude
* Math.PI / 180d;
double deltaLamda = phi2 - phi1;
double deltaLambda = lambda2 - lambda1;
double a = Math.sin(deltaLamda / 2) * Math.sin(deltaLamda / 2)
+ Math.cos(phi1) * Math.cos(phi2) * Math.sin(deltaLambda / 2)
* Math.sin(deltaLambda / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = R * c;
return d;
}
public String toString() {
return "GPS<" + latitude + "," + longitude + ">";
}
}