1. ==========================
    
  2. GIS QuerySet API Reference
    
  3. ==========================
    
  4. 
    
  5. .. currentmodule:: django.contrib.gis.db.models
    
  6. 
    
  7. .. _spatial-lookups:
    
  8. 
    
  9. Spatial Lookups
    
  10. ===============
    
  11. 
    
  12. The spatial lookups in this section are available for :class:`GeometryField`
    
  13. and :class:`RasterField`.
    
  14. 
    
  15. For an introduction, see the :ref:`spatial lookups introduction
    
  16. <spatial-lookups-intro>`.  For an overview of what lookups are
    
  17. compatible with a particular spatial backend, refer to the
    
  18. :ref:`spatial lookup compatibility table <spatial-lookup-compatibility>`.
    
  19. 
    
  20. Lookups with rasters
    
  21. --------------------
    
  22. 
    
  23. All examples in the reference below are given for geometry fields and inputs,
    
  24. but the lookups can be used the same way with rasters on both sides. Whenever
    
  25. a lookup doesn't support raster input, the input is automatically
    
  26. converted to a geometry where necessary using the `ST_Polygon
    
  27. <https://postgis.net/docs/RT_ST_Polygon.html>`_ function. See also the
    
  28. :ref:`introduction to raster lookups <spatial-lookup-raster>`.
    
  29. 
    
  30. The database operators used by the lookups can be divided into three categories:
    
  31. 
    
  32. - Native raster support ``N``: the operator accepts rasters natively on both
    
  33.   sides of the lookup, and raster input can be mixed with geometry inputs.
    
  34. 
    
  35. - Bilateral raster support ``B``: the operator supports rasters only if both
    
  36.   sides of the lookup receive raster inputs. Raster data is automatically
    
  37.   converted to geometries for mixed lookups.
    
  38. 
    
  39. - Geometry conversion support ``C``. The lookup does not have native raster
    
  40.   support, all raster data is automatically converted to geometries.
    
  41. 
    
  42. The examples below show the SQL equivalent for the lookups in the different
    
  43. types of raster support. The same pattern applies to all spatial lookups.
    
  44. 
    
  45. ==== ============================== =======================================================
    
  46. Case Lookup                         SQL Equivalent
    
  47. ==== ============================== =======================================================
    
  48. N, B ``rast__contains=rst``         ``ST_Contains(rast, rst)``
    
  49. N, B ``rast__1__contains=(rst, 2)`` ``ST_Contains(rast, 1, rst, 2)``
    
  50. B, C ``rast__contains=geom``        ``ST_Contains(ST_Polygon(rast), geom)``
    
  51. B, C ``rast__1__contains=geom``     ``ST_Contains(ST_Polygon(rast, 1), geom)``
    
  52. B, C ``poly__contains=rst``         ``ST_Contains(poly, ST_Polygon(rst))``
    
  53. B, C ``poly__contains=(rst, 1)``    ``ST_Contains(poly, ST_Polygon(rst, 1))``
    
  54. C    ``rast__crosses=rst``          ``ST_Crosses(ST_Polygon(rast), ST_Polygon(rst))``
    
  55. C    ``rast__1__crosses=(rst, 2)``  ``ST_Crosses(ST_Polygon(rast, 1), ST_Polygon(rst, 2))``
    
  56. C    ``rast__crosses=geom``         ``ST_Crosses(ST_Polygon(rast), geom)``
    
  57. C    ``poly__crosses=rst``          ``ST_Crosses(poly, ST_Polygon(rst))``
    
  58. ==== ============================== =======================================================
    
  59. 
    
  60. Spatial lookups with rasters are only supported for PostGIS backends
    
  61. (denominated as PGRaster in this section).
    
  62. 
    
  63. .. fieldlookup:: bbcontains
    
  64. 
    
  65. ``bbcontains``
    
  66. --------------
    
  67. 
    
  68. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Contain.html>`__,
    
  69. MariaDB, MySQL, SpatiaLite, PGRaster (Native)
    
  70. 
    
  71. Tests if the geometry or raster field's bounding box completely contains the
    
  72. lookup geometry's bounding box.
    
  73. 
    
  74. Example::
    
  75. 
    
  76.     Zipcode.objects.filter(poly__bbcontains=geom)
    
  77. 
    
  78. ==========  ==========================
    
  79. Backend     SQL Equivalent
    
  80. ==========  ==========================
    
  81. PostGIS     ``poly ~ geom``
    
  82. MariaDB     ``MBRContains(poly, geom)``
    
  83. MySQL       ``MBRContains(poly, geom)``
    
  84. SpatiaLite  ``MbrContains(poly, geom)``
    
  85. ==========  ==========================
    
  86. 
    
  87. .. fieldlookup:: bboverlaps
    
  88. 
    
  89. ``bboverlaps``
    
  90. --------------
    
  91. 
    
  92. *Availability*: `PostGIS <https://postgis.net/docs/geometry_overlaps.html>`__,
    
  93. MariaDB, MySQL, SpatiaLite, PGRaster (Native)
    
  94. 
    
  95. Tests if the geometry field's bounding box overlaps the lookup geometry's
    
  96. bounding box.
    
  97. 
    
  98. Example::
    
  99. 
    
  100.     Zipcode.objects.filter(poly__bboverlaps=geom)
    
  101. 
    
  102. ==========  ==========================
    
  103. Backend     SQL Equivalent
    
  104. ==========  ==========================
    
  105. PostGIS     ``poly && geom``
    
  106. MariaDB     ``MBROverlaps(poly, geom)``
    
  107. MySQL       ``MBROverlaps(poly, geom)``
    
  108. SpatiaLite  ``MbrOverlaps(poly, geom)``
    
  109. ==========  ==========================
    
  110. 
    
  111. .. fieldlookup:: contained
    
  112. 
    
  113. ``contained``
    
  114. -------------
    
  115. 
    
  116. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Contained.html>`__,
    
  117. MariaDB, MySQL, SpatiaLite, PGRaster (Native)
    
  118. 
    
  119. Tests if the geometry field's bounding box is completely contained by the
    
  120. lookup geometry's bounding box.
    
  121. 
    
  122. Example::
    
  123. 
    
  124.     Zipcode.objects.filter(poly__contained=geom)
    
  125. 
    
  126. ==========  ==========================
    
  127. Backend     SQL Equivalent
    
  128. ==========  ==========================
    
  129. PostGIS     ``poly @ geom``
    
  130. MariaDB     ``MBRWithin(poly, geom)``
    
  131. MySQL       ``MBRWithin(poly, geom)``
    
  132. SpatiaLite  ``MbrWithin(poly, geom)``
    
  133. ==========  ==========================
    
  134. 
    
  135. .. fieldlookup:: gis-contains
    
  136. 
    
  137. ``contains``
    
  138. ------------
    
  139. 
    
  140. *Availability*: `PostGIS <https://postgis.net/docs/ST_Contains.html>`__,
    
  141. Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
    
  142. 
    
  143. Tests if the geometry field spatially contains the lookup geometry.
    
  144. 
    
  145. Example::
    
  146. 
    
  147.     Zipcode.objects.filter(poly__contains=geom)
    
  148. 
    
  149. ==========  ============================
    
  150. Backend     SQL Equivalent
    
  151. ==========  ============================
    
  152. PostGIS     ``ST_Contains(poly, geom)``
    
  153. Oracle      ``SDO_CONTAINS(poly, geom)``
    
  154. MariaDB     ``ST_Contains(poly, geom)``
    
  155. MySQL       ``ST_Contains(poly, geom)``
    
  156. SpatiaLite  ``Contains(poly, geom)``
    
  157. ==========  ============================
    
  158. 
    
  159. .. fieldlookup:: contains_properly
    
  160. 
    
  161. ``contains_properly``
    
  162. ---------------------
    
  163. 
    
  164. *Availability*: `PostGIS <https://postgis.net/docs/ST_ContainsProperly.html>`__,
    
  165. PGRaster (Bilateral)
    
  166. 
    
  167. Returns true if the lookup geometry intersects the interior of the
    
  168. geometry field, but not the boundary (or exterior).
    
  169. 
    
  170. Example::
    
  171. 
    
  172.     Zipcode.objects.filter(poly__contains_properly=geom)
    
  173. 
    
  174. ==========  ===================================
    
  175. Backend     SQL Equivalent
    
  176. ==========  ===================================
    
  177. PostGIS     ``ST_ContainsProperly(poly, geom)``
    
  178. ==========  ===================================
    
  179. 
    
  180. .. fieldlookup:: coveredby
    
  181. 
    
  182. ``coveredby``
    
  183. -------------
    
  184. 
    
  185. *Availability*: `PostGIS <https://postgis.net/docs/ST_CoveredBy.html>`__,
    
  186. Oracle, PGRaster (Bilateral), SpatiaLite
    
  187. 
    
  188. Tests if no point in the geometry field is outside the lookup geometry.
    
  189. [#fncovers]_
    
  190. 
    
  191. Example::
    
  192. 
    
  193.     Zipcode.objects.filter(poly__coveredby=geom)
    
  194. 
    
  195. ==========  =============================
    
  196. Backend     SQL Equivalent
    
  197. ==========  =============================
    
  198. PostGIS     ``ST_CoveredBy(poly, geom)``
    
  199. Oracle      ``SDO_COVEREDBY(poly, geom)``
    
  200. SpatiaLite  ``CoveredBy(poly, geom)``
    
  201. ==========  =============================
    
  202. 
    
  203. .. fieldlookup:: covers
    
  204. 
    
  205. ``covers``
    
  206. ----------
    
  207. 
    
  208. *Availability*: `PostGIS <https://postgis.net/docs/ST_Covers.html>`__,
    
  209. Oracle, PGRaster (Bilateral), SpatiaLite
    
  210. 
    
  211. Tests if no point in the lookup geometry is outside the geometry field.
    
  212. [#fncovers]_
    
  213. 
    
  214. Example::
    
  215. 
    
  216.     Zipcode.objects.filter(poly__covers=geom)
    
  217. 
    
  218. ==========  ==========================
    
  219. Backend     SQL Equivalent
    
  220. ==========  ==========================
    
  221. PostGIS     ``ST_Covers(poly, geom)``
    
  222. Oracle      ``SDO_COVERS(poly, geom)``
    
  223. SpatiaLite  ``Covers(poly, geom)``
    
  224. ==========  ==========================
    
  225. 
    
  226. .. fieldlookup:: crosses
    
  227. 
    
  228. ``crosses``
    
  229. -----------
    
  230. 
    
  231. *Availability*: `PostGIS <https://postgis.net/docs/ST_Crosses.html>`__,
    
  232. MariaDB, MySQL, SpatiaLite, PGRaster (Conversion)
    
  233. 
    
  234. Tests if the geometry field spatially crosses the lookup geometry.
    
  235. 
    
  236. Example::
    
  237. 
    
  238.     Zipcode.objects.filter(poly__crosses=geom)
    
  239. 
    
  240. ==========  ==========================
    
  241. Backend     SQL Equivalent
    
  242. ==========  ==========================
    
  243. PostGIS     ``ST_Crosses(poly, geom)``
    
  244. MariaDB     ``ST_Crosses(poly, geom)``
    
  245. MySQL       ``ST_Crosses(poly, geom)``
    
  246. SpatiaLite  ``Crosses(poly, geom)``
    
  247. ==========  ==========================
    
  248. 
    
  249. .. fieldlookup:: disjoint
    
  250. 
    
  251. ``disjoint``
    
  252. ------------
    
  253. 
    
  254. *Availability*: `PostGIS <https://postgis.net/docs/ST_Disjoint.html>`__,
    
  255. Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
    
  256. 
    
  257. Tests if the geometry field is spatially disjoint from the lookup geometry.
    
  258. 
    
  259. Example::
    
  260. 
    
  261.     Zipcode.objects.filter(poly__disjoint=geom)
    
  262. 
    
  263. ==========  =================================================
    
  264. Backend     SQL Equivalent
    
  265. ==========  =================================================
    
  266. PostGIS     ``ST_Disjoint(poly, geom)``
    
  267. Oracle      ``SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)``
    
  268. MariaDB     ``ST_Disjoint(poly, geom)``
    
  269. MySQL       ``ST_Disjoint(poly, geom)``
    
  270. SpatiaLite  ``Disjoint(poly, geom)``
    
  271. ==========  =================================================
    
  272. 
    
  273. .. fieldlookup:: equals
    
  274. 
    
  275. ``equals``
    
  276. ----------
    
  277. 
    
  278. *Availability*: `PostGIS <https://postgis.net/docs/ST_Equals.html>`__,
    
  279. Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Conversion)
    
  280. 
    
  281. Tests if the geometry field is spatially equal to the lookup geometry.
    
  282. 
    
  283. Example::
    
  284. 
    
  285.     Zipcode.objects.filter(poly__equals=geom)
    
  286. 
    
  287. ==========  =================================================
    
  288. Backend     SQL Equivalent
    
  289. ==========  =================================================
    
  290. PostGIS     ``ST_Equals(poly, geom)``
    
  291. Oracle      ``SDO_EQUAL(poly, geom)``
    
  292. MariaDB     ``ST_Equals(poly, geom)``
    
  293. MySQL       ``ST_Equals(poly, geom)``
    
  294. SpatiaLite  ``Equals(poly, geom)``
    
  295. ==========  =================================================
    
  296. 
    
  297. .. fieldlookup:: exact
    
  298.     :noindex:
    
  299. .. fieldlookup:: same_as
    
  300. 
    
  301. ``exact``, ``same_as``
    
  302. ----------------------
    
  303. 
    
  304. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Same.html>`__,
    
  305. Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
    
  306. 
    
  307. Tests if the geometry field is "equal" to the lookup geometry. On Oracle,
    
  308. MySQL, and SpatiaLite, it tests spatial equality, while on PostGIS it tests
    
  309. equality of bounding boxes.
    
  310. 
    
  311. Example::
    
  312. 
    
  313.     Zipcode.objects.filter(poly=geom)
    
  314. 
    
  315. ==========  =================================================
    
  316. Backend     SQL Equivalent
    
  317. ==========  =================================================
    
  318. PostGIS     ``poly ~= geom``
    
  319. Oracle      ``SDO_EQUAL(poly, geom)``
    
  320. MariaDB     ``ST_Equals(poly, geom)``
    
  321. MySQL       ``ST_Equals(poly, geom)``
    
  322. SpatiaLite  ``Equals(poly, geom)``
    
  323. ==========  =================================================
    
  324. 
    
  325. .. fieldlookup:: intersects
    
  326. 
    
  327. ``intersects``
    
  328. --------------
    
  329. 
    
  330. *Availability*: `PostGIS <https://postgis.net/docs/ST_Intersects.html>`__,
    
  331. Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
    
  332. 
    
  333. Tests if the geometry field spatially intersects the lookup geometry.
    
  334. 
    
  335. Example::
    
  336. 
    
  337.     Zipcode.objects.filter(poly__intersects=geom)
    
  338. 
    
  339. ==========  =================================================
    
  340. Backend     SQL Equivalent
    
  341. ==========  =================================================
    
  342. PostGIS     ``ST_Intersects(poly, geom)``
    
  343. Oracle      ``SDO_OVERLAPBDYINTERSECT(poly, geom)``
    
  344. MariaDB     ``ST_Intersects(poly, geom)``
    
  345. MySQL       ``ST_Intersects(poly, geom)``
    
  346. SpatiaLite  ``Intersects(poly, geom)``
    
  347. ==========  =================================================
    
  348. 
    
  349. .. fieldlookup:: isvalid
    
  350. 
    
  351. ``isvalid``
    
  352. -----------
    
  353. 
    
  354. *Availability*: MySQL (≥ 5.7.5), `PostGIS
    
  355. <https://postgis.net/docs/ST_IsValid.html>`__, Oracle, SpatiaLite
    
  356. 
    
  357. Tests if the geometry is valid.
    
  358. 
    
  359. Example::
    
  360. 
    
  361.     Zipcode.objects.filter(poly__isvalid=True)
    
  362. 
    
  363. ==========================  ================================================================
    
  364. Backend                     SQL Equivalent
    
  365. ==========================  ================================================================
    
  366. MySQL, PostGIS, SpatiaLite  ``ST_IsValid(poly)``
    
  367. Oracle                      ``SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE'``
    
  368. ==========================  ================================================================
    
  369. 
    
  370. .. fieldlookup:: overlaps
    
  371. 
    
  372. ``overlaps``
    
  373. ------------
    
  374. 
    
  375. *Availability*: `PostGIS <https://postgis.net/docs/ST_Overlaps.html>`__,
    
  376. Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
    
  377. 
    
  378. Tests if the geometry field spatially overlaps the lookup geometry.
    
  379. 
    
  380. ==========  ============================
    
  381. Backend     SQL Equivalent
    
  382. ==========  ============================
    
  383. PostGIS     ``ST_Overlaps(poly, geom)``
    
  384. Oracle      ``SDO_OVERLAPS(poly, geom)``
    
  385. MariaDB     ``ST_Overlaps(poly, geom)``
    
  386. MySQL       ``ST_Overlaps(poly, geom)``
    
  387. SpatiaLite  ``Overlaps(poly, geom)``
    
  388. ==========  ============================
    
  389. 
    
  390. .. fieldlookup:: relate
    
  391. 
    
  392. ``relate``
    
  393. ----------
    
  394. 
    
  395. *Availability*: `PostGIS <https://postgis.net/docs/ST_Relate.html>`__,
    
  396. MariaDB, Oracle, SpatiaLite, PGRaster (Conversion)
    
  397. 
    
  398. Tests if the geometry field is spatially related to the lookup geometry by
    
  399. the values given in the given pattern.  This lookup requires a tuple parameter,
    
  400. ``(geom, pattern)``; the form of ``pattern`` will depend on the spatial backend:
    
  401. 
    
  402. MariaDB, PostGIS, and SpatiaLite
    
  403. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  404. 
    
  405. On these spatial backends the intersection pattern is a string comprising
    
  406. nine characters, which  define intersections between  the interior, boundary,
    
  407. and exterior of the geometry field and the lookup geometry.
    
  408. The intersection pattern matrix may only use the following characters:
    
  409. ``1``, ``2``, ``T``, ``F``, or ``*``.  This lookup type allows users to "fine tune"
    
  410. a specific geometric relationship consistent with the DE-9IM model. [#fnde9im]_
    
  411. 
    
  412. Geometry example::
    
  413. 
    
  414.     # A tuple lookup parameter is used to specify the geometry and
    
  415.     # the intersection pattern (the pattern here is for 'contains').
    
  416.     Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))
    
  417. 
    
  418. PostGIS and MariaDB SQL equivalent:
    
  419. 
    
  420. .. code-block:: sql
    
  421. 
    
  422.     SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')
    
  423. 
    
  424. SpatiaLite SQL equivalent:
    
  425. 
    
  426. .. code-block:: sql
    
  427. 
    
  428.     SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')
    
  429. 
    
  430. Raster example::
    
  431. 
    
  432.     Zipcode.objects.filter(poly__relate=(rast, 1, 'T*T***FF*'))
    
  433.     Zipcode.objects.filter(rast__2__relate=(rast, 1, 'T*T***FF*'))
    
  434. 
    
  435. PostGIS SQL equivalent:
    
  436. 
    
  437. .. code-block:: sql
    
  438. 
    
  439.     SELECT ... WHERE ST_Relate(poly, ST_Polygon(rast, 1), 'T*T***FF*')
    
  440.     SELECT ... WHERE ST_Relate(ST_Polygon(rast, 2), ST_Polygon(rast, 1), 'T*T***FF*')
    
  441. 
    
  442. Oracle
    
  443. ~~~~~~
    
  444. 
    
  445. Here the relation pattern is comprised of at least one of the nine relation
    
  446. strings: ``TOUCH``, ``OVERLAPBDYDISJOINT``, ``OVERLAPBDYINTERSECT``,
    
  447. ``EQUAL``, ``INSIDE``, ``COVEREDBY``, ``CONTAINS``, ``COVERS``, ``ON``, and
    
  448. ``ANYINTERACT``.   Multiple strings may be combined with the logical Boolean
    
  449. operator OR, for example, ``'inside+touch'``. [#fnsdorelate]_  The relation
    
  450. strings are case-insensitive.
    
  451. 
    
  452. Example::
    
  453. 
    
  454.     Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))
    
  455. 
    
  456. Oracle SQL equivalent:
    
  457. 
    
  458. .. code-block:: sql
    
  459. 
    
  460.     SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')
    
  461. 
    
  462. .. fieldlookup:: touches
    
  463. 
    
  464. ``touches``
    
  465. -----------
    
  466. 
    
  467. *Availability*: `PostGIS <https://postgis.net/docs/ST_Touches.html>`__,
    
  468. Oracle, MariaDB, MySQL, SpatiaLite
    
  469. 
    
  470. Tests if the geometry field spatially touches the lookup geometry.
    
  471. 
    
  472. Example::
    
  473. 
    
  474.     Zipcode.objects.filter(poly__touches=geom)
    
  475. 
    
  476. ==========  ==========================
    
  477. Backend     SQL Equivalent
    
  478. ==========  ==========================
    
  479. PostGIS     ``ST_Touches(poly, geom)``
    
  480. MariaDB     ``ST_Touches(poly, geom)``
    
  481. MySQL       ``ST_Touches(poly, geom)``
    
  482. Oracle      ``SDO_TOUCH(poly, geom)``
    
  483. SpatiaLite  ``Touches(poly, geom)``
    
  484. ==========  ==========================
    
  485. 
    
  486. .. fieldlookup:: within
    
  487. 
    
  488. ``within``
    
  489. ----------
    
  490. 
    
  491. *Availability*: `PostGIS <https://postgis.net/docs/ST_Within.html>`__,
    
  492. Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
    
  493. 
    
  494. Tests if the geometry field is spatially within the lookup geometry.
    
  495. 
    
  496. Example::
    
  497. 
    
  498.     Zipcode.objects.filter(poly__within=geom)
    
  499. 
    
  500. ==========  ==========================
    
  501. Backend     SQL Equivalent
    
  502. ==========  ==========================
    
  503. PostGIS     ``ST_Within(poly, geom)``
    
  504. MariaDB     ``ST_Within(poly, geom)``
    
  505. MySQL       ``ST_Within(poly, geom)``
    
  506. Oracle      ``SDO_INSIDE(poly, geom)``
    
  507. SpatiaLite  ``Within(poly, geom)``
    
  508. ==========  ==========================
    
  509. 
    
  510. .. fieldlookup:: left
    
  511. 
    
  512. ``left``
    
  513. --------
    
  514. 
    
  515. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Left.html>`__,
    
  516. PGRaster (Conversion)
    
  517. 
    
  518. Tests if the geometry field's bounding box is strictly to the left of the
    
  519. lookup geometry's bounding box.
    
  520. 
    
  521. Example::
    
  522. 
    
  523.     Zipcode.objects.filter(poly__left=geom)
    
  524. 
    
  525. PostGIS equivalent:
    
  526. 
    
  527. .. code-block:: sql
    
  528. 
    
  529.     SELECT ... WHERE poly << geom
    
  530. 
    
  531. .. fieldlookup:: right
    
  532. 
    
  533. ``right``
    
  534. ---------
    
  535. 
    
  536. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Right.html>`__,
    
  537. PGRaster (Conversion)
    
  538. 
    
  539. Tests if the geometry field's bounding box is strictly to the right of the
    
  540. lookup geometry's bounding box.
    
  541. 
    
  542. Example::
    
  543. 
    
  544.     Zipcode.objects.filter(poly__right=geom)
    
  545. 
    
  546. PostGIS equivalent:
    
  547. 
    
  548. .. code-block:: sql
    
  549. 
    
  550.     SELECT ... WHERE poly >> geom
    
  551. 
    
  552. .. fieldlookup:: overlaps_left
    
  553. 
    
  554. ``overlaps_left``
    
  555. -----------------
    
  556. 
    
  557. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overleft.html>`__,
    
  558. PGRaster (Bilateral)
    
  559. 
    
  560. Tests if the geometry field's bounding box overlaps or is to the left of the lookup
    
  561. geometry's bounding box.
    
  562. 
    
  563. Example::
    
  564. 
    
  565.     Zipcode.objects.filter(poly__overlaps_left=geom)
    
  566. 
    
  567. PostGIS equivalent:
    
  568. 
    
  569. .. code-block:: sql
    
  570. 
    
  571.     SELECT ... WHERE poly &< geom
    
  572. 
    
  573. 
    
  574. .. fieldlookup:: overlaps_right
    
  575. 
    
  576. ``overlaps_right``
    
  577. ------------------
    
  578. 
    
  579. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overright.html>`__,
    
  580. PGRaster (Bilateral)
    
  581. 
    
  582. Tests if the geometry field's bounding box overlaps or is to the right of the lookup
    
  583. geometry's bounding box.
    
  584. 
    
  585. Example::
    
  586. 
    
  587.     Zipcode.objects.filter(poly__overlaps_right=geom)
    
  588. 
    
  589. PostGIS equivalent:
    
  590. 
    
  591. .. code-block:: sql
    
  592. 
    
  593.     SELECT ... WHERE poly &> geom
    
  594. 
    
  595. .. fieldlookup:: overlaps_above
    
  596. 
    
  597. ``overlaps_above``
    
  598. ------------------
    
  599. 
    
  600. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overabove.html>`__,
    
  601. PGRaster (Conversion)
    
  602. 
    
  603. Tests if the geometry field's bounding box overlaps or is above the lookup
    
  604. geometry's bounding box.
    
  605. 
    
  606. Example::
    
  607. 
    
  608.     Zipcode.objects.filter(poly__overlaps_above=geom)
    
  609. 
    
  610. PostGIS equivalent:
    
  611. 
    
  612. .. code-block:: sql
    
  613. 
    
  614.     SELECT ... WHERE poly |&> geom
    
  615. 
    
  616. .. fieldlookup:: overlaps_below
    
  617. 
    
  618. ``overlaps_below``
    
  619. ------------------
    
  620. 
    
  621. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overbelow.html>`__,
    
  622. PGRaster (Conversion)
    
  623. 
    
  624. Tests if the geometry field's bounding box overlaps or is below the lookup
    
  625. geometry's bounding box.
    
  626. 
    
  627. Example::
    
  628. 
    
  629.     Zipcode.objects.filter(poly__overlaps_below=geom)
    
  630. 
    
  631. PostGIS equivalent:
    
  632. 
    
  633. .. code-block:: sql
    
  634. 
    
  635.     SELECT ... WHERE poly &<| geom
    
  636. 
    
  637. .. fieldlookup:: strictly_above
    
  638. 
    
  639. ``strictly_above``
    
  640. ------------------
    
  641. 
    
  642. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Above.html>`__,
    
  643. PGRaster (Conversion)
    
  644. 
    
  645. Tests if the geometry field's bounding box is strictly above the lookup
    
  646. geometry's bounding box.
    
  647. 
    
  648. Example::
    
  649. 
    
  650.     Zipcode.objects.filter(poly__strictly_above=geom)
    
  651. 
    
  652. PostGIS equivalent:
    
  653. 
    
  654. .. code-block:: sql
    
  655. 
    
  656.     SELECT ... WHERE poly |>> geom
    
  657. 
    
  658. .. fieldlookup:: strictly_below
    
  659. 
    
  660. ``strictly_below``
    
  661. ------------------
    
  662. 
    
  663. *Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Below.html>`__,
    
  664. PGRaster (Conversion)
    
  665. 
    
  666. Tests if the geometry field's bounding box is strictly below the lookup
    
  667. geometry's bounding box.
    
  668. 
    
  669. Example::
    
  670. 
    
  671.     Zipcode.objects.filter(poly__strictly_below=geom)
    
  672. 
    
  673. PostGIS equivalent:
    
  674. 
    
  675. .. code-block:: sql
    
  676. 
    
  677.     SELECT ... WHERE poly <<| geom
    
  678. 
    
  679. 
    
  680. .. _distance-lookups:
    
  681. 
    
  682. Distance Lookups
    
  683. ================
    
  684. 
    
  685. *Availability*: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Native)
    
  686. 
    
  687. For an overview on performing distance queries, please refer to
    
  688. the :ref:`distance queries introduction <distance-queries>`.
    
  689. 
    
  690. Distance lookups take the following form::
    
  691. 
    
  692.     <field>__<distance lookup>=(<geometry/raster>, <distance value>[, 'spheroid'])
    
  693.     <field>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])
    
  694.     <field>__<band_index>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])
    
  695. 
    
  696. The value passed into a distance lookup is a tuple; the first two
    
  697. values are mandatory, and are the geometry to calculate distances to,
    
  698. and a distance value (either a number in units of the field, a
    
  699. :class:`~django.contrib.gis.measure.Distance` object, or a :doc:`query
    
  700. expression </ref/models/expressions>`). To pass a band index to the lookup, use
    
  701. a 3-tuple where the second entry is the band index.
    
  702. 
    
  703. On every distance lookup except :lookup:`dwithin`, an optional element,
    
  704. ``'spheroid'``, may be included to use the more accurate spheroid distance
    
  705. calculation functions on fields with a geodetic coordinate system.
    
  706. 
    
  707. On PostgreSQL, the ``'spheroid'`` option uses `ST_DistanceSpheroid
    
  708. <https://postgis.net/docs/ST_Distance_Spheroid.html>`__ instead of
    
  709. `ST_DistanceSphere <https://postgis.net/docs/ST_DistanceSphere.html>`__. The
    
  710. simpler `ST_Distance <https://postgis.net/docs/ST_Distance.html>`__ function is
    
  711. used with projected coordinate systems. Rasters are converted to geometries for
    
  712. spheroid based lookups.
    
  713. 
    
  714. .. fieldlookup:: distance_gt
    
  715. 
    
  716. ``distance_gt``
    
  717. ---------------
    
  718. 
    
  719. Returns models where the distance to the geometry field from the lookup
    
  720. geometry is greater than the given distance value.
    
  721. 
    
  722. Example::
    
  723. 
    
  724.     Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))
    
  725. 
    
  726. ==========  ==================================================
    
  727. Backend     SQL Equivalent
    
  728. ==========  ==================================================
    
  729. PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) > 5``
    
  730. MariaDB     ``ST_Distance(poly, geom) > 5``
    
  731. MySQL       ``ST_Distance(poly, geom) > 5``
    
  732. Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5``
    
  733. SpatiaLite  ``Distance(poly, geom) > 5``
    
  734. ==========  ==================================================
    
  735. 
    
  736. .. fieldlookup:: distance_gte
    
  737. 
    
  738. ``distance_gte``
    
  739. ----------------
    
  740. 
    
  741. Returns models where the distance to the geometry field from the lookup
    
  742. geometry is greater than or equal to the given distance value.
    
  743. 
    
  744. Example::
    
  745. 
    
  746.     Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))
    
  747. 
    
  748. ==========  ===================================================
    
  749. Backend     SQL Equivalent
    
  750. ==========  ===================================================
    
  751. PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) >= 5``
    
  752. MariaDB     ``ST_Distance(poly, geom) >= 5``
    
  753. MySQL       ``ST_Distance(poly, geom) >= 5``
    
  754. Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5``
    
  755. SpatiaLite  ``Distance(poly, geom) >= 5``
    
  756. ==========  ===================================================
    
  757. 
    
  758. .. fieldlookup:: distance_lt
    
  759. 
    
  760. ``distance_lt``
    
  761. ---------------
    
  762. 
    
  763. Returns models where the distance to the geometry field from the lookup
    
  764. geometry is less than the given distance value.
    
  765. 
    
  766. Example::
    
  767. 
    
  768.     Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))
    
  769. 
    
  770. ==========  ==================================================
    
  771. Backend     SQL Equivalent
    
  772. ==========  ==================================================
    
  773. PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) < 5``
    
  774. MariaDB     ``ST_Distance(poly, geom) < 5``
    
  775. MySQL       ``ST_Distance(poly, geom) < 5``
    
  776. Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5``
    
  777. SpatiaLite  ``Distance(poly, geom) < 5``
    
  778. ==========  ==================================================
    
  779. 
    
  780. .. fieldlookup:: distance_lte
    
  781. 
    
  782. ``distance_lte``
    
  783. ----------------
    
  784. 
    
  785. Returns models where the distance to the geometry field from the lookup
    
  786. geometry is less than or equal to the given distance value.
    
  787. 
    
  788. Example::
    
  789. 
    
  790.     Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))
    
  791. 
    
  792. ==========  ===================================================
    
  793. Backend     SQL Equivalent
    
  794. ==========  ===================================================
    
  795. PostGIS     ``ST_Distance/ST_Distance_Sphere(poly, geom) <= 5``
    
  796. MariaDB     ``ST_Distance(poly, geom) <= 5``
    
  797. MySQL       ``ST_Distance(poly, geom) <= 5``
    
  798. Oracle      ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5``
    
  799. SpatiaLite  ``Distance(poly, geom) <= 5``
    
  800. ==========  ===================================================
    
  801. 
    
  802. .. fieldlookup:: dwithin
    
  803. 
    
  804. ``dwithin``
    
  805. -----------
    
  806. 
    
  807. Returns models where the distance to the geometry field from the lookup
    
  808. geometry are within the given distance from one another. Note that you can only
    
  809. provide :class:`~django.contrib.gis.measure.Distance` objects if the targeted
    
  810. geometries are in a projected system. For geographic geometries, you should use
    
  811. units of the geometry field (e.g. degrees for ``WGS84``) .
    
  812. 
    
  813. Example::
    
  814. 
    
  815.     Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
    
  816. 
    
  817. ==========  ======================================
    
  818. Backend     SQL Equivalent
    
  819. ==========  ======================================
    
  820. PostGIS     ``ST_DWithin(poly, geom, 5)``
    
  821. Oracle      ``SDO_WITHIN_DISTANCE(poly, geom, 5)``
    
  822. SpatiaLite  ``PtDistWithin(poly, geom, 5)``
    
  823. ==========  ======================================
    
  824. 
    
  825. Aggregate Functions
    
  826. -------------------
    
  827. 
    
  828. Django provides some GIS-specific aggregate functions. For details on how to
    
  829. use these aggregate functions, see :doc:`the topic guide on aggregation
    
  830. </topics/db/aggregation>`.
    
  831. 
    
  832. =====================  =====================================================
    
  833. Keyword Argument       Description
    
  834. =====================  =====================================================
    
  835. ``tolerance``          This keyword is for Oracle only.  It is for the
    
  836.                        tolerance value used by the ``SDOAGGRTYPE``
    
  837.                        procedure; the  `Oracle documentation`__ has more
    
  838.                        details.
    
  839. =====================  =====================================================
    
  840. 
    
  841. __ https://docs.oracle.com/en/database/oracle/oracle-database/21/spatl/
    
  842.    spatial-concepts.html#GUID-CE10AB14-D5EA-43BA-A647-DAC9EEF41EE6
    
  843. 
    
  844. Example::
    
  845. 
    
  846.     >>> from django.contrib.gis.db.models import Extent, Union
    
  847.     >>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))
    
  848. 
    
  849. ``Collect``
    
  850. ~~~~~~~~~~~
    
  851. 
    
  852. .. class:: Collect(geo_field)
    
  853. 
    
  854. *Availability*: `PostGIS <https://postgis.net/docs/ST_Collect.html>`__,
    
  855. SpatiaLite
    
  856. 
    
  857. Returns a ``GEOMETRYCOLLECTION`` or a ``MULTI`` geometry object from the geometry
    
  858. column. This is analogous to a simplified version of the :class:`Union`
    
  859. aggregate, except it can be several orders of magnitude faster than performing
    
  860. a union because it rolls up geometries into a collection or multi object, not
    
  861. caring about dissolving boundaries.
    
  862. 
    
  863. ``Extent``
    
  864. ~~~~~~~~~~
    
  865. 
    
  866. .. class:: Extent(geo_field)
    
  867. 
    
  868. *Availability*: `PostGIS <https://postgis.net/docs/ST_Extent.html>`__,
    
  869. Oracle, SpatiaLite
    
  870. 
    
  871. Returns the extent of all ``geo_field`` in the ``QuerySet`` as a four-tuple,
    
  872. comprising the lower left coordinate and the upper right coordinate.
    
  873. 
    
  874. Example::
    
  875. 
    
  876.     >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
    
  877.     >>> print(qs['poly__extent'])
    
  878.     (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
    
  879. 
    
  880. ``Extent3D``
    
  881. ~~~~~~~~~~~~
    
  882. 
    
  883. .. class:: Extent3D(geo_field)
    
  884. 
    
  885. *Availability*: `PostGIS <https://postgis.net/docs/ST_3DExtent.html>`__
    
  886. 
    
  887. Returns the 3D extent of all ``geo_field`` in the ``QuerySet`` as a six-tuple,
    
  888. comprising the lower left coordinate and upper right coordinate (each with x, y,
    
  889. and z coordinates).
    
  890. 
    
  891. Example::
    
  892. 
    
  893.     >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent3D('poly'))
    
  894.     >>> print(qs['poly__extent3d'])
    
  895.     (-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)
    
  896. 
    
  897. ``MakeLine``
    
  898. ~~~~~~~~~~~~
    
  899. 
    
  900. .. class:: MakeLine(geo_field)
    
  901. 
    
  902. *Availability*: `PostGIS <https://postgis.net/docs/ST_MakeLine.html>`__,
    
  903. SpatiaLite
    
  904. 
    
  905. Returns a ``LineString`` constructed from the point field geometries in the
    
  906. ``QuerySet``. Currently, ordering the queryset has no effect.
    
  907. 
    
  908. Example::
    
  909. 
    
  910.     >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(MakeLine('poly'))
    
  911.     >>> print(qs['poly__makeline'])
    
  912.     LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)
    
  913. 
    
  914. ``Union``
    
  915. ~~~~~~~~~
    
  916. 
    
  917. .. class:: Union(geo_field)
    
  918. 
    
  919. *Availability*: `PostGIS <https://postgis.net/docs/ST_Union.html>`__,
    
  920. Oracle, SpatiaLite
    
  921. 
    
  922. This method returns a :class:`~django.contrib.gis.geos.GEOSGeometry` object
    
  923. comprising the union of every geometry in the queryset. Please note that use of
    
  924. ``Union`` is processor intensive and may take a significant amount of time on
    
  925. large querysets.
    
  926. 
    
  927. .. note::
    
  928. 
    
  929.     If the computation time for using this method is too expensive, consider
    
  930.     using :class:`Collect` instead.
    
  931. 
    
  932. Example::
    
  933. 
    
  934.     >>> u = Zipcode.objects.aggregate(Union(poly))  # This may take a long time.
    
  935.     >>> u = Zipcode.objects.filter(poly__within=bbox).aggregate(Union(poly))  # A more sensible approach.
    
  936. 
    
  937. .. rubric:: Footnotes
    
  938. .. [#fnde9im] *See* `OpenGIS Simple Feature Specification For SQL <https://portal.ogc.org/files/?artifact_id=829>`_, at Ch. 2.1.13.2, p. 2-13 (The Dimensionally Extended Nine-Intersection Model).
    
  939. .. [#fnsdorelate] *See* `SDO_RELATE documentation <https://docs.oracle.com/en/
    
  940.    database/oracle/oracle-database/18/spatl/spatial-operators-reference.html#
    
  941.    GUID-97C17C18-F05E-49B4-BE11-E89B972E2A02>`_, from the Oracle Spatial and
    
  942.    Graph Developer's Guide.
    
  943. .. [#fncovers] For an explanation of this routine, read `Quirks of the "Contains" Spatial Predicate <https://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html>`_ by Martin Davis (a PostGIS developer).