0

Is it possible to find the longeststraight distance between a Point (latitude and longitude) and a Polygon in Shapely? I read it's possible to find the closest path, but i'm not sure about the longest.

Mike T
  • 41,085
  • 18
  • 152
  • 203
Alberto Bonsanto
  • 17,556
  • 10
  • 64
  • 93

1 Answers1

3

Try the Hausdorff distance, which is returned by the g1.hausdorff_distance(g2) fuction:

from shapely.geometry import Polygon, Point
poly = Polygon([(-1, -1), (-2, 2), (4, 4), (4, -1), (-1, -1)])
p = Point(0, 0)
poly.hausdorff_distance(p)  # 5.656854249492381

Keep in mind, Shapely only works in Cartesian space. Your question asks about "latitude and longitude", so these distance units are in degrees. You'd need to project this into a suitable coordinate reference system (CRS) to get more conventional units of length. Furthermore, the definition of "straight path" changes depending on the choice of CRS.

Mike T
  • 41,085
  • 18
  • 152
  • 203
  • Is there any way to achieve with CRS in Python? – Alberto Bonsanto Sep 25 '17 at 21:07
  • You can project your geometries with [pyproj](https://github.com/jswhit/pyproj) ([this way](https://gis.stackexchange.com/q/127427/1872)), and find a good projection from [this list of map projections](https://en.wikipedia.org/wiki/List_of_map_projections) – Mike T Sep 25 '17 at 22:38
  • A different strategy, that doesn't involve choosing a projection, would be to find the inverse geodesic distance `s12` from each pair of lat/lon points with [geographiclib](https://geographiclib.sourceforge.io/html/python/). – Mike T Sep 25 '17 at 22:42