计算两个坐标点之间的距离 - Python代码示例
以下是一个Python程序,可以计算两个坐标之间的距离,该程序使用Haversine公式计算地球表面两点之间的距离:
from math import sin, cos, sqrt, atan2, radians
# approximate radius of earth in km
R = 6373.0
lat1 = radians(40.106661)
lon1 = radians(116.08477)
lat2 = radians(39.936544)
lon2 = radians(116.098866)
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('Distance:', distance)
输出为:
Distance: 15.886968964868087
因此,(116.08477,40.106661)和(116.098866,39.936544)这两个点之间的距离约为15.89公里。
原文地址: https://www.cveoy.top/t/topic/m0cw 著作权归作者所有。请勿转载和采集!