简体   繁体   English

如何找到连续的lat,lon点距离

[英]How to find consecutive lat, lon point distances

I have a df with a series of consecutive geo coordinates. 我有一个带有一系列连续地理坐标的df。 I want to find the distance between these consecutive points. 我想找到这些连续点之间的距离。 1->2, 2->3 .... end-1->end. 1-> 2,2-> 3 .... end-1-> end。

Using df.shift(1) doesn't look pretty, using a loop either. 使用df.shift(1)看起来并不漂亮,也使用循环。

Can it be done more elegantly with some recursive functions? 使用一些递归函数可以更优雅地完成吗?

The solution 解决方案

import pandas as pd

def calculate_distance(lat_from, long_from, lat_to, long_to):
    # some better logic
    return lat_from - lat_to + long_from - long_to

df = pd.DataFrame({'long': [1, 2, 4.2, 5, 6], 'lat': [7, 4, 2, 1.2, 2]})
df[['lat_to', 'long_to']] = df.shift(-1)

# this is way faster, but may not be possible depending on your calculation
calculate_distance(df['lat'], df['long'], df['lat_to'], df['long_to'])
>>> 0    2.000000e+00
>>> 1   -2.000000e-01
>>> 2    2.220446e-16
>>> 3   -1.800000e+00
>>> 4             NaN
>>> dtype: float64

# or
# a lot slower, processes on per-row basis
df.apply(lambda row: calculate_distance(row['lat'], row['long'], row['lat_to'], row['long_to']), axis=1)
>>> 0    2.000000e+00
>>> 1   -2.000000e-01
>>> 2    2.220446e-16
>>> 3   -1.800000e+00
>>> 4             NaN
>>> dtype: float64

For speed comparison, try the difference between pandas.DataFrame.apply , pandas.DataFrame.applymap and normal broadcasted operations. 要进行速度比较,请尝试pandas.DataFrame.applypandas.DataFrame.applymap和普通广播操作之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用距离小于一英里的纬度/经度在一条线上查找一个点 - Finding a Point On a Line Using Lat/Lon with Distances Less Than a Mile Apart 如何对纬度/经度进行分类以找到最近的城市 - how to categorize lat/lon to find nearest city 经纬度 csv 文件的距离矩阵 - Matrix of distances from csv file of lat and lon 使用python查找网格上的纬度点的索引 - find indices of lat lon point on a grid using python 生成纬度和经度位置之间距离矩阵的最快方法是什么? - What is the fastest way to generate a matrix for distances between location with lat and lon? 将TOAST地图上的点转换为球面上的经/纬度 - Converting a point on a TOAST map to the lat/lon on a sphere 将Lat&Lon点移动到新的可路由位置 - Move Lat & Lon point to a new routable position 检查纬度/经度是否在陆地或海洋上 - Check if Lat/Lon point is over land or ocean lat/lon to utm to lat/lon 非常有缺陷,怎么会? - lat/lon to utm to lat/lon is extremely flawed, how come? 如何在meteostat的函数Point(lat=float,lon=float)中使用填充有浮点数的列? Python - How to use a column filled with floats in the function Point(lat=float,lon=float) of meteostat? Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM