BearingToUtils.java
3.5 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
package cn.com.glsx.entmicro.util;
/**
* @Title: 计算方位角工具类
* @Description:计算方位角工具类
* @author huanggl
* @version V1.0
* @Company: Didihu.com.cn
* @history 2019-5-1
*/
public class BearingToUtils {
private static int COUNT2 = 2;
public static void computeDistanceAndBearing(double lat1, double lon1,double lat2, double lon2, double[] results) {
int maxIters = 20;
lat1 *= Math.PI / 180.0;
lat2 *= Math.PI / 180.0;
lon1 *= Math.PI / 180.0;
lon2 *= Math.PI / 180.0;
double a = 6378137.0;
double b = 6356752.3142;
double f = (a - b) / a;
double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);
double l = lon2 - lon1;
double zheOne = 0.0;
double u1 = Math.atan((1.0 - f) * Math.tan(lat1));
double u2 = Math.atan((1.0 - f) * Math.tan(lat2));
double cosU1 = Math.cos(u1);
double cosU2 = Math.cos(u2);
double sinU1 = Math.sin(u1);
double sinU2 = Math.sin(u2);
double cosU1cosU2 = cosU1 * cosU2;
double sinU1sinU2 = sinU1 * sinU2;
double sigma = 0.0;
double deltaSigma = 0.0;
double cosSqAlpha = 0.0;
double cos2SM = 0.0;
double cosSigma = 0.0;
double sinSigma = 0.0;
double cosLambda = 0.0;
double sinLambda = 0.0;
double lambda = l;
for (int iter = 0; iter < maxIters; iter++) {
double lambdaOrig = lambda;
cosLambda = Math.cos(lambda);
sinLambda = Math.sin(lambda);
double t1 = cosU2 * sinLambda;
double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
double sinSqSigma = t1 * t1 + t2 * t2;
sinSigma = Math.sqrt(sinSqSigma);
cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda;
sigma = Math.atan2(sinSigma, cosSigma);
double sinAlpha = (sinSigma == 0) ? 0.0 : cosU1cosU2 * sinLambda / sinSigma;
cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
cos2SM = (cosSqAlpha == 0) ? 0.0 : cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha;
double uSquared = cosSqAlpha * aSqMinusBSqOverBSq;
zheOne = 1 + (uSquared / 16384.0) * (4096.0 + uSquared * (-768 + uSquared * (320.0 - 175.0 * uSquared)));
double theTwo = (uSquared / 1024.0) * (256.0 + uSquared * (-128.0 + uSquared * (74.0 - 47.0 * uSquared)));
double theThree = (f / 16.0) * cosSqAlpha * (4.0 + f * (4.0 - 3.0 * cosSqAlpha));
double cos2SMSq = cos2SM * cos2SM;
deltaSigma = theTwo * sinSigma * (cos2SM + (theTwo / 4.0) * (cosSigma * (-1.0 + 2.0 * cos2SMSq) - (theTwo / 6.0) * cos2SM * (-3.0 + 4.0 * sinSigma * sinSigma) * (-3.0 + 4.0 * cos2SMSq)));
lambda = l + (1.0 - theThree) * f * sinAlpha * (sigma + theThree * sinSigma * (cos2SM + theThree * cosSigma * (-1.0 + 2.0 * cos2SM * cos2SM)));
double delta = (lambda - lambdaOrig) / lambda;
if (Math.abs(delta) < 1.0e-12) {
break;
}
}
double distance = (b * zheOne * (sigma - deltaSigma));
results[0] = distance;
if (results.length > 1) {
double initialBearing = (float) Math.atan2(cosU2 * sinLambda,cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
initialBearing *= 180.0 / Math.PI;
results[1] = initialBearing;
if (results.length > COUNT2) {
double finalBearing = (float) Math.atan2(cosU1 * sinLambda, -sinU1 * cosU2 + cosU1 * sinU2 * cosLambda);
finalBearing *= 180.0 / Math.PI;
results[2] = finalBearing;
}
}
}
}