如何从gps数据中找到点之间的距离?

How to find the distance between points from the gps data?

我在这里寻找答案,但没有找到工作。

所以我有带坐标的数据框:

 datetime           lon_deg          lat_deg
    26.01.2018 17:59    15.9511889  48.33841795
    26.01.2018 18:00    15.95111795 48.33848978
    26.01.2018 18:00    15.95091144 48.33857379
    26.01.2018 18:01    15.95061589 48.33869731
    26.01.2018 18:01    15.950249   48.33878743
    26.01.2018 18:02    15.94972038 48.338807
    26.01.2018 18:02    15.94903085 48.33886638
    26.01.2018 18:03    15.9481836  48.3389207
    26.01.2018 18:03    15.94722731 48.3389714
    26.01.2018 18:04    15.94619468 48.33904541

我想计算每 2 行之间的距离并将输出存储在新列中 'distance'。 所以第一个值应该是 0 或 NaN。 接下来应该有 2 和 1 行之间的距离的结果。

我计算距离的函数:

def haversine(lon1,lat1,lon2,lat2):

    # haversine formula 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 


    # Radius of earth in kilometers is 6371,21
    km = 6371 * c 
    return km

从 GPS 坐标计算以米为单位的距离不会 work.TheGPS 坐标之间的距离不是线性的。 要获得两点之间的距离,您必须首先将坐标转换为 UTM,然后像在正常 x/y 坐标系

中一样计算距离

这里是一个使用iterrows函数的例子:

import pandas as pd
from math import radians, cos, sin, asin, sqrt 

def haversine(lon1,lat1,lon2,lat2):

    # haversine formula 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 


    # Radius of earth in kilometers is 6371,21
    km = 6371 * c 
    return km

data = { 'datetime': {0: '26.01.2018 17:59', 1: '26.01.2018 18:00', 2: '26.01.2018 18:00',   3: '26.01.2018 18:01', 4: '26.01.2018 18:01', 5: '26.01.2018 18:02', 6: '26.01.2018 18:02', 7: '26.01.2018 18:03', 8: '26.01.2018 18:03', 9: '26.01.2018 18:04'},
 'lon_deg': {0: 15.9511889, 1: 15.95111795, 2: 15.95091144, 3: 15.95061589, 4: 15.950249, 5: 15.94972038, 6: 15.94903085, 7: 15.948183600000002, 8: 15.94722731, 9: 15.94619468},
 'lat_deg': {0: 48.33841795, 1: 48.33848978, 2: 48.33857379, 3: 48.33869731, 4: 48.33878743, 5: 48.338807, 6: 48.33886638, 7: 48.3389207, 8: 48.3389714, 9: 48.33904541}}

df = pd.DataFrame(data)

#Add empty distance col
df['distance'] = None

#Itering rows
for idx, row in  df.iterrows():

    if idx == df.index.max(): break

    lat1 = df.iloc[idx,-2]
    lat2 = df.iloc[idx+1,-2]

    lon1 = df.iloc[idx,-3]
    lon2 = df.iloc[idx+1,-3]

    df.iloc[idx+1,-1] = haversine(lon1,lat1,lon2,lat2)

输出:

    datetime            lon_deg     lat_deg     distance
0   26.01.2018 17:59    15.951189   48.338418   None
1   26.01.2018 18:00    15.951118   48.338490   0.00955491
2   26.01.2018 18:00    15.950911   48.338574   0.0178957
3   26.01.2018 18:01    15.950616   48.338697   0.0258043
4   26.01.2018 18:01    15.950249   48.338787   0.0289106
5   26.01.2018 18:02    15.949720   48.338807   0.039133
6   26.01.2018 18:02    15.949031   48.338866   0.0513918
7   26.01.2018 18:03    15.948184   48.338921   0.0629141
8   26.01.2018 18:03    15.947227   48.338971   0.0709075
9   26.01.2018 18:04    15.946195   48.339045   0.0767679