【发布时间】:2022-10-21 04:24:47
【问题描述】:
我尝试计算 4 列之间的半正弦距离
cgi longitude_bts latitude_bts longitude_poi latitude_poi
0 510-11-32111-7131 95.335142 5.565253 95.337588 5.563713
1 510-11-32111-7135 95.335142 5.565253 95.337588 5.563713
这是我的代码
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
import numpy as np
import math
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
# haversine formula
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.asin(math.sqrt(a))
# Radius of earth in kilometers is 6371
km = 6371008.799485213* c
return km
ref_location_airport_hospital['radius'] = ref_location_airport_hospital.apply(lambda x: haversine(x['latitude_bts'], x['longitude_bts'], x['latitude_poi'], x['longitude_poi']), axis=1)
这是结果
cgi longitude_bts latitude_bts longitude_poi latitude_poi radius
0 510-11-32111-7131 95.335142 5.565253 95.337588 5.563713 272.441676
1 510-11-32111-7135 95.335142 5.565253 95.337588 5.563713 272.441676
结果不合理,两点距离小于0.004,所以半径应该小于1公里
笔记: 1经/纬度约为111公里
【问题讨论】:
-
我认为您实际上是在返回米,而不是公里。另外,通常我看到
c = 2 * atan2(sqrt(a), sqrt(1-a)),而不是asin