__init__.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Provides multi-point element-wise operations such as ``contains``."""
  2. import warnings
  3. import numpy as np
  4. import shapely
  5. from shapely.prepared import PreparedGeometry
  6. def _construct_points(x, y):
  7. x, y = np.asanyarray(x), np.asanyarray(y)
  8. if x.shape != y.shape:
  9. raise ValueError("X and Y shapes must be equivalent.")
  10. if x.dtype != np.float64:
  11. x = x.astype(np.float64)
  12. if y.dtype != np.float64:
  13. y = y.astype(np.float64)
  14. return shapely.points(x, y)
  15. def contains(geometry, x, y):
  16. """Check whether multiple points are contained by a single geometry.
  17. Vectorized (element-wise) version of `contains`.
  18. Parameters
  19. ----------
  20. geometry : PreparedGeometry or subclass of BaseGeometry
  21. The geometry which is to be checked to see whether each point is
  22. contained within. The geometry will be "prepared" if it is not already
  23. a PreparedGeometry instance.
  24. x : array
  25. The x coordinates of the points to check.
  26. y : array
  27. The y coordinates of the points to check.
  28. Returns
  29. -------
  30. Mask of points contained by the given `geometry`.
  31. """
  32. warnings.warn(
  33. "The 'shapely.vectorized.contains' function is deprecated and will be "
  34. "removed a future version. Use 'shapely.contains_xy' instead (available "
  35. "since shapely 2.0.0).",
  36. DeprecationWarning,
  37. stacklevel=2,
  38. )
  39. if isinstance(geometry, PreparedGeometry):
  40. geometry = geometry.context
  41. shapely.prepare(geometry)
  42. return shapely.contains_xy(geometry, x, y)
  43. def touches(geometry, x, y):
  44. """Check whether multiple points touch the exterior of a single geometry.
  45. Vectorized (element-wise) version of `touches`.
  46. Parameters
  47. ----------
  48. geometry : PreparedGeometry or subclass of BaseGeometry
  49. The geometry which is to be checked to see whether each point is
  50. contained within. The geometry will be "prepared" if it is not already
  51. a PreparedGeometry instance.
  52. x : array
  53. The x coordinates of the points to check.
  54. y : array
  55. The y coordinates of the points to check.
  56. Returns
  57. -------
  58. Mask of points which touch the exterior of the given `geometry`.
  59. """
  60. warnings.warn(
  61. "The 'shapely.vectorized.touches' function is deprecated and will be "
  62. "removed a future version. Use 'shapely.intersects_xy(geometry.boundary, x, y)'"
  63. " instead (available since shapely 2.0.0).",
  64. DeprecationWarning,
  65. stacklevel=2,
  66. )
  67. if isinstance(geometry, PreparedGeometry):
  68. geometry = geometry.context
  69. # Touches(geom, point) == Intersects(Boundary(geom), point)
  70. boundary = geometry.boundary
  71. shapely.prepare(boundary)
  72. return shapely.intersects_xy(boundary, x, y)