【发布时间】:2010-09-28 04:49:44
【问题描述】:
这是this question 的后续。
我似乎陷入了困境。基本上,我需要能够来回转换以参考标准度数系统中的坐标,或者通过测量沿国际日期变更线从南极向北的距离,然后从日期的那个点开始向东的距离线。为了做到这一点(以及一些更一般的距离测量的东西),我有一种方法来确定两个纬度/经度点之间的距离,另一种方法采用纬度/经度点、航向和距离,然后返回该课程结束时的纬度/经度点。
这是我定义的两个静态方法:
/* Takes two lon/lat pairs and returns the distance between them in kilometers.
*/
public static double distance (double lat1, double lon1, double lat2, double lon2) {
double theta = toRadians(lon1-lon2);
lat1 = toRadians(lat1);
lon1 = toRadians(lon1);
lat2 = toRadians(lat2);
lon2 = toRadians(lon2);
double dist = sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(theta);
dist = toDegrees(acos(dist)) * 60 * 1.1515 * 1.609344 * 1000;
return dist;
}
/* endOfCourse takes a lat/lon pair, a heading (in degrees clockwise from north), and a distance (in kilometers), and returns
* the lat/lon pair that would be reached by traveling that distance in that direction from the given point.
*/
public static double[] endOfCourse (double lat1, double lon1, double tc, double dist) {
double pi = Math.PI;
lat1 = toRadians(lat1);
lon1 = toRadians(lon1);
tc = toRadians(tc);
double dist_radians = toRadians(dist / (60 * 1.1515 * 1.609344 * 1000));
double lat = asin(sin(lat1) * cos(dist_radians) + cos(lat1) * sin(dist_radians) * cos(tc));
double dlon = atan2(sin(tc) * sin(dist_radians) * cos(lat1), cos(dist_radians) - sin(lat1) * sin(lat));
double lon = ((lon1-dlon + pi) % (2*pi)) - pi;
double[] endPoint = new double[2];
endPoint[0] = lat; endPoint[1] = lon;
return endPoint;
}
这是我用来测试它的函数:
public static void main(String args[]) throws java.io.IOException, java.io.FileNotFoundException {
double distNorth = distance(0.0, 0.0, 72.0, 0.0);
double distEast = distance(72.0, 0.0, 72.0, 31.5);
double lat1 = endOfCourse(0.0, 0.0, 0.0, distNorth)[0];
double lon1 = endOfCourse(lat1, 0.0, 90.0, distEast)[1];
System.out.println("end at: " + lat1 + " / " + lon1);
return;
}
“结束于”值应为 appx。 72.0 / 31.5。但相反,我得到大约 1.25 / 0.021。
我想我一定是遗漏了一些愚蠢的东西,忘记在某处转换单位,或者其他什么...任何帮助将不胜感激!
更新 1:
我已经(正确地)写了距离函数来返回米,但是错误地在 cmets 中写了公里......当我今天回到它时,这当然让我感到困惑。无论如何,现在已经解决了,并且我已经修复了 endOfCourse 方法中的因式分解错误,而且我还意识到我也忘记了在该方法中将弧度转换回度数。无论如何:虽然看起来我现在得到了正确的纬度数(71.99...),但经度数却相差甚远(我得到的是 3.54 而不是 11.5)。
更新 2: 我在测试中有一个错字,如下所述。它现在已在代码中修复。然而,经度数仍然是错误的:我现在得到的是 -11.34 而不是 11.5。我认为这些行一定有问题:
double dlon = atan2(sin(tc) * sin(dist_radians) * cos(lat1), cos(dist_radians) - sin(lat1) * sin(lat));
double lon = ((lon1-dlon + pi) % (2*pi)) - pi;
【问题讨论】:
-
作为参考资料,您可能需要查看movable-type.co.uk/scripts/latlong-vincenty.html 和movable-type.co.uk/scripts/latlong-vincenty-direct.html(包括javascript 实现。
-
在你的第一个函数中,最好使用两个变量,'double angle = ...1st expression; double dist = ...第二个表达式修改为参考角度;'。事实上,第一个值根本不是距离。只是迂腐的大惊小怪。
标签: geolocation geospatial distance geography