我在这里寻找答案,但是o找不到工作。
所以我有带有坐标的数据框:
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
我想计算每两行之间的距离,并将输出存储在新列“距离”中。 因此,第一个值应为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
答案 0 :(得分:0)
以下是使用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
答案 1 :(得分:-1)
从GPS坐标计算以米为单位的距离不起作用。 GPS“线”之间的Zhr距离不是线性的。 要获得两点之间的距离,您必须先将坐标转换为例如UTM,然后像在普通x / y坐标系中一样计算距离