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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20