4

这是一个非常简单的案例,但到目前为止我还没有找到任何简单的方法来做到这一点。这个想法是获得 a 中定义的所有点GeoDataFrame与 another 中定义的点之间的一组距离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))

gdf_1这会产生共享相同索引的点之间的元素距离gdf_2(还有一个警告,因为两个 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它是对称的)。

编辑

  • In this post, the solution is given for a couple of points; but I cannot find a straightforward way to combine all points in two datasets.

  • In this post only element-wise operations are proposed.

  • An analogous question was also raised on the GitHub repo of geopandas. One of the proposed solution is to use the apply method, without any detailed answer.

4

1 回答 1

7

You have to apply over each geometry in first gdf to get distance to all geometric in second gdf.

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]))

gdf_1.geometry.apply(lambda g: gdf_2.distance(g))
      0      1
0    0.0   90.0
1   90.0  180.0
2  120.0  210.0
于 2020-11-09T16:40:20.263 回答