我试着实现这个公式:http://andrew.hedges.name/experiments/haversine/ aplet对我正在测试的两点有好处:
但我的代码不起作用。
from math import sin, cos, sqrt, atan2
R = 6373.0
lat1 = 52.2296756
lon1 = 21.0122287
lat2 = 52.406374
lon2 = 16.9251681
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))**2 + cos(lat1) * cos(lat2) * (sin(dlon/2))**2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c
print "Result", distance
print "Should be", 278.546
它返回的距离是5447.05546147。为什么?
对于那些通过搜索引擎来这里寻找解决方案的人(比如我),我建议安装^{} 。通过
pip install mpu --user
安装它,并像这样使用它来获得haversine distance:另一个包是^{} 。
如果不需要依赖项,可以使用:
编辑:作为一个注释,如果您只需要一个快速简便的方法来找到两点之间的距离,我强烈建议您使用下面Kurt's answer中描述的方法,而不是重新实现Haversine——请参阅他的文章以了解其基本原理。
这个答案集中在回答操作遇到的特定错误上。
这是因为在Python中,所有的trig函数都是use radians,而不是度。
您可以手动将数字转换为弧度,也可以使用数学模块中的^{} 函数:
距离现在返回正确值
278.545589351
km。更新:2018年4月:请注意,由于GeoPy版本1.13,Vincenty distance已被弃用-您应该改用GeoPy.distance.distance()!
上面的答案是基于Haversine formula,它假设地球是一个球体,结果误差高达0.5%(根据
help(geopy.distance)
)。Vincenty distance使用更精确的椭球模型,如WGS-84,并在geopy中实现。例如将使用默认椭球WGS-84打印
279.352901604
公里的距离。(您也可以选择.miles
或其他几个distance units之一)。相关问题 更多 >
编程相关推荐