Nearest Polygon to A point In Geopandas

Viewed 2587

I have a point and I want to figure out which polygon is nearest to the point. I have geo data of both points and Polygons. click for the image

1 Answers

You can use distance to find the distance to each polygon and sort them to retrieve the nearest.

Example

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

Note: remember to set the CRS of the GeoDataFrame.

Related