prepared.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from .base import GEOSBase
  2. from .error import GEOSException
  3. from .geometry import GEOSGeometry
  4. from .libgeos import geos_version_info
  5. from .prototypes import prepared as capi
  6. class PreparedGeometry(GEOSBase):
  7. """
  8. A geometry that is prepared for performing certain operations.
  9. At the moment this includes the contains covers, and intersects
  10. operations.
  11. """
  12. ptr_type = capi.PREPGEOM_PTR
  13. def __init__(self, geom):
  14. # Keeping a reference to the original geometry object to prevent it
  15. # from being garbage collected which could then crash the prepared one
  16. # See #21662
  17. self._base_geom = geom
  18. if not isinstance(geom, GEOSGeometry):
  19. raise TypeError
  20. self.ptr = capi.geos_prepare(geom.ptr)
  21. def __del__(self):
  22. if self._ptr:
  23. capi.prepared_destroy(self._ptr)
  24. def contains(self, other):
  25. return capi.prepared_contains(self.ptr, other.ptr)
  26. def contains_properly(self, other):
  27. return capi.prepared_contains_properly(self.ptr, other.ptr)
  28. def covers(self, other):
  29. return capi.prepared_covers(self.ptr, other.ptr)
  30. def intersects(self, other):
  31. return capi.prepared_intersects(self.ptr, other.ptr)
  32. # Added in GEOS 3.3:
  33. def crosses(self, other):
  34. if geos_version_info()['version'] < '3.3.0':
  35. raise GEOSException("crosses on prepared geometries requires GEOS >= 3.3.0")
  36. return capi.prepared_crosses(self.ptr, other.ptr)
  37. def disjoint(self, other):
  38. if geos_version_info()['version'] < '3.3.0':
  39. raise GEOSException("disjoint on prepared geometries requires GEOS >= 3.3.0")
  40. return capi.prepared_disjoint(self.ptr, other.ptr)
  41. def overlaps(self, other):
  42. if geos_version_info()['version'] < '3.3.0':
  43. raise GEOSException("overlaps on prepared geometries requires GEOS >= 3.3.0")
  44. return capi.prepared_overlaps(self.ptr, other.ptr)
  45. def touches(self, other):
  46. if geos_version_info()['version'] < '3.3.0':
  47. raise GEOSException("touches on prepared geometries requires GEOS >= 3.3.0")
  48. return capi.prepared_touches(self.ptr, other.ptr)
  49. def within(self, other):
  50. if geos_version_info()['version'] < '3.3.0':
  51. raise GEOSException("within on prepared geometries requires GEOS >= 3.3.0")
  52. return capi.prepared_within(self.ptr, other.ptr)