【发布时间】:2020-06-04 15:19:12
【问题描述】:
GeoPandas 在引擎盖下使用匀称。为了获得最近的邻居,我看到了nearest_points from shapely 的使用。但是,这种方法不包括 k-最近点。
我需要计算从到 GeoDataFrames 到最近点的距离,并将距离插入到包含“从该点开始”数据的 GeoDataFrame 中。
这是我使用GeoSeries.distance() 而不使用其他包或库的方法。请注意,当k == 1 时,返回值基本上显示了到最近点的距离。 There is also a GeoPandas-only solution for nearest point by @cd98 which inspired my approach.
这对我的数据很有效,但我想知道是否有更好或更快的方法或其他好处可以使用 shapely 或 sklearn.neighbors?
import pandas as pd
import geopandas as gp
gdf1 > GeoDataFrame with point type geometry column - distance from this point
gdf2 > GeoDataFrame with point type geometry column - distance to this point
def knearest(from_points, to_points, k):
distlist = to_points.distance(from_points)
distlist.sort_values(ascending=True, inplace=True) # To have the closest ones first
return distlist[:k].mean()
# looping through a list of nearest points
for Ks in [1, 2, 3, 4, 5, 10]:
name = 'dist_to_closest_' + str(Ks) # to set column name
gdf1[name] = gdf1.geometry.apply(knearest, args=(gdf2, closest_x))
【问题讨论】:
标签: python pandas distance geopandas