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

如何解决在python中计算熊猫DataFrame连续点的Harvesine和初始方位

提供这种计算的包太多了,尽管其中大多数是基于点而不是数据框,或者我可能犯了一个错误! 我发现这种方法可以使用我的纬度和经度列的熊猫数据框:

def haversine(lat1,lon1,lat2,lon2,to_radians=True,earth_radius=6378137):
   """
   slightly modified version: of http://stackoverflow.com/a/29546836/2901002

   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,lon2 = map(np.radians,[lat1,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 数组仍然会返回零! 对于数据帧的连续行,是否有某种方法可以这样做?我想计算连续点之间的初始方位。在 R 中,轴承函数将使用数据框完成这项工作,只是想知道 Python 中是否有等价物。

解决方法

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

这对我来说非常有效,并且恢复了最初的方向。对于轴承,您只需替换或添加以下行即可返回: 回报(轴承 + 180)% 360

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?