Быстрая аппроксимация Haversine (Python / Pandas)
Каждая строка в кадре данных Pandas содержит координаты широты / долготы в 2 точки. Используя приведенный ниже код Python, вычисление расстояний между этими двумя точками для многих (миллионов) строк занимает очень много времени!
Учитывая, что эти 2 точки находятся на расстоянии менее 50 миль друг от друга и точность не очень важна, возможно ли ускорить расчет?
from math import radians, cos, sin, asin, sqrt
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
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
for index, row in df.iterrows():
df.loc[index, 'distance'] = haversine(row['a_longitude'], row['a_latitude'], row['b_longitude'], row['b_latitude'])