predicates.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. """Predicates for spatial analysis."""
  2. import warnings
  3. import numpy as np
  4. from shapely import lib
  5. from shapely.decorators import multithreading_enabled, requires_geos
  6. __all__ = [
  7. "contains",
  8. "contains_properly",
  9. "contains_xy",
  10. "covered_by",
  11. "covers",
  12. "crosses",
  13. "disjoint",
  14. "dwithin",
  15. "equals",
  16. "equals_exact",
  17. "equals_identical",
  18. "has_m",
  19. "has_z",
  20. "intersects",
  21. "intersects_xy",
  22. "is_ccw",
  23. "is_closed",
  24. "is_empty",
  25. "is_geometry",
  26. "is_missing",
  27. "is_prepared",
  28. "is_ring",
  29. "is_simple",
  30. "is_valid",
  31. "is_valid_input",
  32. "is_valid_reason",
  33. "overlaps",
  34. "relate",
  35. "relate_pattern",
  36. "touches",
  37. "within",
  38. ]
  39. @multithreading_enabled
  40. def has_z(geometry, **kwargs):
  41. """Return True if a geometry has Z coordinates.
  42. Note that for GEOS < 3.12 this function returns False if the (first) Z coordinate
  43. equals NaN.
  44. Parameters
  45. ----------
  46. geometry : Geometry or array_like
  47. Geometry or geometries to check for Z coordinates.
  48. **kwargs
  49. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  50. See Also
  51. --------
  52. get_coordinate_dimension, has_m
  53. Examples
  54. --------
  55. >>> import shapely
  56. >>> from shapely import Point
  57. >>> shapely.has_z(Point(0, 0))
  58. False
  59. >>> shapely.has_z(Point(0, 0, 0))
  60. True
  61. >>> shapely.has_z(Point())
  62. False
  63. """
  64. return lib.has_z(geometry, **kwargs)
  65. @multithreading_enabled
  66. @requires_geos("3.12.0")
  67. def has_m(geometry, **kwargs):
  68. """Return True if a geometry has M coordinates.
  69. .. versionadded:: 2.1.0
  70. Parameters
  71. ----------
  72. geometry : Geometry or array_like
  73. Geometry or geometries to check for M coordinates.
  74. **kwargs
  75. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  76. See Also
  77. --------
  78. get_coordinate_dimension, has_z
  79. Examples
  80. --------
  81. >>> import shapely
  82. >>> shapely.has_m(shapely.from_wkt("POINT (0 0)"))
  83. False
  84. >>> shapely.has_m(shapely.from_wkt("POINT Z (0 0 0)"))
  85. False
  86. >>> shapely.has_m(shapely.from_wkt("POINT M (0 0 0)"))
  87. True
  88. >>> shapely.has_m(shapely.from_wkt("POINT ZM (0 0 0 0)"))
  89. True
  90. """
  91. return lib.has_m(geometry, **kwargs)
  92. @multithreading_enabled
  93. def is_ccw(geometry, **kwargs):
  94. """Return True if a linestring or linearring is counterclockwise.
  95. Note that there are no checks on whether lines are actually closed and
  96. not self-intersecting, while this is a requirement for is_ccw. The recommended
  97. usage of this function for linestrings is ``is_ccw(g) & is_simple(g)`` and for
  98. linearrings ``is_ccw(g) & is_valid(g)``.
  99. Parameters
  100. ----------
  101. geometry : Geometry or array_like
  102. This function will return False for non-linear geometries and for
  103. lines with fewer than 4 points (including the closing point).
  104. **kwargs
  105. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  106. See Also
  107. --------
  108. is_simple : Checks if a linestring is closed and simple.
  109. is_valid : Checks additionally if the geometry is simple.
  110. Examples
  111. --------
  112. >>> import shapely
  113. >>> from shapely import LinearRing, LineString, Point
  114. >>> shapely.is_ccw(LinearRing([(0, 0), (0, 1), (1, 1), (0, 0)]))
  115. False
  116. >>> shapely.is_ccw(LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]))
  117. True
  118. >>> shapely.is_ccw(LineString([(0, 0), (1, 1), (0, 1)]))
  119. False
  120. >>> shapely.is_ccw(Point(0, 0))
  121. False
  122. """
  123. return lib.is_ccw(geometry, **kwargs)
  124. @multithreading_enabled
  125. def is_closed(geometry, **kwargs):
  126. """Return True if a linestring's first and last points are equal.
  127. Parameters
  128. ----------
  129. geometry : Geometry or array_like
  130. This function will return False for non-linestrings.
  131. **kwargs
  132. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  133. See Also
  134. --------
  135. is_ring : Checks additionally if the geometry is simple.
  136. Examples
  137. --------
  138. >>> import shapely
  139. >>> from shapely import LineString, Point
  140. >>> shapely.is_closed(LineString([(0, 0), (1, 1)]))
  141. False
  142. >>> shapely.is_closed(LineString([(0, 0), (0, 1), (1, 1), (0, 0)]))
  143. True
  144. >>> shapely.is_closed(Point(0, 0))
  145. False
  146. """
  147. return lib.is_closed(geometry, **kwargs)
  148. @multithreading_enabled
  149. def is_empty(geometry, **kwargs):
  150. """Return True if a geometry is an empty point, polygon, etc.
  151. Parameters
  152. ----------
  153. geometry : Geometry or array_like
  154. Any geometry type is accepted.
  155. **kwargs
  156. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  157. See Also
  158. --------
  159. is_missing : checks if the object is a geometry
  160. Examples
  161. --------
  162. >>> import shapely
  163. >>> from shapely import Point
  164. >>> shapely.is_empty(Point())
  165. True
  166. >>> shapely.is_empty(Point(0, 0))
  167. False
  168. >>> shapely.is_empty(None)
  169. False
  170. """
  171. return lib.is_empty(geometry, **kwargs)
  172. @multithreading_enabled
  173. def is_geometry(geometry, **kwargs):
  174. """Return True if the object is a geometry.
  175. Parameters
  176. ----------
  177. geometry : any object or array_like
  178. Geometry or geometries to check.
  179. **kwargs
  180. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  181. See Also
  182. --------
  183. is_missing : check if an object is missing (None)
  184. is_valid_input : check if an object is a geometry or None
  185. Examples
  186. --------
  187. >>> import shapely
  188. >>> from shapely import GeometryCollection, Point
  189. >>> shapely.is_geometry(Point(0, 0))
  190. True
  191. >>> shapely.is_geometry(GeometryCollection())
  192. True
  193. >>> shapely.is_geometry(None)
  194. False
  195. >>> shapely.is_geometry("text")
  196. False
  197. """
  198. return lib.is_geometry(geometry, **kwargs)
  199. @multithreading_enabled
  200. def is_missing(geometry, **kwargs):
  201. """Return True if the object is not a geometry (None).
  202. Parameters
  203. ----------
  204. geometry : any object or array_like
  205. Geometry or geometries to check.
  206. **kwargs
  207. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  208. See Also
  209. --------
  210. is_geometry : check if an object is a geometry
  211. is_valid_input : check if an object is a geometry or None
  212. is_empty : checks if the object is an empty geometry
  213. Examples
  214. --------
  215. >>> import shapely
  216. >>> from shapely import GeometryCollection, Point
  217. >>> shapely.is_missing(Point(0, 0))
  218. False
  219. >>> shapely.is_missing(GeometryCollection())
  220. False
  221. >>> shapely.is_missing(None)
  222. True
  223. >>> shapely.is_missing("text")
  224. False
  225. """
  226. return lib.is_missing(geometry, **kwargs)
  227. @multithreading_enabled
  228. def is_prepared(geometry, **kwargs):
  229. """Return True if a Geometry is prepared.
  230. Note that it is not necessary to check if a geometry is already prepared
  231. before preparing it. It is more efficient to call ``prepare`` directly
  232. because it will skip geometries that are already prepared.
  233. This function will return False for missing geometries (None).
  234. Parameters
  235. ----------
  236. geometry : Geometry or array_like
  237. Geometry or geometries to check.
  238. **kwargs
  239. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  240. See Also
  241. --------
  242. is_valid_input : check if an object is a geometry or None
  243. prepare : prepare a geometry
  244. Examples
  245. --------
  246. >>> import shapely
  247. >>> from shapely import Point
  248. >>> geometry = Point(0, 0)
  249. >>> shapely.is_prepared(Point(0, 0))
  250. False
  251. >>> shapely.prepare(geometry)
  252. >>> shapely.is_prepared(geometry)
  253. True
  254. >>> shapely.is_prepared(None)
  255. False
  256. """
  257. return lib.is_prepared(geometry, **kwargs)
  258. @multithreading_enabled
  259. def is_valid_input(geometry, **kwargs):
  260. """Return True if the object is a geometry or None.
  261. Parameters
  262. ----------
  263. geometry : any object or array_like
  264. Geometry or geometries to check.
  265. **kwargs
  266. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  267. See Also
  268. --------
  269. is_geometry : checks if an object is a geometry
  270. is_missing : checks if an object is None
  271. Examples
  272. --------
  273. >>> import shapely
  274. >>> from shapely import GeometryCollection, Point
  275. >>> shapely.is_valid_input(Point(0, 0))
  276. True
  277. >>> shapely.is_valid_input(GeometryCollection())
  278. True
  279. >>> shapely.is_valid_input(None)
  280. True
  281. >>> shapely.is_valid_input(1.0)
  282. False
  283. >>> shapely.is_valid_input("text")
  284. False
  285. """
  286. return lib.is_valid_input(geometry, **kwargs)
  287. @multithreading_enabled
  288. def is_ring(geometry, **kwargs):
  289. """Return True if a linestring is closed and simple.
  290. This function will return False for non-linestrings.
  291. Parameters
  292. ----------
  293. geometry : Geometry or array_like
  294. Geometry or geometries to check.
  295. **kwargs
  296. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  297. See Also
  298. --------
  299. is_closed : Checks only if the geometry is closed.
  300. is_simple : Checks only if the geometry is simple.
  301. Examples
  302. --------
  303. >>> import shapely
  304. >>> from shapely import LineString, Point
  305. >>> shapely.is_ring(Point(0, 0))
  306. False
  307. >>> geom = LineString([(0, 0), (1, 1)])
  308. >>> shapely.is_closed(geom), shapely.is_simple(geom), shapely.is_ring(geom)
  309. (False, True, False)
  310. >>> geom = LineString([(0, 0), (0, 1), (1, 1), (0, 0)])
  311. >>> shapely.is_closed(geom), shapely.is_simple(geom), shapely.is_ring(geom)
  312. (True, True, True)
  313. >>> geom = LineString([(0, 0), (1, 1), (0, 1), (1, 0), (0, 0)])
  314. >>> shapely.is_closed(geom), shapely.is_simple(geom), shapely.is_ring(geom)
  315. (True, False, False)
  316. """
  317. return lib.is_ring(geometry, **kwargs)
  318. @multithreading_enabled
  319. def is_simple(geometry, **kwargs):
  320. """Return True if the geometry is simple.
  321. A simple geometry has no anomalous geometric points, such as
  322. self-intersections or self tangency.
  323. Note that polygons and linearrings are assumed to be simple. Use is_valid
  324. to check these kind of geometries for self-intersections.
  325. This function will return False for geometrycollections.
  326. Parameters
  327. ----------
  328. geometry : Geometry or array_like
  329. Geometry or geometries to check.
  330. **kwargs
  331. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  332. See Also
  333. --------
  334. is_ring : Checks additionally if the geometry is closed.
  335. is_valid : Checks whether a geometry is well formed.
  336. Examples
  337. --------
  338. >>> import shapely
  339. >>> from shapely import LineString, Polygon
  340. >>> shapely.is_simple(Polygon([(1, 1), (2, 1), (2, 2), (1, 1)]))
  341. True
  342. >>> shapely.is_simple(LineString([(0, 0), (1, 1), (0, 1), (1, 0), (0, 0)]))
  343. False
  344. >>> shapely.is_simple(None)
  345. False
  346. """
  347. return lib.is_simple(geometry, **kwargs)
  348. @multithreading_enabled
  349. def is_valid(geometry, **kwargs):
  350. """Return True if a geometry is well formed.
  351. Returns False for missing values.
  352. Parameters
  353. ----------
  354. geometry : Geometry or array_like
  355. Geometry or geometries to check. Any geometry type is accepted.
  356. **kwargs
  357. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  358. See Also
  359. --------
  360. is_valid_reason : Returns the reason in case of invalid.
  361. Examples
  362. --------
  363. >>> import shapely
  364. >>> from shapely import GeometryCollection, LineString, Polygon
  365. >>> shapely.is_valid(LineString([(0, 0), (1, 1)]))
  366. True
  367. >>> shapely.is_valid(Polygon([(0, 0), (1, 1), (1, 2), (1, 1), (0, 0)]))
  368. False
  369. >>> shapely.is_valid(GeometryCollection())
  370. True
  371. >>> shapely.is_valid(None)
  372. False
  373. """
  374. # GEOS is valid will emit warnings for invalid geometries. Suppress them.
  375. with warnings.catch_warnings():
  376. warnings.simplefilter("ignore")
  377. result = lib.is_valid(geometry, **kwargs)
  378. return result
  379. def is_valid_reason(geometry, **kwargs):
  380. """Return a string stating if a geometry is valid and if not, why.
  381. Returns None for missing values.
  382. Parameters
  383. ----------
  384. geometry : Geometry or array_like
  385. Geometry or geometries to check. Any geometry type is accepted.
  386. **kwargs
  387. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  388. See Also
  389. --------
  390. is_valid : returns True or False
  391. Examples
  392. --------
  393. >>> import shapely
  394. >>> from shapely import LineString, Polygon
  395. >>> shapely.is_valid_reason(LineString([(0, 0), (1, 1)]))
  396. 'Valid Geometry'
  397. >>> shapely.is_valid_reason(Polygon([(0, 0), (1, 1), (1, 2), (1, 1), (0, 0)]))
  398. 'Self-intersection[1 2]'
  399. >>> shapely.is_valid_reason(None) is None
  400. True
  401. """
  402. return lib.is_valid_reason(geometry, **kwargs)
  403. @multithreading_enabled
  404. def crosses(a, b, **kwargs):
  405. """Return True if A and B spatially cross.
  406. A crosses B if they have some but not all interior points in common,
  407. the intersection is one dimension less than the maximum dimension of A or B,
  408. and the intersection is not equal to either A or B.
  409. Parameters
  410. ----------
  411. a, b : Geometry or array_like
  412. Geometry or geometries to check.
  413. **kwargs
  414. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  415. See Also
  416. --------
  417. prepare : improve performance by preparing ``a`` (the first argument)
  418. Examples
  419. --------
  420. >>> import shapely
  421. >>> from shapely import LineString, MultiPoint, Point, Polygon
  422. >>> line = LineString([(0, 0), (1, 1)])
  423. >>> # A contains B:
  424. >>> shapely.crosses(line, Point(0.5, 0.5))
  425. False
  426. >>> # A and B intersect at a point but do not share all points:
  427. >>> shapely.crosses(line, MultiPoint([(0, 1), (0.5, 0.5)]))
  428. True
  429. >>> shapely.crosses(line, LineString([(0, 1), (1, 0)]))
  430. True
  431. >>> # A is contained by B; their intersection is a line (same dimension):
  432. >>> shapely.crosses(line, LineString([(0, 0), (2, 2)]))
  433. False
  434. >>> area = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  435. >>> # A contains B:
  436. >>> shapely.crosses(area, line)
  437. False
  438. >>> # A and B intersect with a line (lower dimension) but do not share all points:
  439. >>> shapely.crosses(area, LineString([(0, 0), (2, 2)]))
  440. True
  441. >>> # A contains B:
  442. >>> shapely.crosses(area, Point(0.5, 0.5))
  443. False
  444. >>> # A contains some but not all points of B; they intersect at a point:
  445. >>> shapely.crosses(area, MultiPoint([(2, 2), (0.5, 0.5)]))
  446. True
  447. """
  448. return lib.crosses(a, b, **kwargs)
  449. @multithreading_enabled
  450. def contains(a, b, **kwargs):
  451. """Return True if geometry B is completely inside geometry A.
  452. A contains B if no points of B lie in the exterior of A and at least one
  453. point of the interior of B lies in the interior of A.
  454. Note: following this definition, a geometry does not contain its boundary,
  455. but it does contain itself. See ``contains_properly`` for a version where
  456. a geometry does not contain itself.
  457. Parameters
  458. ----------
  459. a, b : Geometry or array_like
  460. Geometry or geometries to check.
  461. **kwargs
  462. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  463. See Also
  464. --------
  465. within : ``contains(A, B) == within(B, A)``
  466. contains_properly : contains with no common boundary points
  467. prepare : improve performance by preparing ``a`` (the first argument)
  468. contains_xy : variant for checking against a Point with x, y coordinates
  469. Examples
  470. --------
  471. >>> import shapely
  472. >>> from shapely import LineString, Point, Polygon
  473. >>> line = LineString([(0, 0), (1, 1)])
  474. >>> shapely.contains(line, Point(0, 0))
  475. False
  476. >>> shapely.contains(line, Point(0.5, 0.5))
  477. True
  478. >>> area = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  479. >>> shapely.contains(area, Point(0, 0))
  480. False
  481. >>> shapely.contains(area, line)
  482. True
  483. >>> shapely.contains(area, LineString([(0, 0), (2, 2)]))
  484. False
  485. >>> polygon_with_hole = Polygon(
  486. ... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
  487. ... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]]
  488. ... )
  489. >>> shapely.contains(polygon_with_hole, Point(1, 1))
  490. True
  491. >>> shapely.contains(polygon_with_hole, Point(2, 2))
  492. False
  493. >>> shapely.contains(polygon_with_hole, LineString([(1, 1), (5, 5)]))
  494. False
  495. >>> shapely.contains(area, area)
  496. True
  497. >>> shapely.contains(area, None)
  498. False
  499. """
  500. return lib.contains(a, b, **kwargs)
  501. @multithreading_enabled
  502. def contains_properly(a, b, **kwargs):
  503. """Return True if geometry B is completely inside geometry A, with no common
  504. boundary points.
  505. A contains B properly if B intersects the interior of A but not the
  506. boundary (or exterior). This means that a geometry A does not
  507. "contain properly" itself, which contrasts with the ``contains`` function,
  508. where common points on the boundary are allowed.
  509. Note: this function will prepare the geometries under the hood if needed.
  510. You can prepare the geometries in advance to avoid repeated preparation
  511. when calling this function multiple times.
  512. Parameters
  513. ----------
  514. a, b : Geometry or array_like
  515. Geometry or geometries to check.
  516. **kwargs
  517. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  518. See Also
  519. --------
  520. contains : contains which allows common boundary points
  521. prepare : improve performance by preparing ``a`` (the first argument)
  522. Examples
  523. --------
  524. >>> import shapely
  525. >>> from shapely import Polygon
  526. >>> area1 = Polygon([(0, 0), (3, 0), (3, 3), (0, 3), (0, 0)])
  527. >>> area2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  528. >>> area3 = Polygon([(1, 1), (2, 1), (2, 2), (1, 2), (1, 1)])
  529. ``area1`` and ``area2`` have a common border:
  530. >>> shapely.contains(area1, area2)
  531. True
  532. >>> shapely.contains_properly(area1, area2)
  533. False
  534. ``area3`` is completely inside ``area1`` with no common border:
  535. >>> shapely.contains(area1, area3)
  536. True
  537. >>> shapely.contains_properly(area1, area3)
  538. True
  539. """ # noqa: D205
  540. return lib.contains_properly(a, b, **kwargs)
  541. @multithreading_enabled
  542. def covered_by(a, b, **kwargs):
  543. """Return True if no point in geometry A is outside geometry B.
  544. Parameters
  545. ----------
  546. a, b : Geometry or array_like
  547. Geometry or geometries to check.
  548. **kwargs
  549. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  550. See Also
  551. --------
  552. covers : ``covered_by(A, B) == covers(B, A)``
  553. prepare : improve performance by preparing ``a`` (the first argument)
  554. Examples
  555. --------
  556. >>> import shapely
  557. >>> from shapely import LineString, Point, Polygon
  558. >>> line = LineString([(0, 0), (1, 1)])
  559. >>> shapely.covered_by(Point(0, 0), line)
  560. True
  561. >>> shapely.covered_by(Point(0.5, 0.5), line)
  562. True
  563. >>> area = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  564. >>> shapely.covered_by(Point(0, 0), area)
  565. True
  566. >>> shapely.covered_by(line, area)
  567. True
  568. >>> shapely.covered_by(LineString([(0, 0), (2, 2)]), area)
  569. False
  570. >>> polygon_with_hole = Polygon(
  571. ... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
  572. ... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]]
  573. ... )
  574. >>> shapely.covered_by(Point(1, 1), polygon_with_hole)
  575. True
  576. >>> shapely.covered_by(Point(2, 2), polygon_with_hole)
  577. True
  578. >>> shapely.covered_by(LineString([(1, 1), (5, 5)]), polygon_with_hole)
  579. False
  580. >>> shapely.covered_by(area, area)
  581. True
  582. >>> shapely.covered_by(None, area)
  583. False
  584. """
  585. return lib.covered_by(a, b, **kwargs)
  586. @multithreading_enabled
  587. def covers(a, b, **kwargs):
  588. """Return True if no point in geometry B is outside geometry A.
  589. Parameters
  590. ----------
  591. a, b : Geometry or array_like
  592. Geometry or geometries to check.
  593. **kwargs
  594. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  595. See Also
  596. --------
  597. covered_by : ``covers(A, B) == covered_by(B, A)``
  598. prepare : improve performance by preparing ``a`` (the first argument)
  599. Examples
  600. --------
  601. >>> import shapely
  602. >>> from shapely import LineString, Point, Polygon
  603. >>> line = LineString([(0, 0), (1, 1)])
  604. >>> shapely.covers(line, Point(0, 0))
  605. True
  606. >>> shapely.covers(line, Point(0.5, 0.5))
  607. True
  608. >>> area = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  609. >>> shapely.covers(area, Point(0, 0))
  610. True
  611. >>> shapely.covers(area, line)
  612. True
  613. >>> shapely.covers(area, LineString([(0, 0), (2, 2)]))
  614. False
  615. >>> polygon_with_hole = Polygon(
  616. ... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
  617. ... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]]
  618. ... )
  619. >>> shapely.covers(polygon_with_hole, Point(1, 1))
  620. True
  621. >>> shapely.covers(polygon_with_hole, Point(2, 2))
  622. True
  623. >>> shapely.covers(polygon_with_hole, LineString([(1, 1), (5, 5)]))
  624. False
  625. >>> shapely.covers(area, area)
  626. True
  627. >>> shapely.covers(area, None)
  628. False
  629. """
  630. return lib.covers(a, b, **kwargs)
  631. @multithreading_enabled
  632. def disjoint(a, b, **kwargs):
  633. """Return True if A and B do not share any point in space.
  634. Disjoint implies that overlaps, touches, within, and intersects are False.
  635. Note missing (None) values are never disjoint.
  636. Parameters
  637. ----------
  638. a, b : Geometry or array_like
  639. Geometry or geometries to check.
  640. **kwargs
  641. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  642. See Also
  643. --------
  644. intersects : ``disjoint(A, B) == ~intersects(A, B)``
  645. prepare : improve performance by preparing ``a`` (the first argument)
  646. Examples
  647. --------
  648. >>> import shapely
  649. >>> from shapely import GeometryCollection, LineString, Point
  650. >>> line = LineString([(0, 0), (1, 1)])
  651. >>> shapely.disjoint(line, Point(0, 0))
  652. False
  653. >>> shapely.disjoint(line, Point(0, 1))
  654. True
  655. >>> shapely.disjoint(line, LineString([(0, 2), (2, 0)]))
  656. False
  657. >>> empty = GeometryCollection()
  658. >>> shapely.disjoint(line, empty)
  659. True
  660. >>> shapely.disjoint(empty, empty)
  661. True
  662. >>> shapely.disjoint(empty, None)
  663. False
  664. >>> shapely.disjoint(None, None)
  665. False
  666. """
  667. return lib.disjoint(a, b, **kwargs)
  668. @multithreading_enabled
  669. def equals(a, b, **kwargs):
  670. """Return True if A and B are spatially equal.
  671. If A is within B and B is within A, A and B are considered equal. The
  672. ordering of points can be different.
  673. Parameters
  674. ----------
  675. a, b : Geometry or array_like
  676. Geometry or geometries to check.
  677. **kwargs
  678. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  679. See Also
  680. --------
  681. equals_exact : Check if A and B are structurally equal given a specified
  682. tolerance.
  683. Examples
  684. --------
  685. >>> import shapely
  686. >>> from shapely import GeometryCollection, LineString, Polygon
  687. >>> line = LineString([(0, 0), (5, 5), (10, 10)])
  688. >>> shapely.equals(line, LineString([(0, 0), (10, 10)]))
  689. True
  690. >>> shapely.equals(Polygon(), GeometryCollection())
  691. True
  692. >>> shapely.equals(None, None)
  693. False
  694. """
  695. return lib.equals(a, b, **kwargs)
  696. @multithreading_enabled
  697. def intersects(a, b, **kwargs):
  698. """Return True if A and B share any portion of space.
  699. Intersects implies that overlaps, touches, covers, or within are True.
  700. Parameters
  701. ----------
  702. a, b : Geometry or array_like
  703. Geometry or geometries to check.
  704. **kwargs
  705. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  706. See Also
  707. --------
  708. disjoint : ``intersects(A, B) == ~disjoint(A, B)``
  709. prepare : improve performance by preparing ``a`` (the first argument)
  710. intersects_xy : variant for checking against a Point with x, y coordinates
  711. Examples
  712. --------
  713. >>> import shapely
  714. >>> from shapely import LineString, Point
  715. >>> line = LineString([(0, 0), (1, 1)])
  716. >>> shapely.intersects(line, Point(0, 0))
  717. True
  718. >>> shapely.intersects(line, Point(0, 1))
  719. False
  720. >>> shapely.intersects(line, LineString([(0, 2), (2, 0)]))
  721. True
  722. >>> shapely.intersects(None, None)
  723. False
  724. """
  725. return lib.intersects(a, b, **kwargs)
  726. @multithreading_enabled
  727. def overlaps(a, b, **kwargs):
  728. """Return True if A and B spatially overlap.
  729. A and B overlap if they have some but not all points/space in
  730. common, have the same dimension, and the intersection of the
  731. interiors of the two geometries has the same dimension as the
  732. geometries themselves. That is, only polyons can overlap other
  733. polygons and only lines can overlap other lines. If A covers or is
  734. within B, overlaps won't be True.
  735. If either A or B are None, the output is always False.
  736. Parameters
  737. ----------
  738. a, b : Geometry or array_like
  739. Geometry or geometries to check.
  740. **kwargs
  741. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword
  742. arguments.
  743. See Also
  744. --------
  745. prepare : improve performance by preparing ``a`` (the first argument)
  746. Examples
  747. --------
  748. >>> import shapely
  749. >>> from shapely import LineString, Point, Polygon
  750. >>> poly = Polygon([(0, 0), (0, 4), (4, 4), (4, 0), (0, 0)])
  751. >>> # A and B share all points (are spatially equal):
  752. >>> shapely.overlaps(poly, poly)
  753. False
  754. >>> # A contains B; all points of B are within A:
  755. >>> shapely.overlaps(poly, Polygon([(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]))
  756. False
  757. >>> # A partially overlaps with B:
  758. >>> shapely.overlaps(poly, Polygon([(2, 2), (2, 6), (6, 6), (6, 2), (2, 2)]))
  759. True
  760. >>> line = LineString([(2, 2), (6, 6)])
  761. >>> # A and B are different dimensions; they cannot overlap:
  762. >>> shapely.overlaps(poly, line)
  763. False
  764. >>> shapely.overlaps(poly, Point(2, 2))
  765. False
  766. >>> # A and B share some but not all points:
  767. >>> shapely.overlaps(line, LineString([(0, 0), (4, 4)]))
  768. True
  769. >>> # A and B intersect only at a point (lower dimension); they do not overlap
  770. >>> shapely.overlaps(line, LineString([(6, 0), (0, 6)]))
  771. False
  772. >>> shapely.overlaps(poly, None)
  773. False
  774. >>> shapely.overlaps(None, None)
  775. False
  776. """
  777. return lib.overlaps(a, b, **kwargs)
  778. @multithreading_enabled
  779. def touches(a, b, **kwargs):
  780. """Return True if the only points shared between A and B are on their boundaries.
  781. Parameters
  782. ----------
  783. a, b : Geometry or array_like
  784. Geometry or geometries to check.
  785. **kwargs
  786. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  787. See Also
  788. --------
  789. prepare : improve performance by preparing ``a`` (the first argument)
  790. Examples
  791. --------
  792. >>> import shapely
  793. >>> from shapely import LineString, Point, Polygon
  794. >>> line = LineString([(0, 2), (2, 0)])
  795. >>> shapely.touches(line, Point(0, 2))
  796. True
  797. >>> shapely.touches(line, Point(1, 1))
  798. False
  799. >>> shapely.touches(line, LineString([(0, 0), (1, 1)]))
  800. True
  801. >>> shapely.touches(line, LineString([(0, 0), (2, 2)]))
  802. False
  803. >>> area = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  804. >>> shapely.touches(area, Point(0.5, 0))
  805. True
  806. >>> shapely.touches(area, Point(0.5, 0.5))
  807. False
  808. >>> shapely.touches(area, line)
  809. True
  810. >>> shapely.touches(area, Polygon([(0, 1), (1, 1), (1, 2), (0, 2), (0, 1)]))
  811. True
  812. """
  813. return lib.touches(a, b, **kwargs)
  814. @multithreading_enabled
  815. def within(a, b, **kwargs):
  816. """Return True if geometry A is completely inside geometry B.
  817. A is within B if no points of A lie in the exterior of B and at least one
  818. point of the interior of A lies in the interior of B.
  819. Parameters
  820. ----------
  821. a, b : Geometry or array_like
  822. Geometry or geometries to check.
  823. **kwargs
  824. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  825. See Also
  826. --------
  827. contains : ``within(A, B) == contains(B, A)``
  828. prepare : improve performance by preparing ``a`` (the first argument)
  829. Examples
  830. --------
  831. >>> import shapely
  832. >>> from shapely import LineString, Point, Polygon
  833. >>> line = LineString([(0, 0), (1, 1)])
  834. >>> shapely.within(Point(0, 0), line)
  835. False
  836. >>> shapely.within(Point(0.5, 0.5), line)
  837. True
  838. >>> area = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  839. >>> shapely.within(Point(0, 0), area)
  840. False
  841. >>> shapely.within(line, area)
  842. True
  843. >>> shapely.within(LineString([(0, 0), (2, 2)]), area)
  844. False
  845. >>> polygon_with_hole = Polygon(
  846. ... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
  847. ... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]]
  848. ... )
  849. >>> shapely.within(Point(1, 1), polygon_with_hole)
  850. True
  851. >>> shapely.within(Point(2, 2), polygon_with_hole)
  852. False
  853. >>> shapely.within(LineString([(1, 1), (5, 5)]), polygon_with_hole)
  854. False
  855. >>> shapely.within(area, area)
  856. True
  857. >>> shapely.within(None, area)
  858. False
  859. """
  860. return lib.within(a, b, **kwargs)
  861. @multithreading_enabled
  862. def equals_exact(a, b, tolerance=0.0, *, normalize=False, **kwargs):
  863. """Return True if the geometries are structurally equivalent within a given
  864. tolerance.
  865. This method uses exact coordinate equality, which requires coordinates
  866. to be equal (within specified tolerance) and in the same order for
  867. all components (vertices, rings, or parts) of a geometry. This is in
  868. contrast with the :func:`equals` function which uses spatial
  869. (topological) equality and does not require all components to be in the
  870. same order. Because of this, it is possible for :func:`equals` to
  871. be ``True`` while :func:`equals_exact` is ``False``.
  872. The order of the coordinates can be normalized (by setting the `normalize`
  873. keyword to ``True``) so that this function will return ``True`` when geometries
  874. are structurally equivalent but differ only in the ordering of vertices.
  875. However, this function will still return ``False`` if the order of interior
  876. rings within a :class:`Polygon` or the order of geometries within a multi
  877. geometry are different.
  878. Parameters
  879. ----------
  880. a, b : Geometry or array_like
  881. Geometry or geometries to check.
  882. tolerance : float or array_like (default: 0.)
  883. The tolerance to use in the comparison.
  884. normalize : bool, optional (default: False)
  885. If True, normalize the two geometries so that the coordinates are
  886. in the same order.
  887. .. versionadded:: 2.1.0
  888. **kwargs
  889. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  890. See Also
  891. --------
  892. equals : Check if `a` and `b` are spatially (topologically) equal.
  893. Examples
  894. --------
  895. >>> import shapely
  896. >>> from shapely import Point, Polygon
  897. >>> point1 = Point(50, 50)
  898. >>> point2 = Point(50.1, 50.1)
  899. >>> shapely.equals_exact(point1, point2)
  900. False
  901. >>> shapely.equals_exact(point1, point2, tolerance=0.2)
  902. True
  903. >>> shapely.equals_exact(point1, None, tolerance=0.2)
  904. False
  905. Difference between structural and spatial equality:
  906. >>> polygon1 = Polygon([(0, 0), (1, 1), (0, 1), (0, 0)])
  907. >>> polygon2 = Polygon([(0, 0), (0, 1), (1, 1), (0, 0)])
  908. >>> shapely.equals_exact(polygon1, polygon2)
  909. False
  910. >>> shapely.equals(polygon1, polygon2)
  911. True
  912. """ # noqa: D205
  913. if normalize:
  914. a = lib.normalize(a)
  915. b = lib.normalize(b)
  916. return lib.equals_exact(a, b, tolerance, **kwargs)
  917. @multithreading_enabled
  918. def equals_identical(a, b, **kwargs):
  919. """Return True if the geometries are identical.
  920. This function verifies whether geometries are pointwise equivalent by checking
  921. that the structure, ordering, and values of all vertices are identical
  922. in all dimensions.
  923. Similarly to :func:`equals_exact`, this function uses exact coordinate
  924. equality and requires coordinates to be in the same order for all
  925. components (vertices, rings, or parts) of a geometry. However, in
  926. contrast :func:`equals_exact`, this function does not allow to specify
  927. a tolerance, but does require all dimensions to be the same
  928. (:func:`equals_exact` ignores the Z and M dimensions), and NaN values
  929. are considered to be equal to other NaN values.
  930. This function is the vectorized equivalent of scalar equality of
  931. geometry objects (``a == b``, i.e. ``__eq__``).
  932. .. versionadded:: 2.1.0
  933. Parameters
  934. ----------
  935. a, b : Geometry or array_like
  936. Geometry or geometries to check.
  937. **kwargs
  938. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  939. See Also
  940. --------
  941. equals_exact : Check if geometries are structurally equal given a specified
  942. tolerance.
  943. equals : Check if geometries are spatially (topologically) equal.
  944. Examples
  945. --------
  946. >>> import shapely
  947. >>> from shapely import Point
  948. >>> shapely.equals_identical(Point(1, 2, 3), Point(1, 2, 3))
  949. True
  950. >>> shapely.equals_identical(Point(1, 2, 3), Point(1, 2, 0))
  951. False
  952. """
  953. return lib.equals_identical(a, b, **kwargs)
  954. def relate(a, b, **kwargs):
  955. """Return a string representation of the DE-9IM intersection matrix.
  956. Parameters
  957. ----------
  958. a, b : Geometry or array_like
  959. Geometry or geometries to check.
  960. **kwargs
  961. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  962. Examples
  963. --------
  964. >>> import shapely
  965. >>> from shapely import LineString, Point
  966. >>> point = Point(0, 0)
  967. >>> line = LineString([(0, 0), (1, 1)])
  968. >>> shapely.relate(point, line)
  969. 'F0FFFF102'
  970. """
  971. return lib.relate(a, b, **kwargs)
  972. @multithreading_enabled
  973. def relate_pattern(a, b, pattern, **kwargs):
  974. """Return True if the DE-9IM relationship code satisfies the pattern.
  975. This function compares the DE-9IM code string for two geometries
  976. against a specified pattern. If the string matches the pattern then
  977. ``True`` is returned, otherwise ``False``. The pattern specified can
  978. be an exact match (``0``, ``1`` or ``2``), a boolean match
  979. (uppercase ``T`` or ``F``), or a wildcard (``*``). For example,
  980. the pattern for the ``within`` predicate is ``'T*F**F***'``.
  981. Parameters
  982. ----------
  983. a, b : Geometry or array_like
  984. Geometry or geometries to check.
  985. pattern : string
  986. The pattern to match the DE-9IM relationship code against.
  987. **kwargs
  988. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  989. Examples
  990. --------
  991. >>> import shapely
  992. >>> from shapely import Point, Polygon
  993. >>> point = Point(0.5, 0.5)
  994. >>> square = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])
  995. >>> shapely.relate(point, square)
  996. '0FFFFF212'
  997. >>> shapely.relate_pattern(point, square, "T*F**F***")
  998. True
  999. """
  1000. return lib.relate_pattern(a, b, pattern, **kwargs)
  1001. @multithreading_enabled
  1002. @requires_geos("3.10.0")
  1003. def dwithin(a, b, distance, **kwargs):
  1004. """Return True if the geometries are within a given distance.
  1005. Using this function is more efficient than computing the distance and
  1006. comparing the result.
  1007. Parameters
  1008. ----------
  1009. a, b : Geometry or array_like
  1010. Geometry or geometries to check.
  1011. distance : float
  1012. Negative distances always return False.
  1013. **kwargs
  1014. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  1015. See Also
  1016. --------
  1017. distance : compute the actual distance between A and B
  1018. prepare : improve performance by preparing ``a`` (the first argument)
  1019. Examples
  1020. --------
  1021. >>> import shapely
  1022. >>> from shapely import Point
  1023. >>> point = Point(0.5, 0.5)
  1024. >>> shapely.dwithin(point, Point(2, 0.5), 2)
  1025. True
  1026. >>> shapely.dwithin(point, Point(2, 0.5), [2, 1.5, 1]).tolist()
  1027. [True, True, False]
  1028. >>> shapely.dwithin(point, Point(0.5, 0.5), 0)
  1029. True
  1030. >>> shapely.dwithin(point, None, 100)
  1031. False
  1032. """
  1033. return lib.dwithin(a, b, distance, **kwargs)
  1034. @multithreading_enabled
  1035. def contains_xy(geom, x, y=None, **kwargs):
  1036. """Return True if the Point (x, y) is completely inside geom.
  1037. This is a special-case (and faster) variant of the `contains` function
  1038. which avoids having to create a Point object if you start from x/y
  1039. coordinates.
  1040. Note that in the case of points, the `contains_properly` predicate is
  1041. equivalent to `contains`.
  1042. See the docstring of `contains` for more details about the predicate.
  1043. Parameters
  1044. ----------
  1045. geom : Geometry or array_like
  1046. Geometry or geometries to check if they contain the point.
  1047. x, y : float or array_like
  1048. Coordinates as separate x and y arrays, or a single array of
  1049. coordinate x, y tuples.
  1050. **kwargs
  1051. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  1052. See Also
  1053. --------
  1054. contains : variant taking two geometries as input
  1055. Notes
  1056. -----
  1057. If you compare a small number of polygons or lines with many points,
  1058. it can be beneficial to prepare the geometries in advance using
  1059. :func:`shapely.prepare`.
  1060. Examples
  1061. --------
  1062. >>> import shapely
  1063. >>> from shapely import Point, Polygon
  1064. >>> area = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  1065. >>> shapely.contains(area, Point(0.5, 0.5))
  1066. True
  1067. >>> shapely.contains_xy(area, 0.5, 0.5)
  1068. True
  1069. """
  1070. if y is None:
  1071. coords = np.asarray(x)
  1072. x, y = coords[:, 0], coords[:, 1]
  1073. if isinstance(geom, lib.Geometry):
  1074. lib.prepare(geom)
  1075. return lib.contains_xy(geom, x, y, **kwargs)
  1076. @multithreading_enabled
  1077. def intersects_xy(geom, x, y=None, **kwargs):
  1078. """Return True if geom and the Point (x, y) share any portion of space.
  1079. This is a special-case (and faster) variant of the `intersects` function
  1080. which avoids having to create a Point object if you start from x/y
  1081. coordinates.
  1082. See the docstring of `intersects` for more details about the predicate.
  1083. Parameters
  1084. ----------
  1085. geom : Geometry or array_like
  1086. Geometry or geometries to check if they intersect with the point.
  1087. x, y : float or array_like
  1088. Coordinates as separate x and y arrays, or a single array of
  1089. coordinate x, y tuples.
  1090. **kwargs
  1091. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  1092. See Also
  1093. --------
  1094. intersects : variant taking two geometries as input
  1095. Notes
  1096. -----
  1097. If you compare a single or few geometries with many points, it can be
  1098. beneficial to prepare the geometries in advance using
  1099. :func:`shapely.prepare`.
  1100. The `touches` predicate can be determined with this function by getting
  1101. the boundary of the geometries: ``intersects_xy(boundary(geom), x, y)``.
  1102. Examples
  1103. --------
  1104. >>> import shapely
  1105. >>> from shapely import LineString, Point
  1106. >>> line = LineString([(0, 0), (1, 1)])
  1107. >>> shapely.intersects(line, Point(0, 0))
  1108. True
  1109. >>> shapely.intersects_xy(line, 0, 0)
  1110. True
  1111. """
  1112. if y is None:
  1113. coords = np.asarray(x)
  1114. x, y = coords[:, 0], coords[:, 1]
  1115. if isinstance(geom, lib.Geometry):
  1116. lib.prepare(geom)
  1117. return lib.intersects_xy(geom, x, y, **kwargs)