1 |
torben |
2211 |
package dk.daoas.fulddaekning; |
2 |
|
|
|
3 |
|
|
public class GeoPoint { |
4 |
|
|
|
5 |
|
|
public double latitude; |
6 |
|
|
public double longitude; |
7 |
|
|
|
8 |
|
|
public GeoPoint() { //Default |
9 |
|
|
} |
10 |
|
|
|
11 |
|
|
public GeoPoint(double lat, double lng) { //Default |
12 |
|
|
latitude=lat; |
13 |
|
|
longitude=lng; |
14 |
|
|
} |
15 |
torben |
2578 |
|
16 |
|
|
//Denne er alt for upræcis |
17 |
|
|
@Deprecated |
18 |
|
|
public static double beregnAfstand_old(GeoPoint point1, GeoPoint point2) { |
19 |
torben |
2211 |
//(62.8*sqrt(3.1*(Power(a.Latitude-x.Latitude,2)+Power(a.Longitude-x.Longitude,2)))) as Afstand, |
20 |
|
|
|
21 |
|
|
|
22 |
|
|
double pwrLat = Math.pow(point1.latitude - point2.latitude, 2); |
23 |
|
|
double pwrLng = Math.pow(point1.longitude - point2.longitude, 2); |
24 |
|
|
|
25 |
|
|
return 62.8 * Math.sqrt( 3.1 * (pwrLat + pwrLng) ); |
26 |
|
|
} |
27 |
torben |
2577 |
|
28 |
torben |
2579 |
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ |
29 |
|
|
/*:: This function converts decimal degrees to radians :*/ |
30 |
|
|
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ |
31 |
|
|
private static double deg2rad(double deg) { |
32 |
|
|
return (deg * Math.PI / 180.0); |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ |
36 |
|
|
/*:: This function converts radians to decimal degrees :*/ |
37 |
|
|
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ |
38 |
|
|
private static double rad2deg(double rad) { |
39 |
|
|
return (rad * 180 / Math.PI); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
//http://www.geodatasource.com/developers/java |
43 |
|
|
private static double distanceHaversine(double lat1, double lon1, double lat2, double lon2) { |
44 |
|
|
double theta = lon1 - lon2; |
45 |
|
|
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta)); |
46 |
|
|
dist = Math.acos(dist); |
47 |
|
|
dist = rad2deg(dist); |
48 |
|
|
dist = dist * 60 * 1.1515; |
49 |
|
|
|
50 |
|
|
//indtil nu er dist i miles - så vi omregner lige til km |
51 |
|
|
dist = dist * 1.609344; |
52 |
|
|
return (dist); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
public static double beregnAfstand(GeoPoint p1, GeoPoint p2) { |
56 |
|
|
return distanceHaversine(p1.latitude, p1.longitude, p2.latitude, p2.longitude); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
// denne er nok den mest præcise - men er også den langsomste |
61 |
|
|
public static float beregnAfstand_google(GeoPoint p1, GeoPoint p2) { |
62 |
torben |
2577 |
float[] result = new float[1]; |
63 |
|
|
|
64 |
torben |
2578 |
computeDistanceAndBearing(p1.latitude, p1.longitude, p2.latitude, p2.longitude, result); |
65 |
torben |
2577 |
|
66 |
torben |
2578 |
return (result[0] / 1000.0f); |
67 |
torben |
2577 |
} |
68 |
torben |
2222 |
|
69 |
|
|
|
70 |
|
|
//Latitude (horizonal), longitude(vertical) so |
71 |
|
|
// 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same |
72 |
|
|
// 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles. |
73 |
|
|
// so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95) |
74 |
|
|
// and is 59.4km at northern point (skagen = 57.75,10.65) |
75 |
|
|
|
76 |
|
|
public static double kmToLatitude(double km) { |
77 |
|
|
return km / 111.320 ; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
public static double kmToLongitude( double km) {//denne er kun ca |
81 |
|
|
return km / 62.0; |
82 |
|
|
} |
83 |
torben |
2577 |
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
//Kopieret fra android.location.Location |
87 |
|
|
private static void computeDistanceAndBearing(double lat1, double lon1, |
88 |
|
|
double lat2, double lon2, float[] results) { |
89 |
|
|
// Based on http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf |
90 |
|
|
// using the "Inverse Formula" (section 4) |
91 |
|
|
|
92 |
|
|
int MAXITERS = 20; |
93 |
|
|
// Convert lat/long to radians |
94 |
|
|
lat1 *= Math.PI / 180.0; |
95 |
|
|
lat2 *= Math.PI / 180.0; |
96 |
|
|
lon1 *= Math.PI / 180.0; |
97 |
|
|
lon2 *= Math.PI / 180.0; |
98 |
|
|
|
99 |
|
|
double a = 6378137.0; // WGS84 major axis |
100 |
|
|
double b = 6356752.3142; // WGS84 semi-major axis |
101 |
|
|
double f = (a - b) / a; |
102 |
|
|
double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b); |
103 |
|
|
|
104 |
|
|
double L = lon2 - lon1; |
105 |
|
|
double A = 0.0; |
106 |
|
|
double U1 = Math.atan((1.0 - f) * Math.tan(lat1)); |
107 |
|
|
double U2 = Math.atan((1.0 - f) * Math.tan(lat2)); |
108 |
|
|
|
109 |
|
|
double cosU1 = Math.cos(U1); |
110 |
|
|
double cosU2 = Math.cos(U2); |
111 |
|
|
double sinU1 = Math.sin(U1); |
112 |
|
|
double sinU2 = Math.sin(U2); |
113 |
|
|
double cosU1cosU2 = cosU1 * cosU2; |
114 |
|
|
double sinU1sinU2 = sinU1 * sinU2; |
115 |
|
|
|
116 |
|
|
double sigma = 0.0; |
117 |
|
|
double deltaSigma = 0.0; |
118 |
|
|
double cosSqAlpha = 0.0; |
119 |
|
|
double cos2SM = 0.0; |
120 |
|
|
double cosSigma = 0.0; |
121 |
|
|
double sinSigma = 0.0; |
122 |
|
|
double cosLambda = 0.0; |
123 |
|
|
double sinLambda = 0.0; |
124 |
|
|
|
125 |
|
|
double lambda = L; // initial guess |
126 |
|
|
for (int iter = 0; iter < MAXITERS; iter++) { |
127 |
|
|
double lambdaOrig = lambda; |
128 |
|
|
cosLambda = Math.cos(lambda); |
129 |
|
|
sinLambda = Math.sin(lambda); |
130 |
|
|
double t1 = cosU2 * sinLambda; |
131 |
|
|
double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda; |
132 |
|
|
double sinSqSigma = t1 * t1 + t2 * t2; // (14) |
133 |
|
|
sinSigma = Math.sqrt(sinSqSigma); |
134 |
|
|
cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15) |
135 |
|
|
sigma = Math.atan2(sinSigma, cosSigma); // (16) |
136 |
|
|
double sinAlpha = (sinSigma == 0) ? 0.0 : |
137 |
|
|
cosU1cosU2 * sinLambda / sinSigma; // (17) |
138 |
|
|
cosSqAlpha = 1.0 - sinAlpha * sinAlpha; |
139 |
|
|
cos2SM = (cosSqAlpha == 0) ? 0.0 : |
140 |
|
|
cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18) |
141 |
|
|
|
142 |
|
|
double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn |
143 |
|
|
A = 1 + (uSquared / 16384.0) * // (3) |
144 |
|
|
(4096.0 + uSquared * |
145 |
|
|
(-768 + uSquared * (320.0 - 175.0 * uSquared))); |
146 |
|
|
double B = (uSquared / 1024.0) * // (4) |
147 |
|
|
(256.0 + uSquared * |
148 |
|
|
(-128.0 + uSquared * (74.0 - 47.0 * uSquared))); |
149 |
|
|
double C = (f / 16.0) * |
150 |
|
|
cosSqAlpha * |
151 |
|
|
(4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10) |
152 |
|
|
double cos2SMSq = cos2SM * cos2SM; |
153 |
|
|
deltaSigma = B * sinSigma * // (6) |
154 |
|
|
(cos2SM + (B / 4.0) * |
155 |
|
|
(cosSigma * (-1.0 + 2.0 * cos2SMSq) - |
156 |
|
|
(B / 6.0) * cos2SM * |
157 |
|
|
(-3.0 + 4.0 * sinSigma * sinSigma) * |
158 |
|
|
(-3.0 + 4.0 * cos2SMSq))); |
159 |
|
|
|
160 |
|
|
lambda = L + |
161 |
|
|
(1.0 - C) * f * sinAlpha * |
162 |
|
|
(sigma + C * sinSigma * |
163 |
|
|
(cos2SM + C * cosSigma * |
164 |
|
|
(-1.0 + 2.0 * cos2SM * cos2SM))); // (11) |
165 |
|
|
|
166 |
|
|
double delta = (lambda - lambdaOrig) / lambda; |
167 |
|
|
if (Math.abs(delta) < 1.0e-12) { |
168 |
|
|
break; |
169 |
|
|
} |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
float distance = (float) (b * A * (sigma - deltaSigma)); |
173 |
|
|
results[0] = distance; |
174 |
|
|
if (results.length > 1) { |
175 |
|
|
float initialBearing = (float) Math.atan2(cosU2 * sinLambda, |
176 |
|
|
cosU1 * sinU2 - sinU1 * cosU2 * cosLambda); |
177 |
|
|
initialBearing *= 180.0 / Math.PI; |
178 |
|
|
results[1] = initialBearing; |
179 |
|
|
if (results.length > 2) { |
180 |
|
|
float finalBearing = (float) Math.atan2(cosU1 * sinLambda, |
181 |
|
|
-sinU1 * cosU2 + cosU1 * sinU2 * cosLambda); |
182 |
|
|
finalBearing *= 180.0 / Math.PI; |
183 |
|
|
results[2] = finalBearing; |
184 |
|
|
} |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
/** |
189 |
|
|
* Computes the approximate distance in meters between two |
190 |
|
|
* locations, and optionally the initial and final bearings of the |
191 |
|
|
* shortest path between them. Distance and bearing are defined using the |
192 |
|
|
* WGS84 ellipsoid. |
193 |
|
|
* |
194 |
|
|
* <p> The computed distance is stored in results[0]. If results has length |
195 |
|
|
* 2 or greater, the initial bearing is stored in results[1]. If results has |
196 |
|
|
* length 3 or greater, the final bearing is stored in results[2]. |
197 |
|
|
* |
198 |
|
|
* @param startLatitude the starting latitude |
199 |
|
|
* @param startLongitude the starting longitude |
200 |
|
|
* @param endLatitude the ending latitude |
201 |
|
|
* @param endLongitude the ending longitude |
202 |
|
|
* @param results an array of floats to hold the results |
203 |
|
|
* |
204 |
|
|
* @throws IllegalArgumentException if results is null or has length < 1 |
205 |
|
|
*/ |
206 |
|
|
public static void distanceBetween(double startLatitude, double startLongitude, |
207 |
|
|
double endLatitude, double endLongitude, float[] results) { |
208 |
|
|
if (results == null || results.length < 1) { |
209 |
|
|
throw new IllegalArgumentException("results is null or has length < 1"); |
210 |
|
|
} |
211 |
|
|
computeDistanceAndBearing(startLatitude, startLongitude, |
212 |
|
|
endLatitude, endLongitude, results); |
213 |
|
|
} |
214 |
|
|
|
215 |
torben |
2211 |
} |