geoquerysets.txt 29 KB

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