geoquerysets.txt 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. =========================
  2. GeoQuerySet API Reference
  3. =========================
  4. .. currentmodule:: django.contrib.gis.db.models
  5. .. class:: GeoQuerySet(model=None)
  6. .. _spatial-lookups:
  7. Spatial Lookups
  8. ===============
  9. Just like when using the :ref:`queryset-api`, interaction
  10. with ``GeoQuerySet`` by :ref:`chaining filters <chaining-filters>`.
  11. Instead of the regular Django :ref:`field-lookups`, the
  12. spatial lookups in this section are available for :class:`GeometryField`.
  13. For an introduction, see the :ref:`spatial lookups introduction
  14. <spatial-lookups-intro>`. For an overview of what lookups are
  15. compatible with a particular spatial backend, refer to the
  16. :ref:`spatial lookup compatibility table <spatial-lookup-compatibility>`.
  17. .. fieldlookup:: bbcontains
  18. bbcontains
  19. ----------
  20. *Availability*: PostGIS, MySQL, SpatiaLite
  21. Tests if the geometry field's bounding box completely contains the lookup
  22. geometry's bounding box.
  23. Example::
  24. Zipcode.objects.filter(poly__bbcontains=geom)
  25. ========== ==========================
  26. Backend SQL Equivalent
  27. ========== ==========================
  28. PostGIS ``poly ~ geom``
  29. MySQL ``MBRContains(poly, geom)``
  30. SpatiaLite ``MbrContains(poly, geom)``
  31. ========== ==========================
  32. .. fieldlookup:: bboverlaps
  33. bboverlaps
  34. ----------
  35. *Availability*: PostGIS, MySQL, SpatiaLite
  36. Tests if the geometry field's bounding box overlaps the lookup geometry's
  37. bounding box.
  38. Example::
  39. Zipcode.objects.filter(poly__bboverlaps=geom)
  40. ========== ==========================
  41. Backend SQL Equivalent
  42. ========== ==========================
  43. PostGIS ``poly && geom``
  44. MySQL ``MBROverlaps(poly, geom)``
  45. SpatiaLite ``MbrOverlaps(poly, geom)``
  46. ========== ==========================
  47. .. fieldlookup:: contained
  48. contained
  49. ---------
  50. *Availability*: PostGIS, MySQL, SpatiaLite
  51. Tests if the geometry field's bounding box is completely contained by the
  52. lookup geometry's bounding box.
  53. Example::
  54. Zipcode.objects.filter(poly__contained=geom)
  55. ========== ==========================
  56. Backend SQL Equivalent
  57. ========== ==========================
  58. PostGIS ``poly @ geom``
  59. MySQL ``MBRWithin(poly, geom)``
  60. SpatiaLite ``MbrWithin(poly, geom)``
  61. ========== ==========================
  62. .. fieldlookup:: gis-contains
  63. contains
  64. --------
  65. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  66. Tests if the geometry field spatially contains the lookup geometry.
  67. Example::
  68. Zipcode.objects.filter(poly__contains=geom)
  69. ========== ============================
  70. Backend SQL Equivalent
  71. ========== ============================
  72. PostGIS ``ST_Contains(poly, geom)``
  73. Oracle ``SDO_CONTAINS(poly, geom)``
  74. MySQL ``MBRContains(poly, geom)``
  75. SpatiaLite ``Contains(poly, geom)``
  76. ========== ============================
  77. .. fieldlookup:: contains_properly
  78. contains_properly
  79. -----------------
  80. *Availability*: PostGIS
  81. Returns true if the lookup geometry intersects the interior of the
  82. geometry field, but not the boundary (or exterior). [#fncontainsproperly]_
  83. Example::
  84. Zipcode.objects.filter(poly__contains_properly=geom)
  85. ========== ===================================
  86. Backend SQL Equivalent
  87. ========== ===================================
  88. PostGIS ``ST_ContainsProperly(poly, geom)``
  89. ========== ===================================
  90. .. fieldlookup:: coveredby
  91. coveredby
  92. ---------
  93. *Availability*: PostGIS, Oracle
  94. Tests if no point in the geometry field is outside the lookup geometry.
  95. [#fncovers]_
  96. Example::
  97. Zipcode.objects.filter(poly__coveredby=geom)
  98. ========== =============================
  99. Backend SQL Equivalent
  100. ========== =============================
  101. PostGIS ``ST_CoveredBy(poly, geom)``
  102. Oracle ``SDO_COVEREDBY(poly, geom)``
  103. ========== =============================
  104. .. fieldlookup:: covers
  105. covers
  106. ------
  107. *Availability*: PostGIS, Oracle
  108. Tests if no point in the lookup geometry is outside the geometry field.
  109. [#fncovers]_
  110. Example::
  111. Zipcode.objects.filter(poly__covers=geom)
  112. ========== ==========================
  113. Backend SQL Equivalent
  114. ========== ==========================
  115. PostGIS ``ST_Covers(poly, geom)``
  116. Oracle ``SDO_COVERS(poly, geom)``
  117. ========== ==========================
  118. .. fieldlookup:: crosses
  119. crosses
  120. -------
  121. *Availability*: PostGIS, SpatiaLite
  122. Tests if the geometry field spatially crosses the lookup geometry.
  123. Example::
  124. Zipcode.objects.filter(poly__crosses=geom)
  125. ========== ==========================
  126. Backend SQL Equivalent
  127. ========== ==========================
  128. PostGIS ``ST_Crosses(poly, geom)``
  129. SpatiaLite ``Crosses(poly, geom)``
  130. ========== ==========================
  131. .. fieldlookup:: disjoint
  132. disjoint
  133. --------
  134. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  135. Tests if the geometry field is spatially disjoint from the lookup geometry.
  136. Example::
  137. Zipcode.objects.filter(poly__disjoint=geom)
  138. ========== =================================================
  139. Backend SQL Equivalent
  140. ========== =================================================
  141. PostGIS ``ST_Disjoint(poly, geom)``
  142. Oracle ``SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)``
  143. MySQL ``MBRDisjoint(poly, geom)``
  144. SpatiaLite ``Disjoint(poly, geom)``
  145. ========== =================================================
  146. .. fieldlookup:: equals
  147. equals
  148. ------
  149. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  150. .. fieldlookup:: exact
  151. .. fieldlookup:: same_as
  152. exact, same_as
  153. --------------
  154. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  155. .. fieldlookup:: intersects
  156. intersects
  157. ----------
  158. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  159. Tests if the geometry field spatially intersects the lookup geometry.
  160. Example::
  161. Zipcode.objects.filter(poly__intersects=geom)
  162. ========== =================================================
  163. Backend SQL Equivalent
  164. ========== =================================================
  165. PostGIS ``ST_Intersects(poly, geom)``
  166. Oracle ``SDO_OVERLAPBDYINTERSECT(poly, geom)``
  167. MySQL ``MBRIntersects(poly, geom)``
  168. SpatiaLite ``Intersects(poly, geom)``
  169. ========== =================================================
  170. .. fieldlookup:: overlaps
  171. overlaps
  172. --------
  173. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  174. .. fieldlookup:: relate
  175. relate
  176. ------
  177. *Availability*: PostGIS, Oracle, SpatiaLite
  178. Tests if the geometry field is spatially related to the lookup geometry by
  179. the values given in the given pattern. This lookup requires a tuple parameter,
  180. ``(geom, pattern)``; the form of ``pattern`` will depend on the spatial backend:
  181. PostGIS & SpatiaLite
  182. ~~~~~~~~~~~~~~~~~~~~
  183. On these spatial backends the intersection pattern is a string comprising
  184. nine characters, which define intersections between the interior, boundary,
  185. and exterior of the geometry field and the lookup geometry.
  186. The intersection pattern matrix may only use the following characters:
  187. ``1``, ``2``, ``T``, ``F``, or ``*``. This lookup type allows users to "fine tune"
  188. a specific geometric relationship consistent with the DE-9IM model. [#fnde9im]_
  189. Example::
  190. # A tuple lookup parameter is used to specify the geometry and
  191. # the intersection pattern (the pattern here is for 'contains').
  192. Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))
  193. PostGIS SQL equivalent::
  194. SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')
  195. SpatiaLite SQL equivalent::
  196. SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')
  197. Oracle
  198. ~~~~~~
  199. Here the relation pattern is comprised at least one of the nine relation
  200. strings: ``TOUCH``, ``OVERLAPBDYDISJOINT``, ``OVERLAPBDYINTERSECT``,
  201. ``EQUAL``, ``INSIDE``, ``COVEREDBY``, ``CONTAINS``, ``COVERS``, ``ON``, and
  202. ``ANYINTERACT``. Multiple strings may be combined with the logical Boolean
  203. operator OR, for example, ``'inside+touch'``. [#fnsdorelate]_ The relation
  204. strings are case-insensitive.
  205. Example::
  206. Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))
  207. Oracle SQL equivalent::
  208. SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')
  209. .. fieldlookup:: touches
  210. touches
  211. -------
  212. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  213. Tests if the geometry field spatially touches the lookup geometry.
  214. Example::
  215. Zipcode.objects.filter(poly__touches=geom)
  216. ========== ==========================
  217. Backend SQL Equivalent
  218. ========== ==========================
  219. PostGIS ``ST_Touches(poly, geom)``
  220. MySQL ``MBRTouches(poly, geom)``
  221. Oracle ``SDO_TOUCH(poly, geom)``
  222. SpatiaLite ``Touches(poly, geom)``
  223. ========== ==========================
  224. .. fieldlookup:: within
  225. within
  226. ------
  227. *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
  228. Tests if the geometry field is spatially within the lookup geometry.
  229. Example::
  230. Zipcode.objects.filter(poly__within=geom)
  231. ========== ==========================
  232. Backend SQL Equivalent
  233. ========== ==========================
  234. PostGIS ``ST_Within(poly, geom)``
  235. MySQL ``MBRWithin(poly, geom)``
  236. Oracle ``SDO_INSIDE(poly, geom)``
  237. SpatiaLite ``Within(poly, geom)``
  238. ========== ==========================
  239. .. fieldlookup:: left
  240. left
  241. ----
  242. *Availability*: PostGIS
  243. Tests if the geometry field's bounding box is strictly to the left of the
  244. lookup geometry's bounding box.
  245. Example::
  246. Zipcode.objects.filter(poly__left=geom)
  247. PostGIS equivalent::
  248. SELECT ... WHERE poly << geom
  249. .. fieldlookup:: right
  250. right
  251. -----
  252. *Availability*: PostGIS
  253. Tests if the geometry field's bounding box is strictly to the right of the
  254. lookup geometry's bounding box.
  255. Example::
  256. Zipcode.objects.filter(poly__right=geom)
  257. PostGIS equivalent::
  258. SELECT ... WHERE poly >> geom
  259. .. fieldlookup:: overlaps_left
  260. overlaps_left
  261. -------------
  262. *Availability*: PostGIS
  263. Tests if the geometry field's bounding box overlaps or is to the left of the lookup
  264. geometry's bounding box.
  265. Example::
  266. Zipcode.objects.filter(poly__overlaps_left=geom)
  267. PostGIS equivalent::
  268. SELECT ... WHERE poly &< geom
  269. .. fieldlookup:: overlaps_right
  270. overlaps_right
  271. --------------
  272. *Availability*: PostGIS
  273. Tests if the geometry field's bounding box overlaps or is to the right of the lookup
  274. geometry's bounding box.
  275. Example::
  276. Zipcode.objects.filter(poly__overlaps_right=geom)
  277. PostGIS equivalent::
  278. SELECT ... WHERE poly &> geom
  279. .. fieldlookup:: overlaps_above
  280. overlaps_above
  281. --------------
  282. *Availability*: PostGIS
  283. Tests if the geometry field's bounding box overlaps or is above the lookup
  284. geometry's bounding box.
  285. Example::
  286. Zipcode.objects.filter(poly__overlaps_above=geom)
  287. PostGIS equivalent::
  288. SELECT ... WHERE poly |&> geom
  289. .. fieldlookup:: overlaps_below
  290. overlaps_below
  291. --------------
  292. *Availability*: PostGIS
  293. Tests if the geometry field's bounding box overlaps or is below the lookup
  294. geometry's bounding box.
  295. Example::
  296. Zipcode.objects.filter(poly__overlaps_below=geom)
  297. PostGIS equivalent::
  298. SELECT ... WHERE poly &<| geom
  299. .. fieldlookup:: strictly_above
  300. strictly_above
  301. --------------
  302. *Availability*: PostGIS
  303. Tests if the geometry field's bounding box is strictly above the lookup
  304. geometry's bounding box.
  305. Example::
  306. Zipcode.objects.filter(poly__strictly_above=geom)
  307. PostGIS equivalent::
  308. SELECT ... WHERE poly |>> geom
  309. .. fieldlookup:: strictly_below
  310. strictly_below
  311. --------------
  312. *Availability*: PostGIS
  313. Tests if the geometry field's bounding box is strictly below the lookup
  314. geometry's bounding box.
  315. Example::
  316. Zipcode.objects.filter(poly__strictly_below=geom)
  317. PostGIS equivalent::
  318. SELECT ... WHERE poly <<| geom
  319. .. _distance-lookups:
  320. Distance Lookups
  321. ================
  322. *Availability*: PostGIS, Oracle, SpatiaLite
  323. For an overview on performing distance queries, please refer to
  324. the :ref:`distance queries introduction <distance-queries>`.
  325. Distance lookups take the following form::
  326. <field>__<distance lookup>=(<geometry>, <distance value>[, 'spheroid'])
  327. The value passed into a distance lookup is a tuple; the first two
  328. values are mandatory, and are the geometry to calculate distances to,
  329. and a distance value (either a number in units of the field or a
  330. :class:`~django.contrib.gis.measure.Distance` object). On every
  331. distance lookup but :lookup:`dwithin`, an optional
  332. third element, ``'spheroid'``, may be included to tell GeoDjango
  333. to use the more accurate spheroid distance calculation functions on
  334. fields with a geodetic coordinate system (e.g., ``ST_Distance_Spheroid``
  335. would be used instead of ``ST_Distance_Sphere``).
  336. .. fieldlookup:: distance_gt
  337. distance_gt
  338. -----------
  339. Returns models where the distance to the geometry field from the lookup
  340. geometry is greater than the given distance value.
  341. Example::
  342. Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))
  343. ========== ===============================================
  344. Backend SQL Equivalent
  345. ========== ===============================================
  346. PostGIS ``ST_Distance(poly, geom) > 5``
  347. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5``
  348. SpatiaLite ``Distance(poly, geom) > 5``
  349. ========== ===============================================
  350. .. fieldlookup:: distance_gte
  351. distance_gte
  352. ------------
  353. Returns models where the distance to the geometry field from the lookup
  354. geometry is greater than or equal to the given distance value.
  355. Example::
  356. Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))
  357. ========== ================================================
  358. Backend SQL Equivalent
  359. ========== ================================================
  360. PostGIS ``ST_Distance(poly, geom) >= 5``
  361. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5``
  362. SpatiaLite ``Distance(poly, geom) >= 5``
  363. ========== ================================================
  364. .. fieldlookup:: distance_lt
  365. distance_lt
  366. -----------
  367. Returns models where the distance to the geometry field from the lookup
  368. geometry is less than the given distance value.
  369. Example::
  370. Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))
  371. ========== ===============================================
  372. Backend SQL Equivalent
  373. ========== ===============================================
  374. PostGIS ``ST_Distance(poly, geom) < 5``
  375. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5``
  376. SpatiaLite ``Distance(poly, geom) < 5``
  377. ========== ===============================================
  378. .. fieldlookup:: distance_lte
  379. distance_lte
  380. ------------
  381. Returns models where the distance to the geometry field from the lookup
  382. geometry is less than or equal to the given distance value.
  383. Example::
  384. Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))
  385. ========== ================================================
  386. Backend SQL Equivalent
  387. ========== ================================================
  388. PostGIS ``ST_Distance(poly, geom) <= 5``
  389. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5``
  390. SpatiaLite ``Distance(poly, geom) <= 5``
  391. ========== ================================================
  392. .. fieldlookup:: dwithin
  393. dwithin
  394. -------
  395. Returns models where the distance to the geometry field from the lookup
  396. geometry are within the given distance from one another. Note that you can only
  397. provide :class:`~django.contrib.gis.measure.Distance` objects if the targeted
  398. geometries are in a projected system. For geographic geometries, you should use
  399. units of the geometry field (e.g. degrees for ``WGS84``) .
  400. Example::
  401. Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
  402. ========== ======================================
  403. Backend SQL Equivalent
  404. ========== ======================================
  405. PostGIS ``ST_DWithin(poly, geom, 5)``
  406. Oracle ``SDO_WITHIN_DISTANCE(poly, geom, 5)``
  407. ========== ======================================
  408. .. note::
  409. This lookup is not available on SpatiaLite.
  410. .. _geoqueryset-methods:
  411. ``GeoQuerySet`` Methods
  412. =======================
  413. .. deprecated:: 1.9
  414. Using ``GeoQuerySet`` methods is now deprecated in favor of the new
  415. :doc:`functions`. Albeit a little more verbose, they are much more powerful
  416. in how it is possible to combine them to build more complex queries.
  417. ``GeoQuerySet`` methods specify that a spatial operation be performed
  418. on each spatial operation on each geographic
  419. field in the queryset and store its output in a new attribute on the model
  420. (which is generally the name of the ``GeoQuerySet`` method).
  421. There are also aggregate ``GeoQuerySet`` methods which return a single value
  422. instead of a queryset. This section will describe the API and availability
  423. of every ``GeoQuerySet`` method available in GeoDjango.
  424. .. note::
  425. What methods are available depend on your spatial backend. See
  426. the :ref:`compatibility table <database-functions-compatibility>`
  427. for more details.
  428. With a few exceptions, the following keyword arguments may be used with all
  429. ``GeoQuerySet`` methods:
  430. ===================== =====================================================
  431. Keyword Argument Description
  432. ===================== =====================================================
  433. ``field_name`` By default, ``GeoQuerySet`` methods use the first
  434. geographic field encountered in the model. This
  435. keyword should be used to specify another
  436. geographic field (e.g., ``field_name='point2'``)
  437. when there are multiple geographic fields in a model.
  438. On PostGIS, the ``field_name`` keyword may also be
  439. used on geometry fields in models that are related
  440. via a ``ForeignKey`` relation (e.g.,
  441. ``field_name='related__point'``).
  442. ``model_att`` By default, ``GeoQuerySet`` methods typically attach
  443. their output in an attribute with the same name as
  444. the ``GeoQuerySet`` method. Setting this keyword
  445. with the desired attribute name will override this
  446. default behavior. For example,
  447. ``qs = Zipcode.objects.centroid(model_att='c')`` will
  448. attach the centroid of the ``Zipcode`` geometry field
  449. in a ``c`` attribute on every model rather than in a
  450. ``centroid`` attribute.
  451. This keyword is required if
  452. a method name clashes with an existing
  453. ``GeoQuerySet`` method -- if you wanted to use the
  454. ``area()`` method on model with a ``PolygonField``
  455. named ``area``, for example.
  456. ===================== =====================================================
  457. Measurement
  458. -----------
  459. *Availability*: PostGIS, Oracle, SpatiaLite
  460. ``area``
  461. ~~~~~~~~
  462. .. method:: GeoQuerySet.area(**kwargs)
  463. .. deprecated:: 1.9
  464. Use the :class:`~django.contrib.gis.db.models.functions.Area` function
  465. instead.
  466. Returns the area of the geographic field in an ``area`` attribute on
  467. each element of this GeoQuerySet.
  468. ``distance``
  469. ~~~~~~~~~~~~
  470. .. method:: GeoQuerySet.distance(geom, **kwargs)
  471. .. deprecated:: 1.9
  472. Use the :class:`~django.contrib.gis.db.models.functions.Distance` function
  473. instead.
  474. This method takes a geometry as a parameter, and attaches a ``distance``
  475. attribute to every model in the returned queryset that contains the
  476. distance (as a :class:`~django.contrib.gis.measure.Distance` object) to the given geometry.
  477. In the following example (taken from the `GeoDjango distance tests`__),
  478. the distance from the `Tasmanian`__ city of Hobart to every other
  479. :class:`PointField` in the ``AustraliaCity`` queryset is calculated::
  480. >>> pnt = AustraliaCity.objects.get(name='Hobart').point
  481. >>> for city in AustraliaCity.objects.distance(pnt): print(city.name, city.distance)
  482. Wollongong 990071.220408 m
  483. Shellharbour 972804.613941 m
  484. Thirroul 1002334.36351 m
  485. Mittagong 975691.632637 m
  486. Batemans Bay 834342.185561 m
  487. Canberra 598140.268959 m
  488. Melbourne 575337.765042 m
  489. Sydney 1056978.87363 m
  490. Hobart 0.0 m
  491. Adelaide 1162031.83522 m
  492. Hillsdale 1049200.46122 m
  493. .. note::
  494. Because the ``distance`` attribute is a
  495. :class:`~django.contrib.gis.measure.Distance` object, you can easily express
  496. the value in the units of your choice. For example, ``city.distance.mi`` is
  497. the distance value in miles and ``city.distance.km`` is the distance value
  498. in kilometers. See :doc:`measure` for usage details and the list of
  499. :ref:`supported_units`.
  500. __ https://github.com/django/django/blob/master/tests/gis_tests/distapp/models.py
  501. __ https://en.wikipedia.org/wiki/Tasmania
  502. ``length``
  503. ~~~~~~~~~~
  504. .. method:: GeoQuerySet.length(**kwargs)
  505. .. deprecated:: 1.9
  506. Use the :class:`~django.contrib.gis.db.models.functions.Length` function
  507. instead.
  508. Returns the length of the geometry field in a ``length`` attribute
  509. (a :class:`~django.contrib.gis.measure.Distance` object) on each model in
  510. the queryset.
  511. ``perimeter``
  512. ~~~~~~~~~~~~~
  513. .. method:: GeoQuerySet.perimeter(**kwargs)
  514. .. deprecated:: 1.9
  515. Use the :class:`~django.contrib.gis.db.models.functions.Perimeter` function
  516. instead.
  517. Returns the perimeter of the geometry field in a ``perimeter`` attribute
  518. (a :class:`~django.contrib.gis.measure.Distance` object) on each model in
  519. the queryset.
  520. Geometry Relationships
  521. ----------------------
  522. The following methods take no arguments, and attach geometry objects
  523. each element of the :class:`GeoQuerySet` that is the result of relationship
  524. function evaluated on the geometry field.
  525. ``centroid``
  526. ~~~~~~~~~~~~
  527. .. method:: GeoQuerySet.centroid(**kwargs)
  528. .. deprecated:: 1.9
  529. Use the :class:`~django.contrib.gis.db.models.functions.Centroid` function
  530. instead.
  531. *Availability*: PostGIS, Oracle, SpatiaLite
  532. Returns the ``centroid`` value for the geographic field in a ``centroid``
  533. attribute on each element of the ``GeoQuerySet``.
  534. ``envelope``
  535. ~~~~~~~~~~~~
  536. .. method:: GeoQuerySet.envelope(**kwargs)
  537. .. deprecated:: 1.9
  538. Use the :class:`~django.contrib.gis.db.models.functions.Envelope` function
  539. instead.
  540. *Availability*: PostGIS, SpatiaLite
  541. Returns a geometry representing the bounding box of the geometry field in
  542. an ``envelope`` attribute on each element of the ``GeoQuerySet``.
  543. ``point_on_surface``
  544. ~~~~~~~~~~~~~~~~~~~~
  545. .. method:: GeoQuerySet.point_on_surface(**kwargs)
  546. .. deprecated:: 1.9
  547. Use the :class:`~django.contrib.gis.db.models.functions.PointOnSurface`
  548. function instead.
  549. *Availability*: PostGIS, Oracle, SpatiaLite
  550. Returns a Point geometry guaranteed to lie on the surface of the
  551. geometry field in a ``point_on_surface`` attribute on each element
  552. of the queryset; otherwise sets with None.
  553. Geometry Editors
  554. ----------------
  555. ``force_rhr``
  556. ~~~~~~~~~~~~~
  557. .. method:: GeoQuerySet.force_rhr(**kwargs)
  558. .. deprecated:: 1.9
  559. Use the :class:`~django.contrib.gis.db.models.functions.ForceRHR` function
  560. instead.
  561. *Availability*: PostGIS
  562. Returns a modified version of the polygon/multipolygon in which all
  563. of the vertices follow the Right-Hand-Rule, and attaches as a
  564. ``force_rhr`` attribute on each element of the queryset.
  565. ``reverse_geom``
  566. ~~~~~~~~~~~~~~~~
  567. .. method:: GeoQuerySet.reverse_geom(**kwargs)
  568. .. deprecated:: 1.9
  569. Use the :class:`~django.contrib.gis.db.models.functions.Reverse` function
  570. instead.
  571. *Availability*: PostGIS, Oracle
  572. Reverse the coordinate order of the geometry field, and attaches as a
  573. ``reverse`` attribute on each element of the queryset.
  574. ``scale``
  575. ~~~~~~~~~
  576. .. method:: GeoQuerySet.scale(x, y, z=0.0, **kwargs)
  577. .. deprecated:: 1.9
  578. Use the :class:`~django.contrib.gis.db.models.functions.Scale` function
  579. instead.
  580. *Availability*: PostGIS, SpatiaLite
  581. ``snap_to_grid``
  582. ~~~~~~~~~~~~~~~~
  583. .. method:: GeoQuerySet.snap_to_grid(*args, **kwargs)
  584. .. deprecated:: 1.9
  585. Use the :class:`~django.contrib.gis.db.models.functions.SnapToGrid` function
  586. instead.
  587. Snap all points of the input geometry to the grid. How the
  588. geometry is snapped to the grid depends on how many numeric
  589. (either float, integer, or long) arguments are given.
  590. =================== =====================================================
  591. Number of Arguments Description
  592. =================== =====================================================
  593. 1 A single size to snap bot the X and Y grids to.
  594. 2 X and Y sizes to snap the grid to.
  595. 4 X, Y sizes and the corresponding X, Y origins.
  596. =================== =====================================================
  597. ``transform``
  598. ~~~~~~~~~~~~~
  599. .. method:: GeoQuerySet.transform(srid=4326, **kwargs)
  600. .. deprecated:: 1.9
  601. Use the :class:`~django.contrib.gis.db.models.functions.Transform` function
  602. instead.
  603. *Availability*: PostGIS, Oracle, SpatiaLite
  604. The ``transform`` method transforms the geometry field of a model to the spatial
  605. reference system specified by the ``srid`` parameter. If no ``srid`` is given,
  606. then 4326 (WGS84) is used by default.
  607. .. note::
  608. Unlike other ``GeoQuerySet`` methods, ``transform`` stores its output
  609. "in-place". In other words, no new attribute for the transformed
  610. geometry is placed on the models.
  611. .. note::
  612. What spatial reference system an integer SRID corresponds to may depend on
  613. the spatial database used. In other words, the SRID numbers used for Oracle
  614. are not necessarily the same as those used by PostGIS.
  615. Example::
  616. >>> qs = Zipcode.objects.all().transform() # Transforms to WGS84
  617. >>> qs = Zipcode.objects.all().transform(32140) # Transforming to "NAD83 / Texas South Central"
  618. >>> print(qs[0].poly.srid)
  619. 32140
  620. >>> print(qs[0].poly)
  621. POLYGON ((234055.1698884720099159 4937796.9232223574072123 ...
  622. ``translate``
  623. ~~~~~~~~~~~~~
  624. .. method:: GeoQuerySet.translate(x, y, z=0.0, **kwargs)
  625. .. deprecated:: 1.9
  626. Use the :class:`~django.contrib.gis.db.models.functions.Translate` function
  627. instead.
  628. *Availability*: PostGIS, SpatiaLite
  629. Translates the geometry field to a new location using the given numeric
  630. parameters as offsets.
  631. Geometry Operations
  632. -------------------
  633. *Availability*: PostGIS, Oracle, SpatiaLite
  634. The following methods all take a geometry as a parameter and attach a geometry
  635. to each element of the ``GeoQuerySet`` that is the result of the operation.
  636. ``difference``
  637. ~~~~~~~~~~~~~~
  638. .. method:: GeoQuerySet.difference(geom)
  639. .. deprecated:: 1.9
  640. Use the :class:`~django.contrib.gis.db.models.functions.Difference` function
  641. instead.
  642. Returns the spatial difference of the geographic field with the given
  643. geometry in a ``difference`` attribute on each element of the
  644. ``GeoQuerySet``.
  645. ``intersection``
  646. ~~~~~~~~~~~~~~~~
  647. .. method:: GeoQuerySet.intersection(geom)
  648. .. deprecated:: 1.9
  649. Use the :class:`~django.contrib.gis.db.models.functions.Intersection`
  650. function instead.
  651. Returns the spatial intersection of the geographic field with the
  652. given geometry in an ``intersection`` attribute on each element of the
  653. ``GeoQuerySet``.
  654. ``sym_difference``
  655. ~~~~~~~~~~~~~~~~~~
  656. .. method:: GeoQuerySet.sym_difference(geom)
  657. .. deprecated:: 1.9
  658. Use the :class:`~django.contrib.gis.db.models.functions.SymDifference`
  659. function instead.
  660. Returns the symmetric difference of the geographic field with the
  661. given geometry in a ``sym_difference`` attribute on each element of the
  662. ``GeoQuerySet``.
  663. ``union``
  664. ~~~~~~~~~
  665. .. method:: GeoQuerySet.union(geom)
  666. .. deprecated:: 1.9
  667. Use the :class:`~django.contrib.gis.db.models.functions.Union` function
  668. instead.
  669. Returns the union of the geographic field with the given
  670. geometry in an ``union`` attribute on each element of the
  671. ``GeoQuerySet``.
  672. Geometry Output
  673. ---------------
  674. The following ``GeoQuerySet`` methods will return an attribute that has the value
  675. of the geometry field in each model converted to the requested output format.
  676. ``geohash``
  677. ~~~~~~~~~~~
  678. .. method:: GeoQuerySet.geohash(precision=20, **kwargs)
  679. .. deprecated:: 1.9
  680. Use the :class:`~django.contrib.gis.db.models.functions.GeoHash` function
  681. instead.
  682. Attaches a ``geohash`` attribute to every model the queryset
  683. containing the `GeoHash`__ representation of the geometry.
  684. __ http://geohash.org/
  685. ``geojson``
  686. ~~~~~~~~~~~
  687. .. method:: GeoQuerySet.geojson(**kwargs)
  688. .. deprecated:: 1.9
  689. Use the :class:`~django.contrib.gis.db.models.functions.AsGeoJSON` function
  690. instead.
  691. *Availability*: PostGIS, SpatiaLite
  692. Attaches a ``geojson`` attribute to every model in the queryset that contains the
  693. `GeoJSON`__ representation of the geometry.
  694. ===================== =====================================================
  695. Keyword Argument Description
  696. ===================== =====================================================
  697. ``precision`` It may be used to specify the number of significant
  698. digits for the coordinates in the GeoJSON
  699. representation -- the default value is 8.
  700. ``crs`` Set this to ``True`` if you want the coordinate
  701. reference system to be included in the returned
  702. GeoJSON.
  703. ``bbox`` Set this to ``True`` if you want the bounding box
  704. to be included in the returned GeoJSON.
  705. ===================== =====================================================
  706. __ http://geojson.org/
  707. ``gml``
  708. ~~~~~~~
  709. .. method:: GeoQuerySet.gml(**kwargs)
  710. .. deprecated:: 1.9
  711. Use the :class:`~django.contrib.gis.db.models.functions.AsGML` function
  712. instead.
  713. *Availability*: PostGIS, Oracle, SpatiaLite
  714. Attaches a ``gml`` attribute to every model in the queryset that contains the
  715. `Geographic Markup Language (GML)`__ representation of the geometry.
  716. Example::
  717. >>> qs = Zipcode.objects.all().gml()
  718. >>> print(qs[0].gml)
  719. <gml:Polygon srsName="EPSG:4326"><gml:OuterBoundaryIs>-147.78711,70.245363 ... -147.78711,70.245363</gml:OuterBoundaryIs></gml:Polygon>
  720. ===================== =====================================================
  721. Keyword Argument Description
  722. ===================== =====================================================
  723. ``precision`` This keyword is for PostGIS only. It may be used
  724. to specify the number of significant digits for the
  725. coordinates in the GML representation -- the default
  726. value is 8.
  727. ``version`` This keyword is for PostGIS only. It may be used to
  728. specify the GML version used, and may only be values
  729. of 2 or 3. The default value is 2.
  730. ===================== =====================================================
  731. __ https://en.wikipedia.org/wiki/Geography_Markup_Language
  732. ``kml``
  733. ~~~~~~~
  734. .. method:: GeoQuerySet.kml(**kwargs)
  735. .. deprecated:: 1.9
  736. Use the :class:`~django.contrib.gis.db.models.functions.AsKML` function
  737. instead.
  738. *Availability*: PostGIS, SpatiaLite
  739. Attaches a ``kml`` attribute to every model in the queryset that contains the
  740. `Keyhole Markup Language (KML)`__ representation of the geometry fields. It
  741. should be noted that the contents of the KML are transformed to WGS84 if
  742. necessary.
  743. Example::
  744. >>> qs = Zipcode.objects.all().kml()
  745. >>> print(qs[0].kml)
  746. <Polygon><outerBoundaryIs><LinearRing><coordinates>-103.04135,36.217596,0 ... -103.04135,36.217596,0</coordinates></LinearRing></outerBoundaryIs></Polygon>
  747. ===================== =====================================================
  748. Keyword Argument Description
  749. ===================== =====================================================
  750. ``precision`` This keyword may be used to specify the number of
  751. significant digits for the coordinates in the KML
  752. representation -- the default value is 8.
  753. ===================== =====================================================
  754. __ https://developers.google.com/kml/documentation/
  755. ``svg``
  756. ~~~~~~~
  757. .. method:: GeoQuerySet.svg(**kwargs)
  758. .. deprecated:: 1.9
  759. Use the :class:`~django.contrib.gis.db.models.functions.AsSVG` function
  760. instead.
  761. *Availability*: PostGIS, SpatiaLite
  762. Attaches a ``svg`` attribute to every model in the queryset that contains
  763. the `Scalable Vector Graphics (SVG)`__ path data of the geometry fields.
  764. ===================== =====================================================
  765. Keyword Argument Description
  766. ===================== =====================================================
  767. ``relative`` If set to ``True``, the path data will be implemented
  768. in terms of relative moves. Defaults to ``False``,
  769. meaning that absolute moves are used instead.
  770. ``precision`` This keyword may be used to specify the number of
  771. significant digits for the coordinates in the SVG
  772. representation -- the default value is 8.
  773. ===================== =====================================================
  774. __ http://www.w3.org/Graphics/SVG/
  775. Miscellaneous
  776. -------------
  777. ``mem_size``
  778. ~~~~~~~~~~~~
  779. .. method:: GeoQuerySet.mem_size(**kwargs)
  780. .. deprecated:: 1.9
  781. Use the :class:`~django.contrib.gis.db.models.functions.MemSize` function
  782. instead.
  783. *Availability*: PostGIS
  784. Returns the memory size (number of bytes) that the geometry field takes
  785. in a ``mem_size`` attribute on each element of the ``GeoQuerySet``.
  786. ``num_geom``
  787. ~~~~~~~~~~~~
  788. .. method:: GeoQuerySet.num_geom(**kwargs)
  789. .. deprecated:: 1.9
  790. Use the :class:`~django.contrib.gis.db.models.functions.NumGeometries`
  791. function instead.
  792. *Availability*: PostGIS, Oracle, SpatiaLite
  793. Returns the number of geometries in a ``num_geom`` attribute on
  794. each element of the ``GeoQuerySet`` if the geometry field is a
  795. collection (e.g., a ``GEOMETRYCOLLECTION`` or ``MULTI*`` field);
  796. otherwise sets with ``None``.
  797. ``num_points``
  798. ~~~~~~~~~~~~~~
  799. .. method:: GeoQuerySet.num_points(**kwargs)
  800. .. deprecated:: 1.9
  801. Use the :class:`~django.contrib.gis.db.models.functions.NumPoints` function
  802. instead.
  803. *Availability*: PostGIS, Oracle, SpatiaLite
  804. Returns the number of points in the first linestring in the
  805. geometry field in a ``num_points`` attribute on each element of
  806. the ``GeoQuerySet``; otherwise sets with ``None``.
  807. Spatial Aggregates
  808. ==================
  809. Aggregate Methods
  810. -----------------
  811. .. deprecated:: 1.8
  812. Aggregate methods are now deprecated. Prefer using their function-based
  813. equivalents.
  814. ``collect``
  815. ~~~~~~~~~~~
  816. .. method:: GeoQuerySet.collect(**kwargs)
  817. .. deprecated:: 1.8
  818. Use the :class:`Collect` aggregate instead.
  819. Shortcut for ``aggregate(Collect(<field>))``.
  820. ``extent``
  821. ~~~~~~~~~~
  822. .. method:: GeoQuerySet.extent(**kwargs)
  823. .. deprecated:: 1.8
  824. Use the :class:`Extent` aggregate instead.
  825. Shortcut for ``aggregate(Extent(<field>))``.
  826. ``extent3d``
  827. ~~~~~~~~~~~~
  828. .. method:: GeoQuerySet.extent3d(**kwargs)
  829. .. deprecated:: 1.8
  830. Use the :class:`Extent` aggregate instead.
  831. Shortcut for ``aggregate(Extent3D(<field>))``.
  832. ``make_line``
  833. ~~~~~~~~~~~~~
  834. .. method:: GeoQuerySet.make_line(**kwargs)
  835. .. deprecated:: 1.8
  836. Use the :class:`MakeLine` aggregate instead.
  837. Shortcut for ``aggregate(MakeLine(<field>))``.
  838. ``unionagg``
  839. ~~~~~~~~~~~~
  840. .. method:: GeoQuerySet.unionagg(**kwargs)
  841. .. deprecated:: 1.8
  842. Use the :class:`Union` aggregate instead.
  843. Shortcut for ``aggregate(Union(<field>))``.
  844. Aggregate Functions
  845. -------------------
  846. Django provides some GIS-specific aggregate functions. For details on how to
  847. use these aggregate functions, see :doc:`the topic guide on aggregation
  848. </topics/db/aggregation>`.
  849. ===================== =====================================================
  850. Keyword Argument Description
  851. ===================== =====================================================
  852. ``tolerance`` This keyword is for Oracle only. It is for the
  853. tolerance value used by the ``SDOAGGRTYPE``
  854. procedure; the `Oracle documentation`__ has more
  855. details.
  856. ===================== =====================================================
  857. __ http://docs.oracle.com/html/B14255_01/sdo_intro.htm#sthref150
  858. Example::
  859. >>> from django.contrib.gis.db.models import Extent, Union
  860. >>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))
  861. ``Collect``
  862. ~~~~~~~~~~~
  863. .. class:: Collect(geo_field)
  864. *Availability*: PostGIS, Spatialite (≥3.0)
  865. Returns a ``GEOMETRYCOLLECTION`` or a ``MULTI`` geometry object from the geometry
  866. column. This is analogous to a simplified version of the :class:`Union`
  867. aggregate, except it can be several orders of magnitude faster than performing
  868. a union because it simply rolls up geometries into a collection or multi object,
  869. not caring about dissolving boundaries.
  870. ``Extent``
  871. ~~~~~~~~~~
  872. .. class:: Extent(geo_field)
  873. *Availability*: PostGIS, Oracle, Spatialite (≥3.0)
  874. Returns the extent of all ``geo_field`` in the ``QuerySet`` as a four-tuple,
  875. comprising the lower left coordinate and the upper right coordinate.
  876. Example::
  877. >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
  878. >>> print(qs[poly__extent])
  879. (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
  880. ``Extent3D``
  881. ~~~~~~~~~~~~
  882. .. class:: Extent3D(geo_field)
  883. *Availability*: PostGIS
  884. Returns the 3D extent of all ``geo_field`` in the ``QuerySet`` as a six-tuple,
  885. comprising the lower left coordinate and upper right coordinate (each with x, y,
  886. and z coordinates).
  887. Example::
  888. >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent3D('poly'))
  889. >>> print(qs[poly__extent3d])
  890. (-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)
  891. ``MakeLine``
  892. ~~~~~~~~~~~~
  893. .. class:: MakeLine(geo_field)
  894. *Availability*: PostGIS
  895. Returns a ``LineString`` constructed from the point field geometries in the
  896. ``QuerySet``. Currently, ordering the queryset has no effect.
  897. Example::
  898. >>> print(City.objects.filter(name__in=('Houston', 'Dallas')
  899. ... ).aggregate(MakeLine('poly'))[poly__makeline]
  900. LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)
  901. ``Union``
  902. ~~~~~~~~~
  903. .. class:: Union(geo_field)
  904. *Availability*: PostGIS, Oracle, SpatiaLite
  905. This method returns a :class:`~django.contrib.gis.geos.GEOSGeometry` object
  906. comprising the union of every geometry in the queryset. Please note that use of
  907. ``Union`` is processor intensive and may take a significant amount of time on
  908. large querysets.
  909. .. note::
  910. If the computation time for using this method is too expensive, consider
  911. using :class:`Collect` instead.
  912. Example::
  913. >>> u = Zipcode.objects.aggregate(Union(poly)) # This may take a long time.
  914. >>> u = Zipcode.objects.filter(poly__within=bbox).aggregate(Union(poly)) # A more sensible approach.
  915. .. rubric:: Footnotes
  916. .. [#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).
  917. .. [#fnsdorelate] *See* `SDO_RELATE documentation <http://docs.oracle.com/cd/B19306_01/appdev.102/b14255/sdo_operat.htm#sthref845>`_, from Ch. 11 of the Oracle Spatial User's Guide and Manual.
  918. .. [#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).
  919. .. [#fncontainsproperly] Refer to the PostGIS ``ST_ContainsProperly`` `documentation <http://postgis.net/docs/manual-1.5/ST_ContainsProperly.html>`_ for more details.