gpt4 book ai didi

python - 根据纬度/经度获取两点之间的距离

转载 作者:IT老高 更新时间:2023-10-28 21:06:21 28 4
gpt4 key购买 nike

我尝试实现这个公式:http://andrew.hedges.name/experiments/haversine/aplet 对我正在测试的两点有好处:

enter image description here

但我的代码不起作用。

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。为什么?

最佳答案

更新:04/2018:文森蒂距离为 deprecated since GeoPy version 1.13 - you should use geopy.distance.distance()而是!


以上答案基于Haversine formula ,假设地球是一个球体,这会导致高达约 0.5% 的错误(根据 help(geopy.distance))。 Vincenty distance使用更精确的椭球模型,例如 WGS-84 , 并在 geopy 中实现.例如,

import geopy.distance

coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)

print geopy.distance.geodesic(coords_1, coords_2).km

将使用默认椭球 WGS-84 打印 279.352901604 公里的距离。 (您也可以选择 .miles 或其他几个距离单位之一。

关于python - 根据纬度/经度获取两点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19412462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com