prepared.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Support for GEOS prepared geometry operations."""
  2. from pickle import PicklingError
  3. import shapely
  4. class PreparedGeometry:
  5. """A geometry prepared for efficient comparison to a set of other geometries.
  6. Examples
  7. --------
  8. >>> from shapely.prepared import prep
  9. >>> from shapely.geometry import Point, Polygon
  10. >>> triangle = Polygon([(0.0, 0.0), (1.0, 1.0), (1.0, -1.0)])
  11. >>> p = prep(triangle)
  12. >>> p.intersects(Point(0.5, 0.5))
  13. True
  14. """
  15. def __init__(self, context):
  16. """Prepare a geometry for efficient comparison to other geometries."""
  17. if isinstance(context, PreparedGeometry):
  18. self.context = context.context
  19. else:
  20. shapely.prepare(context)
  21. self.context = context
  22. self.prepared = True
  23. def contains(self, other):
  24. """Return True if the geometry contains the other, else False."""
  25. return self.context.contains(other)
  26. def contains_properly(self, other):
  27. """Return True if the geometry properly contains the other, else False."""
  28. return self.context.contains_properly(other)
  29. def covers(self, other):
  30. """Return True if the geometry covers the other, else False."""
  31. return self.context.covers(other)
  32. def crosses(self, other):
  33. """Return True if the geometries cross, else False."""
  34. return self.context.crosses(other)
  35. def disjoint(self, other):
  36. """Return True if geometries are disjoint, else False."""
  37. return self.context.disjoint(other)
  38. def intersects(self, other):
  39. """Return True if geometries intersect, else False."""
  40. return self.context.intersects(other)
  41. def overlaps(self, other):
  42. """Return True if geometries overlap, else False."""
  43. return self.context.overlaps(other)
  44. def touches(self, other):
  45. """Return True if geometries touch, else False."""
  46. return self.context.touches(other)
  47. def within(self, other):
  48. """Return True if geometry is within the other, else False."""
  49. return self.context.within(other)
  50. def __reduce__(self):
  51. """Pickling is not supported."""
  52. raise PicklingError("Prepared geometries cannot be pickled.")
  53. def prep(ob):
  54. """Create and return a prepared geometric object."""
  55. return PreparedGeometry(ob)