10

Let's say we have the following point and polygon:

poly = Polygon([(0, 0), (2,8), (14, 10), (6,1)])
point = Point(4,4)

Because point lies within poly, the distance from the first to the latter will be zero (poly.distance(point) will return 0.0).

Is there a way to instead calculate the distance to the nearest edge?

Note: Effectively the same question as this, but desire a solution in Python, ideally with Shapely.

16

Compare the point to the polygon's exterior ring:

poly.exterior.distance(point)
| improve this answer | |
  • I'm working on a similar problem. What imports are needed to get Polygon and Point? – brad Sep 4 '19 at 14:20
  • 3
    @brad from shapely.geometry import Point, Polygon – Amstell Sep 9 '19 at 21:40
  • but this only works after we convert the coordinates to cartesian coordinates, isn't it? It won't work on Point(lat, lon) – Ciprian Tomoiagă May 5 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.