algorithm - 计算给定方位和距离的坐标

标签 algorithm gps coordinates bearing

我在实现此处描述的功能时遇到问题 here .

这是我的 Java 实现:

private static double[] pointRadialDistance(double lat1, double lon1, 
        double radianBearing, double radialDistance) {
     double lat = Math.asin(Math.sin(lat1)*Math.cos(radialDistance)+Math.cos(lat1)
             *Math.sin(radialDistance)*Math.cos(radianBearing));
     double lon;
     if(Math.cos(lat) == 0) {  // Endpoint a pole
        lon=lon1;      
     }
     else {
        lon = ((lon1-Math.asin(Math.sin(radianBearing)*Math.sin(radialDistance)/Math.cos(lat))
                +Math.PI) % (2*Math.PI)) - Math.PI;
     }
    return (new double[]{lat, lon});
}

在调用函数之前,我将度方位角转换为弧度并将距离 (km) 转换为弧度距离 - 所以这不是问题所在。

但是,当我输入如下坐标时: 经纬度 = 49.25705; lon = -123.140259; 方位225(西南),距离1km

我得到这个返回: 纬度:-1.0085434360125864 经度:-3.7595299668539504

这显然不正确,谁能看出我做错了什么?

谢谢

最佳答案

看起来这些是您的代码中的问题:

  1. 您需要转换lat1lon1在调用您的函数之前转换为弧度。
  2. 您可能正在缩放 radialDistance不正确。
  3. 测试 float 是否相等是很危险的。在精确算术之后相等的两个数字在浮点算术之后可能不完全相等。因此 abs(x-y) < thresholdx == y 更安全用于测试两个 float xy为了平等。
  4. 我想你想转换 latlon从弧度到度数。

这是我用 Python 实现的代码:

#!/usr/bin/env python

from math import asin,cos,pi,sin

rEarth = 6371.01 # Earth's average radius in km
epsilon = 0.000001 # threshold for floating-point equality


def deg2rad(angle):
    return angle*pi/180


def rad2deg(angle):
    return angle*180/pi


def pointRadialDistance(lat1, lon1, bearing, distance):
    """
    Return final coordinates (lat2,lon2) [in degrees] given initial coordinates
    (lat1,lon1) [in degrees] and a bearing [in degrees] and distance [in km]
    """
    rlat1 = deg2rad(lat1)
    rlon1 = deg2rad(lon1)
    rbearing = deg2rad(bearing)
    rdistance = distance / rEarth # normalize linear distance to radian angle

    rlat = asin( sin(rlat1) * cos(rdistance) + cos(rlat1) * sin(rdistance) * cos(rbearing) )

    if cos(rlat) == 0 or abs(cos(rlat)) < epsilon: # Endpoint a pole
        rlon=rlon1
    else:
        rlon = ( (rlon1 - asin( sin(rbearing)* sin(rdistance) / cos(rlat) ) + pi ) % (2*pi) ) - pi

    lat = rad2deg(rlat)
    lon = rad2deg(rlon)
    return (lat, lon)


def main():
    print "lat1 \t lon1 \t\t bear \t dist \t\t lat2 \t\t lon2"
    testcases = []
    testcases.append((0,0,0,1))
    testcases.append((0,0,90,1))
    testcases.append((0,0,0,100))
    testcases.append((0,0,90,100))
    testcases.append((49.25705,-123.140259,225,1))
    testcases.append((49.25705,-123.140259,225,100))
    testcases.append((49.25705,-123.140259,225,1000))
    for lat1, lon1, bear, dist in testcases:
        (lat,lon) = pointRadialDistance(lat1,lon1,bear,dist)
        print "%6.2f \t %6.2f \t %4.1f \t %6.1f \t %6.2f \t %6.2f" % (lat1,lon1,bear,dist,lat,lon)


if __name__ == "__main__":
    main()

这是输出:

lat1     lon1        bear    dist        lat2        lon2
  0.00     0.00       0.0       1.0        0.01        0.00
  0.00     0.00      90.0       1.0        0.00       -0.01
  0.00     0.00       0.0     100.0        0.90        0.00
  0.00     0.00      90.0     100.0        0.00       -0.90
 49.26   -123.14     225.0      1.0       49.25      -123.13
 49.26   -123.14     225.0    100.0       48.62      -122.18
 49.26   -123.14     225.0   1000.0       42.55      -114.51

关于algorithm - 计算给定方位和距离的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/877524/

相关文章:

algorithm - Mahout 基于内容的推荐引擎

java - 一段代码如何影响算法?

canvas - 为什么要从左上方测量图形坐标?

iphone - 如何将 View 从容器 View 内部移动到另一个容器 View 内部?

c# - 如何搜索坐标列表并根据其与当前位置之间的距离进行显示? (Windows 手机 7)

algorithm - 具有依赖边界行程计数的嵌套循环

python - 未排序列表与线性和二进制搜索

java - GPS、传感器和唤醒

android - 通过js在WebView中获取位置

android - GPS 位置如何可信?