You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prepared.py 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from .base import GEOSBase
  2. from .error import GEOSException
  3. from .libgeos import geos_version_info
  4. from .prototypes import prepared as capi
  5. class PreparedGeometry(GEOSBase):
  6. """
  7. A geometry that is prepared for performing certain operations.
  8. At the moment this includes the contains covers, and intersects
  9. operations.
  10. """
  11. ptr_type = capi.PREPGEOM_PTR
  12. def __init__(self, geom):
  13. # Keeping a reference to the original geometry object to prevent it
  14. # from being garbage collected which could then crash the prepared one
  15. # See #21662
  16. self._base_geom = geom
  17. from .geometry import GEOSGeometry
  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 and capi:
  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)