polylabel.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Provides functions for finding the pole of inaccessibility for a given polygon."""
  2. from shapely._geometry import get_point
  3. from shapely.constructive import maximum_inscribed_circle
  4. def polylabel(polygon, tolerance=1.0):
  5. """Find pole of inaccessibility for a given polygon.
  6. Based on Vladimir Agafonkin's https://github.com/mapbox/polylabel
  7. Parameters
  8. ----------
  9. polygon : shapely.geometry.Polygon
  10. Polygon for which to find the pole of inaccessibility.
  11. tolerance : int or float, optional
  12. `tolerance` represents the highest resolution in units of the
  13. input geometry that will be considered for a solution. (default
  14. value is 1.0).
  15. Returns
  16. -------
  17. shapely.geometry.Point
  18. A point representing the pole of inaccessibility for the given input
  19. polygon.
  20. Raises
  21. ------
  22. shapely.errors.TopologicalError
  23. If the input polygon is not a valid geometry.
  24. Examples
  25. --------
  26. >>> from shapely.ops import polylabel
  27. >>> from shapely import LineString
  28. >>> polygon = LineString([(0, 0), (50, 200), (100, 100), (20, 50),
  29. ... (-100, -20), (-150, -200)]).buffer(100)
  30. >>> polylabel(polygon, tolerance=0.001)
  31. <POINT (59.733 111.33)>
  32. """
  33. line = maximum_inscribed_circle(polygon, tolerance)
  34. return get_point(line, 0)