geoquerysets.txt 35 KB

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