当前位置:   article > 正文

python输入两个坐标求距离_计算python中*多组*地理坐标之间的距离

geopy.distance import vincenty

编辑:

here’s a simple notebook example

一般方法,假设您有一个包含点的DataFrame列,并且您想要计算所有这些列之间的距离(例如,如果您有单独的列,则首先将它们组合成(lon,lat)元组).命名新列coords.

import pandas as pd

import numpy as np

from geopy.distance import vincenty

# assumes your DataFrame is named df, and its lon and lat columns are named lon and lat. Adjust as needed.

df['coords'] = zip(df.lat, df.lon)

# first, let's create a square DataFrame (think of it as a matrix if you like)

square = pd.DataFrame(

np.zeros(len(df) ** 2).reshape(len(df), len(df)),

index=df.index, columns=df.index)

此函数使用输入列名称从df DataFrame中查找“end”坐标,然后使用square.coords列作为第一个参数,将geopy vincenty()函数应用于输入列中的每一行.这是有效的,因为该功能是从右到左逐列应用的.

def get_distance(col):

end = df.ix[col.name]['coords']

return df['coords'].apply(vincenty, args=(end,), ellipsoid='WGS-84')

现在我们已准备好计算所有距离.

我们正在转置DataFrame(.T),因为我们将用于检索距离的loc []方法是指索引标签,行标签.但是,我们的内部应用函数(见上文)使用检索的值填充列

distances = square.apply(get_distance, axis=1).T

您的地理位置值(IIRC)以公里为单位返回,因此您可能需要将这些值转换为您想要使用的任何单位.meter,.miles等.

像下面这样的东西应该工作:

def units(input_instance):

return input_instance.meters

distances_meters = distances.applymap(units)

您现在可以使用例如索引到您的距离矩阵. loc [row_index,column_index].

你应该能够很容易地适应上述内容.您可能必须在get_distance函数中调整apply调用,以确保将正确的值传递给great_circle.大熊猫apply文档可能很有用,特别是关于使用args传递位置参数(你需要一个最近的pandas版本才能工作).

此代码尚未分析,并且可能有更快的方法,但400k距离计算应该相当快.

哦,还

我不记得geopy是否期望坐标为(lon,lat)或(lat,lon).我打赌这是后者(叹气).

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/821598
推荐阅读
相关标签
  

闽ICP备14008679号