geoquerysets.txt 35 KB

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