validation.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Validate geometries and make them valid."""
  2. # TODO: allow for implementations using other than GEOS
  3. import shapely
  4. __all__ = ["explain_validity", "make_valid"]
  5. def explain_validity(ob):
  6. """Explain the validity of the input geometry, if it is invalid.
  7. This will describe why the geometry is invalid, and might
  8. include a location if there is a self-intersection or a
  9. ring self-intersection.
  10. Parameters
  11. ----------
  12. ob: Geometry
  13. A shapely geometry object
  14. Returns
  15. -------
  16. str
  17. A string describing the reason the geometry is invalid.
  18. """
  19. return shapely.is_valid_reason(ob)
  20. def make_valid(ob):
  21. """Make the input geometry valid according to the GEOS MakeValid algorithm.
  22. If the input geometry is already valid, then it will be returned.
  23. If the geometry must be split into multiple parts of the same type to be
  24. made valid, then a multi-part geometry will be returned.
  25. If the geometry must be split into multiple parts of different types to be
  26. made valid, then a GeometryCollection will be returned.
  27. Parameters
  28. ----------
  29. ob : Geometry
  30. A shapely geometry object which should be made valid. If the object is
  31. already valid, it will be returned as-is.
  32. Returns
  33. -------
  34. Geometry
  35. The input geometry, made valid according to the GEOS MakeValid algorithm.
  36. """
  37. if ob.is_valid:
  38. return ob
  39. return shapely.make_valid(ob)