geoquerysets.txt 28 KB

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