如何从gps数据中找到点之间的距离?
Posted
技术标签:
【中文标题】如何从gps数据中找到点之间的距离?【英文标题】:How to find the distance between points from the gps data? 【发布时间】:2019-06-15 11:02:43 【问题描述】:我在这里寻找答案,但没有找到工作。
所以我有坐标数据框:
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 行之间的距离并将输出存储在新列“距离”中。 所以第一个值应该是 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
【问题讨论】:
Getting distance between two points based on latitude/longitude的可能重复 相关:How to process 2 GeoDataFrames by rows pairwise? 【参考方案1】:下面是一个使用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
【讨论】:
【参考方案2】:从 GPS 坐标计算以米为单位的距离不起作用。 GPS“线”之间的Zhr距离不是线性的。 要获得两点之间的距离,您必须首先将坐标转换为 UTM,然后像在正常 x/y 坐标系中那样计算距离
【讨论】:
以上是关于如何从gps数据中找到点之间的距离?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用数据库(mysql)中的gps位置在两个地理点之间找到pople?