Geopandas中最接近点的多边形

时间:2020-07-18 14:10:01

标签: geopandas

我有一个点,我想找出哪个多边形最接近该点。 我有点和多边形的地理数据。 click for the image

1 个答案:

答案 0 :(得分:1)

您可以使用distance查找到每个多边形的距离,并对它们进行排序以获取最接近的距离。

示例

>>> from shapely.geometry import Point, Polygon
>>> import geopandas as gpd
>>> d = {'geometry': [Polygon([(0, 0), (1, 1), (1, 0)]), Polygon([(3, 3), (4, 3), (4, 4)])]}
>>> gdf = gpd.GeoDataFrame(d)
>>> red_point = Point(1,2)
>>> polygon_index = gdf.distance(red_point).sort_values().index[0]
>>> gdf.loc[polygon_index]
geometry    POLYGON ((0.00000 0.00000, 1.00000 1.00000, 1....
Name: 0, dtype: geometry

注意:请记住设置GeoDataFrame的CRS。