python计算经纬度之间距离
时间: 2024-01-04 15:19:03 浏览: 110
以下是两种Python计算经纬度之间距离的方法:
1. 使用haversine公式计算经纬度之间的距离:
```python
import math
def haversine(lon1, lat1, lon2, lat2):
# 将经纬度转换为弧度
lon1 = math.radians(lon1)
lat1 = math.radians(lat1)
lon2 = math.radians(lon2)
lat2 = math.radians(lat2)
# haversine公式
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
distance = 6371 * c * 1000 # 结果以米为单位
return distance
# 示例使用
lon1 = 102.8083989
lat1 = 24.8962294
lon2 = 102.471938
lat2 = 24.912491
distance = haversine(lon1, lat1, lon2, lat2)
print("距离:", distance, "米")
```
2. 使用scipy库中的cdist函数计算经纬度之间的距离:
```python
from scipy.spatial.distance import cdist
lonlat1 = [[102.8083989, 24.8962294]] # 经度、纬度
lonlat2 = [[102.471938, 24.912491]] # 经度、纬度
distance = cdist(lonlat1, lonlat2, metric='euclidean')
distance = list(list(list(distance))[0] * 100000)[0] # 结果以米为单位
print("距离:", distance, "米")
```
阅读全文