【问题标题】:Finding distance between two gps points in Python在Python中查找两个gps点之间的距离
【发布时间】:2016-08-23 00:17:18
【问题描述】:

我有下面的方法(haversine),它返回两个 gps 点之间的距离。下表是我的数据框。

当我在数据框上应用该函数时,我收到错误“无法将系列转换为”。不确定我是否遗漏了什么。任何帮助将不胜感激。

 distdf1['distance'] = distdf1.apply(lambda x: haversine(distdf1['SLongitude'], distdf1['SLatitude'], distdf1['ClosestLong'], distdf1['ClosestLat']), axis=1) 

数据框:

SLongitude  SLatitude   ClosestLong ClosestLat
0   -100.248093 25.756313   -98.220240  26.189491
1   -77.441536  38.991512   -77.481600  38.748722
2   -72.376370  40.898690   -73.662870  41.025640

方法:

def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points 
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians 
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula 
dlon = lon2 - lon1 
dlat = lat2 - lat1 
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a)) 
km = 6367 * c
return km

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    试试:

    distdf1.apply(lambda x: haversine(x['SLongitude'], x['SLatitude'], x['ClosestLong'], x['ClosestLat']), axis=1) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 2022-08-14
      • 2023-01-20
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多