[GIS] Calculating distance from polygon to point

distancegeopandaspolygonpython

t1 = Polygon([(4.338074,50.848677), (4.344961,50.833264), (4.366227,50.840809), (4.367945,50.852455), (4.346693,50.858306)])
t = geopandas.GeoSeries(t1)
t.crs = {'init':'espg:4326'}

t2 = geopandas.GeoSeries([Point(4.382617,50.811948)])
t2.crs = {'init':'espg:4326'}

How can I calculate the distance of a point from the polygon respectively find the closest point between the polygon and the point?

Best Answer

Geopandas GeoSeries has a method distance which uses Shapely to calculate distances:

from shapely.geometry import Polygon, Point
import geopandas

t1 = Polygon([(4.338074,50.848677), (4.344961,50.833264), (4.366227,50.840809), (4.367945,50.852455), (4.346693,50.858306)])
t = geopandas.GeoSeries(t1)
t.crs = 4326

t2 = geopandas.GeoSeries([Point(4.382617,50.811948)])
t2.crs = 4326

dist = t.distance(t2)
print(dist)

Please notice that shapely assumes coordinates on a cartesian plane. Therefore the Geopandas/Shapely distance function is not useful when using geographic coordinates. You should transform your data to a projected coordinate system before calculating distances using Geopandas/Shapely.

Related Question