geoquerysets.txt 26 KB

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