简体   繁体   English

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

[英]How to find the distance between points from the gps data?

I was looking here for the answer but o didn't found work one. 我在这里寻找答案,但是o找不到工作。

So I have dataframe with coordinates: 所以我有坐标的数据框:

 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

I want to calculate the distance between every 2 rows and store the output in a new column 'distance'. 我想计算每两行之间的距离,并将输出存储在新列“距离”中。 So the first value should be 0 or NaN. 因此,第一个值应为0或NaN。 And next should have the result of the distance between 2 and 1 row. 接下来应该是2到1行之间的距离的结果。

My function to calculate distance: 我计算距离的函数:

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

Here is an example using iterrows 's function: 这是使用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)

Output : 输出:

    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

Calculating the distance in meter from GPS coordinates does not work. 从GPS坐标计算以米为单位的距离不起作用。 Zhr distance between rhe GPS "lines" is not linear. GPS“线”之间的Zhr距离不是线性的。 To get a distance between two points you have to transform the coordinates to for example UTM first, and then calculate the distance like you would do in a normal x/y coordinate System 要获得两点之间的距离,您必须先将坐标转换为例如UTM,然后像在普通x / y坐标系中那样计算距离

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM