开始/停止坐标之间的距离(以英里为单位)

问题描述 投票:0回答:1

我正在尝试计算大型数据集中多次行程的

Lat/Long coordinates
之间的距离。

我一直在尝试使用

Haversine formula
,但不确定我的数据集格式是否正确以进行计算:

enter image description here

我正在尝试按行程 # 查找

Start
Stop
坐标之间的距离。

任何建议都会有很大帮助!

我使用了与下面类似的方法,它可以计算里程:

loc1 = np.array([40.099993, -83.166000])
loc2 = np.array([40.148652, -82.903962])

但是这个Dataset有几十万个坐标,我还没有找到计算整个Dataframe的方法。

python haversine
1个回答
0
投票

可以直接通过DataFrame的相关列在numpy中进行向量化计算。

以我自己的坐标为例:

locations = [[29.737100294779246, -95.34830543711836],
             [29.79372533795864, -95.40306541447599],
             [29.740230408813613, -95.30487511024852],
             [29.7791251002745, -95.29577705758408]]

locations = np.array(locations)

df = pd.DataFrame({'LAT1': locations[::2, 0], 'LNG1': locations[::2, 1], 'LAT2': locations[1::2, 0], 'LNG2': locations[1::2, 1]})

lat1 = df['LAT1'] / 180 * np.pi
lat2 = df['LAT2'] / 180 * np.pi
lng1 = df['LNG1'] / 180 * np.pi
lng2 = df['LNG2'] / 180 * np.pi

haversine = 1 - np.cos(lat2 - lat1) + np.cos(lat1)*np.cos(lat2)*(1 - np.cos(lng2 - lng1))
haversine /= 2
haversine = np.sqrt(haversine)
haversine = np.arcsin(haversine)
haversine *= 2 * 3958.8

df['Haversine_distance'] = haversine
© www.soinside.com 2019 - 2024. All rights reserved.