geoquerysets.txt 24 KB

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