这是一个相当简单的案例,但我迄今为止没有找到任何简单的方法来完成它。想法是获取定义在一个GeoDataFrame
中的所有点与另一个GeoDataFrame
中定义的所有点之间的一组距离。
import geopandas as gpd
import pandas as pd
# random coordinates
gdf_1 = gpd.GeoDataFrame(geometry=gpd.points_from_xy([0, 0, 0], [0, 90, 120]))
gdf_2 = gpd.GeoDataFrame(geometry=gpd.points_from_xy([0, 0], [0, -90]))
print(gdf_1)
print(gdf_2)
# distances are calculated elementwise
print(gdf_1.distance(gdf_2))
此功能可以产生两个 GeoDataFrame 中具有相同索引的点之间的逐元素距离(同时还会出现警告,因为这两个 GeoSeries 的索引不相同,这也是我的情况)。
geometry
0 POINT (0.000 0.000)
1 POINT (0.000 90.000)
2 POINT (0.000 120.000)
geometry
0 POINT (0.00000 0.00000)
1 POINT (0.00000 -90.00000)
/home/seydoux/anaconda3/envs/chelyabinsk/lib/python3.8/site-packages/geopandas/base.py:39: UserWarning: The indices of the two GeoSeries are different.
warn("The indices of the two GeoSeries are different.")
0 0.0
1 180.0
2 NaN
问题是:如何获取所有点到点的距离序列(或至少是
gdf_1
和gdf_2
的唯一索引组合,因为它是对称的)。
编辑
在这篇文章中,给出了几个点的解决方案;但我找不到一个简单直接的方法来结合两个数据集中的所有点。
在这篇文章中,只提出了逐元素操作的解决方案。
在GitHub上的geopandas仓库中,也提出了类似的问题。其中一个建议的解决方案是使用应用程序方法,没有详细的答案。
gdf_1['geometry'].distance(gdf_2['geometry'].values[:,None])
。 - wwii