geoquerysets.txt 24 KB

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