为 python 中的熊猫 DataFrame 的连续点计算 Harvesine 和初始方位

Calculate Harvesine and initial bearing for successive points of a panda DataFrame in python

虽然大多数都是基于点而不是数据框,但有很多包都提供了这种计算,或者我可能犯了一个错误! 我发现这种方法适用于我的纬度和经度列的熊猫数据框:

def haversine(lat1, lon1, lat2, lon2, to_radians=True, earth_radius=6378137):
   """
   slightly modified version: of 

   Calculate the great circle distance between two points
   on the earth (specified in decimal degrees or in radians)

   All (lat, lon) coordinates must have numeric dtypes and be of equal length.
   """
   if to_radians:
       lat1, lon1, lat2, lon2 = map(np.radians, [lat1, lon1, lat2, lon2])
       a = np.sin((lat2-lat1)/2.0)**2 + \
           np.cos(lat1) * np.cos(lat2) * np.sin((lon2-lon1)/2.0)**2
   return earth_radius * 2 * np.arcsin(np.sqrt(a))

但是我尝试过的所有初始方位角或方位角都不接受数据帧系列并且尝试使用 numpy 数组仍然会 return 我归零! 对于数据帧的连续行,是否有某种方法可以做到这一点?我想计算连续点之间的初始方位。在 R 中,轴承函数将使用数据框完成工作,只是想知道 Python.

中是否有等效项

更新: 我发现了问题。我使用 R 方法能够找到连续行之间的轴承,所以我基本上删除了第一行和最后一行,制作了两组具有两列的数据帧,但它与 shift() 完美配合,我编写了自己的轴承函数这比使用那里的那个更容易...... 所以我从我的主要数据框 pts 制作了下面的两个数据框: latlon_a = 分 latlon_b = pts.shift() 和我自己的初始轴承功能:

def initial_bearing(lon1, lat1, lon2, lat2):
   """
   My own version based on R source

   Calculate the initial bearing between two points

   All (latitude, longitude) coordinates must have numeric dtypes and be of equal length.
   """
   lat1, lon1, lat2, lon2 = map(np.radians, [lon1, lat1, lon2, lat2])
   delta1 = lon1-lon2
   term1 = np.sin(delta1) * np.cos(lat2)
   term2 = np.cos(lat1) * np.sin(lat2)
   term3 = np.sin(lat1) * np.cos(lat2) * np.cos(delta1)
   rad = np.arctan2(term1, (term2-term3))
   bearing = np.rad2deg(rad)
   return (bearing + 360) % 360


bearing = initial_bearing(latlon_a['longitude'],latlon_a['latitude'],
                          latlon_b['longitude'],latlon_b['latitude'])

这对我来说非常有效,并且恢复了初始状态。对于 funial bearing,您只需将下面的行替换或添加到 return: return (方位角 + 180) % 360