1 |
torben |
2878 |
package dk.daoas.adressevedligehold.afstandandenrute; |
2 |
|
|
|
3 |
|
|
import dk.daoas.adressevedligehold.beans.Address; |
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
|
|
public class GeoPointHelper { |
8 |
|
|
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
// Latitude (horizonal), longitude(vertical) so |
12 |
|
|
// 1 degree latitude is ~ 111320 meters, since the distance between the |
13 |
|
|
// horizonal lines is always the same |
14 |
|
|
// 1 degree longitude is ~111320 meters at equator but gets shorter as we |
15 |
|
|
// get closer to the poles. |
16 |
|
|
// so 1 degree longitude is 64.5 km at denmarks southern point |
17 |
|
|
// (gedser=54.55,11.95) |
18 |
|
|
// and is 59.4km at northern point (skagen = 57.75,10.65) |
19 |
|
|
|
20 |
|
|
public static double kmToLatitude(double km) { |
21 |
|
|
return km / 111.320; |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
public static double kmToLongitude(double km) {// denne er kun ca |
25 |
|
|
return km / 62.0; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
29 |
|
|
|
30 |
|
|
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ |
31 |
|
|
/* :: This function converts decimal degrees to radians : */ |
32 |
|
|
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ |
33 |
|
|
private static double deg2rad(double deg) { |
34 |
|
|
return (deg * Math.PI / 180.0); |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ |
38 |
|
|
/* :: This function converts radians to decimal degrees : */ |
39 |
|
|
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ |
40 |
|
|
private static double rad2deg(double rad) { |
41 |
|
|
return (rad * 180 / Math.PI); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
// http://www.geodatasource.com/developers/java |
45 |
|
|
private static double distanceHaversine(double lat1, double lon1, |
46 |
|
|
double lat2, double lon2) { |
47 |
|
|
double theta = lon1 - lon2; |
48 |
|
|
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) |
49 |
|
|
+ Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) |
50 |
|
|
* Math.cos(deg2rad(theta)); |
51 |
|
|
dist = Math.acos(dist); |
52 |
|
|
dist = rad2deg(dist); |
53 |
|
|
dist = dist * 60 * 1.1515; |
54 |
|
|
|
55 |
|
|
// indtil nu er dist i miles - så vi omregner lige til km |
56 |
|
|
dist = dist * 1.609344; |
57 |
|
|
return (dist); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
public static double beregnAfstand(Address p1, Address p2) { |
61 |
|
|
if (p1.latitude == p2.latitude && p1.longitude == p2.longitude) { |
62 |
|
|
return 0.00; //Haversine går i baglås hvis p1 og p2 er samme position |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
return distanceHaversine(p1.latitude, p1.longitude, p2.latitude, p2.longitude); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
} |