/[projects]/dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/GeoPoint.java
ViewVC logotype

Contents of /dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/GeoPoint.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2577 - (show annotations) (download)
Thu Jun 11 20:10:23 2015 UTC (8 years, 11 months ago) by torben
File size: 6718 byte(s)
add new distance calculator
1 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
16 public static double beregnAfstand(GeoPoint point1, GeoPoint point2) {
17 //(62.8*sqrt(3.1*(Power(a.Latitude-x.Latitude,2)+Power(a.Longitude-x.Longitude,2)))) as Afstand,
18
19
20 double pwrLat = Math.pow(point1.latitude - point2.latitude, 2);
21 double pwrLng = Math.pow(point1.longitude - point2.longitude, 2);
22
23 return 62.8 * Math.sqrt( 3.1 * (pwrLat + pwrLng) );
24 }
25
26 public static float beregnAfstand2(GeoPoint p1, GeoPoint p2) {
27 float[] result = new float[1];
28
29
30 computeDistanceAndBearing(p1.latitude, p1.longitude, p2.latitude, p2.longitude, result);
31
32 return result[0];
33 }
34
35
36 //Latitude (horizonal), longitude(vertical) so
37 // 1 degree latitude is ~ 111320 meters, since the distance between the horizonal lines is always the same
38 // 1 degree longitude is ~111320 meters at equator but gets shorter as we get closer to the poles.
39 // so 1 degree longitude is 64.5 km at denmarks southern point (gedser=54.55,11.95)
40 // and is 59.4km at northern point (skagen = 57.75,10.65)
41
42 public static double kmToLatitude(double km) {
43 return km / 111.320 ;
44 }
45
46 public static double kmToLongitude( double km) {//denne er kun ca
47 return km / 62.0;
48 }
49
50
51
52 //Kopieret fra android.location.Location
53 private static void computeDistanceAndBearing(double lat1, double lon1,
54 double lat2, double lon2, float[] results) {
55 // Based on http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
56 // using the "Inverse Formula" (section 4)
57
58 int MAXITERS = 20;
59 // Convert lat/long to radians
60 lat1 *= Math.PI / 180.0;
61 lat2 *= Math.PI / 180.0;
62 lon1 *= Math.PI / 180.0;
63 lon2 *= Math.PI / 180.0;
64
65 double a = 6378137.0; // WGS84 major axis
66 double b = 6356752.3142; // WGS84 semi-major axis
67 double f = (a - b) / a;
68 double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);
69
70 double L = lon2 - lon1;
71 double A = 0.0;
72 double U1 = Math.atan((1.0 - f) * Math.tan(lat1));
73 double U2 = Math.atan((1.0 - f) * Math.tan(lat2));
74
75 double cosU1 = Math.cos(U1);
76 double cosU2 = Math.cos(U2);
77 double sinU1 = Math.sin(U1);
78 double sinU2 = Math.sin(U2);
79 double cosU1cosU2 = cosU1 * cosU2;
80 double sinU1sinU2 = sinU1 * sinU2;
81
82 double sigma = 0.0;
83 double deltaSigma = 0.0;
84 double cosSqAlpha = 0.0;
85 double cos2SM = 0.0;
86 double cosSigma = 0.0;
87 double sinSigma = 0.0;
88 double cosLambda = 0.0;
89 double sinLambda = 0.0;
90
91 double lambda = L; // initial guess
92 for (int iter = 0; iter < MAXITERS; iter++) {
93 double lambdaOrig = lambda;
94 cosLambda = Math.cos(lambda);
95 sinLambda = Math.sin(lambda);
96 double t1 = cosU2 * sinLambda;
97 double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
98 double sinSqSigma = t1 * t1 + t2 * t2; // (14)
99 sinSigma = Math.sqrt(sinSqSigma);
100 cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15)
101 sigma = Math.atan2(sinSigma, cosSigma); // (16)
102 double sinAlpha = (sinSigma == 0) ? 0.0 :
103 cosU1cosU2 * sinLambda / sinSigma; // (17)
104 cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
105 cos2SM = (cosSqAlpha == 0) ? 0.0 :
106 cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18)
107
108 double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn
109 A = 1 + (uSquared / 16384.0) * // (3)
110 (4096.0 + uSquared *
111 (-768 + uSquared * (320.0 - 175.0 * uSquared)));
112 double B = (uSquared / 1024.0) * // (4)
113 (256.0 + uSquared *
114 (-128.0 + uSquared * (74.0 - 47.0 * uSquared)));
115 double C = (f / 16.0) *
116 cosSqAlpha *
117 (4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10)
118 double cos2SMSq = cos2SM * cos2SM;
119 deltaSigma = B * sinSigma * // (6)
120 (cos2SM + (B / 4.0) *
121 (cosSigma * (-1.0 + 2.0 * cos2SMSq) -
122 (B / 6.0) * cos2SM *
123 (-3.0 + 4.0 * sinSigma * sinSigma) *
124 (-3.0 + 4.0 * cos2SMSq)));
125
126 lambda = L +
127 (1.0 - C) * f * sinAlpha *
128 (sigma + C * sinSigma *
129 (cos2SM + C * cosSigma *
130 (-1.0 + 2.0 * cos2SM * cos2SM))); // (11)
131
132 double delta = (lambda - lambdaOrig) / lambda;
133 if (Math.abs(delta) < 1.0e-12) {
134 break;
135 }
136 }
137
138 float distance = (float) (b * A * (sigma - deltaSigma));
139 results[0] = distance;
140 if (results.length > 1) {
141 float initialBearing = (float) Math.atan2(cosU2 * sinLambda,
142 cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
143 initialBearing *= 180.0 / Math.PI;
144 results[1] = initialBearing;
145 if (results.length > 2) {
146 float finalBearing = (float) Math.atan2(cosU1 * sinLambda,
147 -sinU1 * cosU2 + cosU1 * sinU2 * cosLambda);
148 finalBearing *= 180.0 / Math.PI;
149 results[2] = finalBearing;
150 }
151 }
152 }
153
154 /**
155 * Computes the approximate distance in meters between two
156 * locations, and optionally the initial and final bearings of the
157 * shortest path between them. Distance and bearing are defined using the
158 * WGS84 ellipsoid.
159 *
160 * <p> The computed distance is stored in results[0]. If results has length
161 * 2 or greater, the initial bearing is stored in results[1]. If results has
162 * length 3 or greater, the final bearing is stored in results[2].
163 *
164 * @param startLatitude the starting latitude
165 * @param startLongitude the starting longitude
166 * @param endLatitude the ending latitude
167 * @param endLongitude the ending longitude
168 * @param results an array of floats to hold the results
169 *
170 * @throws IllegalArgumentException if results is null or has length < 1
171 */
172 public static void distanceBetween(double startLatitude, double startLongitude,
173 double endLatitude, double endLongitude, float[] results) {
174 if (results == null || results.length < 1) {
175 throw new IllegalArgumentException("results is null or has length < 1");
176 }
177 computeDistanceAndBearing(startLatitude, startLongitude,
178 endLatitude, endLongitude, results);
179 }
180
181 }

  ViewVC Help
Powered by ViewVC 1.1.20