geoquerysets.txt 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341
  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, a
  330. :class:`~django.contrib.gis.measure.Distance` object, or a `query expression
  331. <ref/models/expressions>`).
  332. With PostGIS, on every distance lookup but :lookup:`dwithin`, an optional
  333. third element, ``'spheroid'``, may be included to tell GeoDjango
  334. to use the more accurate spheroid distance calculation functions on
  335. fields with a geodetic coordinate system (e.g., ``ST_Distance_Spheroid``
  336. would be used instead of ``ST_Distance_Sphere``). The simpler ``ST_Distance``
  337. function is used with projected coordinate systems.
  338. .. versionadded:: 1.10
  339. The ability to pass an expression as the distance value was added.
  340. .. fieldlookup:: distance_gt
  341. ``distance_gt``
  342. ---------------
  343. Returns models where the distance to the geometry field from the lookup
  344. geometry is greater than the given distance value.
  345. Example::
  346. Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))
  347. ========== ==================================================
  348. Backend SQL Equivalent
  349. ========== ==================================================
  350. PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) > 5``
  351. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5``
  352. SpatiaLite ``Distance(poly, geom) > 5``
  353. ========== ==================================================
  354. .. fieldlookup:: distance_gte
  355. ``distance_gte``
  356. ----------------
  357. Returns models where the distance to the geometry field from the lookup
  358. geometry is greater than or equal to the given distance value.
  359. Example::
  360. Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))
  361. ========== ===================================================
  362. Backend SQL Equivalent
  363. ========== ===================================================
  364. PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) >= 5``
  365. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5``
  366. SpatiaLite ``Distance(poly, geom) >= 5``
  367. ========== ===================================================
  368. .. fieldlookup:: distance_lt
  369. ``distance_lt``
  370. ---------------
  371. Returns models where the distance to the geometry field from the lookup
  372. geometry is less than the given distance value.
  373. Example::
  374. Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))
  375. ========== ==================================================
  376. Backend SQL Equivalent
  377. ========== ==================================================
  378. PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) < 5``
  379. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5``
  380. SpatiaLite ``Distance(poly, geom) < 5``
  381. ========== ==================================================
  382. .. fieldlookup:: distance_lte
  383. ``distance_lte``
  384. ----------------
  385. Returns models where the distance to the geometry field from the lookup
  386. geometry is less than or equal to the given distance value.
  387. Example::
  388. Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))
  389. ========== ===================================================
  390. Backend SQL Equivalent
  391. ========== ===================================================
  392. PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) <= 5``
  393. Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5``
  394. SpatiaLite ``Distance(poly, geom) <= 5``
  395. ========== ===================================================
  396. .. fieldlookup:: dwithin
  397. ``dwithin``
  398. -----------
  399. Returns models where the distance to the geometry field from the lookup
  400. geometry are within the given distance from one another. Note that you can only
  401. provide :class:`~django.contrib.gis.measure.Distance` objects if the targeted
  402. geometries are in a projected system. For geographic geometries, you should use
  403. units of the geometry field (e.g. degrees for ``WGS84``) .
  404. Example::
  405. Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
  406. ========== ======================================
  407. Backend SQL Equivalent
  408. ========== ======================================
  409. PostGIS ``ST_DWithin(poly, geom, 5)``
  410. Oracle ``SDO_WITHIN_DISTANCE(poly, geom, 5)``
  411. ========== ======================================
  412. .. note::
  413. This lookup is not available on SpatiaLite.
  414. .. _geoqueryset-methods:
  415. ``GeoQuerySet`` Methods
  416. =======================
  417. .. deprecated:: 1.9
  418. Using ``GeoQuerySet`` methods is now deprecated in favor of the new
  419. :doc:`functions`. Albeit a little more verbose, they are much more powerful
  420. in how it is possible to combine them to build more complex queries.
  421. ``GeoQuerySet`` methods specify that a spatial operation be performed
  422. on each spatial operation on each geographic
  423. field in the queryset and store its output in a new attribute on the model
  424. (which is generally the name of the ``GeoQuerySet`` method).
  425. There are also aggregate ``GeoQuerySet`` methods which return a single value
  426. instead of a queryset. This section will describe the API and availability
  427. of every ``GeoQuerySet`` method available in GeoDjango.
  428. .. note::
  429. What methods are available depend on your spatial backend. See
  430. the :ref:`compatibility table <database-functions-compatibility>`
  431. for more details.
  432. With a few exceptions, the following keyword arguments may be used with all
  433. ``GeoQuerySet`` methods:
  434. ===================== =====================================================
  435. Keyword Argument Description
  436. ===================== =====================================================
  437. ``field_name`` By default, ``GeoQuerySet`` methods use the first
  438. geographic field encountered in the model. This
  439. keyword should be used to specify another
  440. geographic field (e.g., ``field_name='point2'``)
  441. when there are multiple geographic fields in a model.
  442. On PostGIS, the ``field_name`` keyword may also be
  443. used on geometry fields in models that are related
  444. via a ``ForeignKey`` relation (e.g.,
  445. ``field_name='related__point'``).
  446. ``model_att`` By default, ``GeoQuerySet`` methods typically attach
  447. their output in an attribute with the same name as
  448. the ``GeoQuerySet`` method. Setting this keyword
  449. with the desired attribute name will override this
  450. default behavior. For example,
  451. ``qs = Zipcode.objects.centroid(model_att='c')`` will
  452. attach the centroid of the ``Zipcode`` geometry field
  453. in a ``c`` attribute on every model rather than in a
  454. ``centroid`` attribute.
  455. This keyword is required if
  456. a method name clashes with an existing
  457. ``GeoQuerySet`` method -- if you wanted to use the
  458. ``area()`` method on model with a ``PolygonField``
  459. named ``area``, for example.
  460. ===================== =====================================================
  461. Measurement
  462. -----------
  463. *Availability*: PostGIS, Oracle, SpatiaLite
  464. ``area``
  465. ~~~~~~~~
  466. .. method:: GeoQuerySet.area(**kwargs)
  467. .. deprecated:: 1.9
  468. Use the :class:`~django.contrib.gis.db.models.functions.Area` function
  469. instead.
  470. Returns the area of the geographic field in an ``area`` attribute on
  471. each element of this GeoQuerySet.
  472. ``distance``
  473. ~~~~~~~~~~~~
  474. .. method:: GeoQuerySet.distance(geom, **kwargs)
  475. .. deprecated:: 1.9
  476. Use the :class:`~django.contrib.gis.db.models.functions.Distance` function
  477. instead.
  478. This method takes a geometry as a parameter, and attaches a ``distance``
  479. attribute to every model in the returned queryset that contains the
  480. distance (as a :class:`~django.contrib.gis.measure.Distance` object) to the given geometry.
  481. In the following example (taken from the `GeoDjango distance tests`__),
  482. the distance from the `Tasmanian`__ city of Hobart to every other
  483. :class:`PointField` in the ``AustraliaCity`` queryset is calculated::
  484. >>> pnt = AustraliaCity.objects.get(name='Hobart').point
  485. >>> for city in AustraliaCity.objects.distance(pnt): print(city.name, city.distance)
  486. Wollongong 990071.220408 m
  487. Shellharbour 972804.613941 m
  488. Thirroul 1002334.36351 m
  489. Mittagong 975691.632637 m
  490. Batemans Bay 834342.185561 m
  491. Canberra 598140.268959 m
  492. Melbourne 575337.765042 m
  493. Sydney 1056978.87363 m
  494. Hobart 0.0 m
  495. Adelaide 1162031.83522 m
  496. Hillsdale 1049200.46122 m
  497. .. note::
  498. Because the ``distance`` attribute is a
  499. :class:`~django.contrib.gis.measure.Distance` object, you can easily express
  500. the value in the units of your choice. For example, ``city.distance.mi`` is
  501. the distance value in miles and ``city.distance.km`` is the distance value
  502. in kilometers. See :doc:`measure` for usage details and the list of
  503. :ref:`supported_units`.
  504. __ https://github.com/django/django/blob/master/tests/gis_tests/distapp/models.py
  505. __ https://en.wikipedia.org/wiki/Tasmania
  506. ``length``
  507. ~~~~~~~~~~
  508. .. method:: GeoQuerySet.length(**kwargs)
  509. .. deprecated:: 1.9
  510. Use the :class:`~django.contrib.gis.db.models.functions.Length` function
  511. instead.
  512. Returns the length of the geometry field in a ``length`` attribute
  513. (a :class:`~django.contrib.gis.measure.Distance` object) on each model in
  514. the queryset.
  515. ``perimeter``
  516. ~~~~~~~~~~~~~
  517. .. method:: GeoQuerySet.perimeter(**kwargs)
  518. .. deprecated:: 1.9
  519. Use the :class:`~django.contrib.gis.db.models.functions.Perimeter` function
  520. instead.
  521. Returns the perimeter of the geometry field in a ``perimeter`` attribute
  522. (a :class:`~django.contrib.gis.measure.Distance` object) on each model in
  523. the queryset.
  524. Geometry Relationships
  525. ----------------------
  526. The following methods take no arguments, and attach geometry objects
  527. each element of the :class:`GeoQuerySet` that is the result of relationship
  528. function evaluated on the geometry field.
  529. ``centroid``
  530. ~~~~~~~~~~~~
  531. .. method:: GeoQuerySet.centroid(**kwargs)
  532. .. deprecated:: 1.9
  533. Use the :class:`~django.contrib.gis.db.models.functions.Centroid` function
  534. instead.
  535. *Availability*: PostGIS, Oracle, SpatiaLite
  536. Returns the ``centroid`` value for the geographic field in a ``centroid``
  537. attribute on each element of the ``GeoQuerySet``.
  538. ``envelope``
  539. ~~~~~~~~~~~~
  540. .. method:: GeoQuerySet.envelope(**kwargs)
  541. .. deprecated:: 1.9
  542. Use the :class:`~django.contrib.gis.db.models.functions.Envelope` function
  543. instead.
  544. *Availability*: PostGIS, SpatiaLite
  545. Returns a geometry representing the bounding box of the geometry field in
  546. an ``envelope`` attribute on each element of the ``GeoQuerySet``.
  547. ``point_on_surface``
  548. ~~~~~~~~~~~~~~~~~~~~
  549. .. method:: GeoQuerySet.point_on_surface(**kwargs)
  550. .. deprecated:: 1.9
  551. Use the :class:`~django.contrib.gis.db.models.functions.PointOnSurface`
  552. function instead.
  553. *Availability*: PostGIS, Oracle, SpatiaLite
  554. Returns a Point geometry guaranteed to lie on the surface of the
  555. geometry field in a ``point_on_surface`` attribute on each element
  556. of the queryset; otherwise sets with None.
  557. Geometry Editors
  558. ----------------
  559. ``force_rhr``
  560. ~~~~~~~~~~~~~
  561. .. method:: GeoQuerySet.force_rhr(**kwargs)
  562. .. deprecated:: 1.9
  563. Use the :class:`~django.contrib.gis.db.models.functions.ForceRHR` function
  564. instead.
  565. *Availability*: PostGIS
  566. Returns a modified version of the polygon/multipolygon in which all
  567. of the vertices follow the Right-Hand-Rule, and attaches as a
  568. ``force_rhr`` attribute on each element of the queryset.
  569. ``reverse_geom``
  570. ~~~~~~~~~~~~~~~~
  571. .. method:: GeoQuerySet.reverse_geom(**kwargs)
  572. .. deprecated:: 1.9
  573. Use the :class:`~django.contrib.gis.db.models.functions.Reverse` function
  574. instead.
  575. *Availability*: PostGIS, Oracle
  576. Reverse the coordinate order of the geometry field, and attaches as a
  577. ``reverse`` attribute on each element of the queryset.
  578. ``scale``
  579. ~~~~~~~~~
  580. .. method:: GeoQuerySet.scale(x, y, z=0.0, **kwargs)
  581. .. deprecated:: 1.9
  582. Use the :class:`~django.contrib.gis.db.models.functions.Scale` function
  583. instead.
  584. *Availability*: PostGIS, SpatiaLite
  585. ``snap_to_grid``
  586. ~~~~~~~~~~~~~~~~
  587. .. method:: GeoQuerySet.snap_to_grid(*args, **kwargs)
  588. .. deprecated:: 1.9
  589. Use the :class:`~django.contrib.gis.db.models.functions.SnapToGrid` function
  590. instead.
  591. Snap all points of the input geometry to the grid. How the
  592. geometry is snapped to the grid depends on how many numeric
  593. (either float, integer, or long) arguments are given.
  594. =================== =====================================================
  595. Number of Arguments Description
  596. =================== =====================================================
  597. 1 A single size to snap bot the X and Y grids to.
  598. 2 X and Y sizes to snap the grid to.
  599. 4 X, Y sizes and the corresponding X, Y origins.
  600. =================== =====================================================
  601. ``transform``
  602. ~~~~~~~~~~~~~
  603. .. method:: GeoQuerySet.transform(srid=4326, **kwargs)
  604. .. deprecated:: 1.9
  605. Use the :class:`~django.contrib.gis.db.models.functions.Transform` function
  606. instead.
  607. *Availability*: PostGIS, Oracle, SpatiaLite
  608. The ``transform`` method transforms the geometry field of a model to the spatial
  609. reference system specified by the ``srid`` parameter. If no ``srid`` is given,
  610. then 4326 (WGS84) is used by default.
  611. .. note::
  612. Unlike other ``GeoQuerySet`` methods, ``transform`` stores its output
  613. "in-place". In other words, no new attribute for the transformed
  614. geometry is placed on the models.
  615. .. note::
  616. What spatial reference system an integer SRID corresponds to may depend on
  617. the spatial database used. In other words, the SRID numbers used for Oracle
  618. are not necessarily the same as those used by PostGIS.
  619. Example::
  620. >>> qs = Zipcode.objects.all().transform() # Transforms to WGS84
  621. >>> qs = Zipcode.objects.all().transform(32140) # Transforming to "NAD83 / Texas South Central"
  622. >>> print(qs[0].poly.srid)
  623. 32140
  624. >>> print(qs[0].poly)
  625. POLYGON ((234055.1698884720099159 4937796.9232223574072123 ...
  626. ``translate``
  627. ~~~~~~~~~~~~~
  628. .. method:: GeoQuerySet.translate(x, y, z=0.0, **kwargs)
  629. .. deprecated:: 1.9
  630. Use the :class:`~django.contrib.gis.db.models.functions.Translate` function
  631. instead.
  632. *Availability*: PostGIS, SpatiaLite
  633. Translates the geometry field to a new location using the given numeric
  634. parameters as offsets.
  635. Geometry Operations
  636. -------------------
  637. *Availability*: PostGIS, Oracle, SpatiaLite
  638. The following methods all take a geometry as a parameter and attach a geometry
  639. to each element of the ``GeoQuerySet`` that is the result of the operation.
  640. ``difference``
  641. ~~~~~~~~~~~~~~
  642. .. method:: GeoQuerySet.difference(geom)
  643. .. deprecated:: 1.9
  644. Use the :class:`~django.contrib.gis.db.models.functions.Difference` function
  645. instead.
  646. Returns the spatial difference of the geographic field with the given
  647. geometry in a ``difference`` attribute on each element of the
  648. ``GeoQuerySet``.
  649. ``intersection``
  650. ~~~~~~~~~~~~~~~~
  651. .. method:: GeoQuerySet.intersection(geom)
  652. .. deprecated:: 1.9
  653. Use the :class:`~django.contrib.gis.db.models.functions.Intersection`
  654. function instead.
  655. Returns the spatial intersection of the geographic field with the
  656. given geometry in an ``intersection`` attribute on each element of the
  657. ``GeoQuerySet``.
  658. ``sym_difference``
  659. ~~~~~~~~~~~~~~~~~~
  660. .. method:: GeoQuerySet.sym_difference(geom)
  661. .. deprecated:: 1.9
  662. Use the :class:`~django.contrib.gis.db.models.functions.SymDifference`
  663. function instead.
  664. Returns the symmetric difference of the geographic field with the
  665. given geometry in a ``sym_difference`` attribute on each element of the
  666. ``GeoQuerySet``.
  667. ``union``
  668. ~~~~~~~~~
  669. .. method:: GeoQuerySet.union(geom)
  670. .. deprecated:: 1.9
  671. Use the :class:`~django.contrib.gis.db.models.functions.Union` function
  672. instead.
  673. Returns the union of the geographic field with the given
  674. geometry in an ``union`` attribute on each element of the
  675. ``GeoQuerySet``.
  676. Geometry Output
  677. ---------------
  678. The following ``GeoQuerySet`` methods will return an attribute that has the value
  679. of the geometry field in each model converted to the requested output format.
  680. ``geohash``
  681. ~~~~~~~~~~~
  682. .. method:: GeoQuerySet.geohash(precision=20, **kwargs)
  683. .. deprecated:: 1.9
  684. Use the :class:`~django.contrib.gis.db.models.functions.GeoHash` function
  685. instead.
  686. Attaches a ``geohash`` attribute to every model the queryset
  687. containing the `GeoHash`__ representation of the geometry.
  688. __ http://geohash.org/
  689. ``geojson``
  690. ~~~~~~~~~~~
  691. .. method:: GeoQuerySet.geojson(**kwargs)
  692. .. deprecated:: 1.9
  693. Use the :class:`~django.contrib.gis.db.models.functions.AsGeoJSON` function
  694. instead.
  695. *Availability*: PostGIS, SpatiaLite
  696. Attaches a ``geojson`` attribute to every model in the queryset that contains the
  697. `GeoJSON`__ representation of the geometry.
  698. ===================== =====================================================
  699. Keyword Argument Description
  700. ===================== =====================================================
  701. ``precision`` It may be used to specify the number of significant
  702. digits for the coordinates in the GeoJSON
  703. representation -- the default value is 8.
  704. ``crs`` Set this to ``True`` if you want the coordinate
  705. reference system to be included in the returned
  706. GeoJSON.
  707. ``bbox`` Set this to ``True`` if you want the bounding box
  708. to be included in the returned GeoJSON.
  709. ===================== =====================================================
  710. __ http://geojson.org/
  711. ``gml``
  712. ~~~~~~~
  713. .. method:: GeoQuerySet.gml(**kwargs)
  714. .. deprecated:: 1.9
  715. Use the :class:`~django.contrib.gis.db.models.functions.AsGML` function
  716. instead.
  717. *Availability*: PostGIS, Oracle, SpatiaLite
  718. Attaches a ``gml`` attribute to every model in the queryset that contains the
  719. `Geographic Markup Language (GML)`__ representation of the geometry.
  720. Example::
  721. >>> qs = Zipcode.objects.all().gml()
  722. >>> print(qs[0].gml)
  723. <gml:Polygon srsName="EPSG:4326"><gml:OuterBoundaryIs>-147.78711,70.245363 ... -147.78711,70.245363</gml:OuterBoundaryIs></gml:Polygon>
  724. ===================== =====================================================
  725. Keyword Argument Description
  726. ===================== =====================================================
  727. ``precision`` This keyword is for PostGIS only. It may be used
  728. to specify the number of significant digits for the
  729. coordinates in the GML representation -- the default
  730. value is 8.
  731. ``version`` This keyword is for PostGIS only. It may be used to
  732. specify the GML version used, and may only be values
  733. of 2 or 3. The default value is 2.
  734. ===================== =====================================================
  735. __ https://en.wikipedia.org/wiki/Geography_Markup_Language
  736. ``kml``
  737. ~~~~~~~
  738. .. method:: GeoQuerySet.kml(**kwargs)
  739. .. deprecated:: 1.9
  740. Use the :class:`~django.contrib.gis.db.models.functions.AsKML` function
  741. instead.
  742. *Availability*: PostGIS, SpatiaLite
  743. Attaches a ``kml`` attribute to every model in the queryset that contains the
  744. `Keyhole Markup Language (KML)`__ representation of the geometry fields. It
  745. should be noted that the contents of the KML are transformed to WGS84 if
  746. necessary.
  747. Example::
  748. >>> qs = Zipcode.objects.all().kml()
  749. >>> print(qs[0].kml)
  750. <Polygon><outerBoundaryIs><LinearRing><coordinates>-103.04135,36.217596,0 ... -103.04135,36.217596,0</coordinates></LinearRing></outerBoundaryIs></Polygon>
  751. ===================== =====================================================
  752. Keyword Argument Description
  753. ===================== =====================================================
  754. ``precision`` This keyword may be used to specify the number of
  755. significant digits for the coordinates in the KML
  756. representation -- the default value is 8.
  757. ===================== =====================================================
  758. __ https://developers.google.com/kml/documentation/
  759. ``svg``
  760. ~~~~~~~
  761. .. method:: GeoQuerySet.svg(**kwargs)
  762. .. deprecated:: 1.9
  763. Use the :class:`~django.contrib.gis.db.models.functions.AsSVG` function
  764. instead.
  765. *Availability*: PostGIS, SpatiaLite
  766. Attaches a ``svg`` attribute to every model in the queryset that contains
  767. the `Scalable Vector Graphics (SVG)`__ path data of the geometry fields.
  768. ===================== =====================================================
  769. Keyword Argument Description
  770. ===================== =====================================================
  771. ``relative`` If set to ``True``, the path data will be implemented
  772. in terms of relative moves. Defaults to ``False``,
  773. meaning that absolute moves are used instead.
  774. ``precision`` This keyword may be used to specify the number of
  775. significant digits for the coordinates in the SVG
  776. representation -- the default value is 8.
  777. ===================== =====================================================
  778. __ http://www.w3.org/Graphics/SVG/
  779. Miscellaneous
  780. -------------
  781. ``mem_size``
  782. ~~~~~~~~~~~~
  783. .. method:: GeoQuerySet.mem_size(**kwargs)
  784. .. deprecated:: 1.9
  785. Use the :class:`~django.contrib.gis.db.models.functions.MemSize` function
  786. instead.
  787. *Availability*: PostGIS
  788. Returns the memory size (number of bytes) that the geometry field takes
  789. in a ``mem_size`` attribute on each element of the ``GeoQuerySet``.
  790. ``num_geom``
  791. ~~~~~~~~~~~~
  792. .. method:: GeoQuerySet.num_geom(**kwargs)
  793. .. deprecated:: 1.9
  794. Use the :class:`~django.contrib.gis.db.models.functions.NumGeometries`
  795. function instead.
  796. *Availability*: PostGIS, Oracle, SpatiaLite
  797. Returns the number of geometries in a ``num_geom`` attribute on
  798. each element of the ``GeoQuerySet`` if the geometry field is a
  799. collection (e.g., a ``GEOMETRYCOLLECTION`` or ``MULTI*`` field);
  800. otherwise sets with ``None``.
  801. ``num_points``
  802. ~~~~~~~~~~~~~~
  803. .. method:: GeoQuerySet.num_points(**kwargs)
  804. .. deprecated:: 1.9
  805. Use the :class:`~django.contrib.gis.db.models.functions.NumPoints` function
  806. instead.
  807. *Availability*: PostGIS, Oracle, SpatiaLite
  808. Returns the number of points in the first linestring in the
  809. geometry field in a ``num_points`` attribute on each element of
  810. the ``GeoQuerySet``; otherwise sets with ``None``.
  811. Aggregate Functions
  812. -------------------
  813. Django provides some GIS-specific aggregate functions. For details on how to
  814. use these aggregate functions, see :doc:`the topic guide on aggregation
  815. </topics/db/aggregation>`.
  816. ===================== =====================================================
  817. Keyword Argument Description
  818. ===================== =====================================================
  819. ``tolerance`` This keyword is for Oracle only. It is for the
  820. tolerance value used by the ``SDOAGGRTYPE``
  821. procedure; the `Oracle documentation`__ has more
  822. details.
  823. ===================== =====================================================
  824. __ https://docs.oracle.com/html/B14255_01/sdo_intro.htm#sthref150
  825. Example::
  826. >>> from django.contrib.gis.db.models import Extent, Union
  827. >>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))
  828. ``Collect``
  829. ~~~~~~~~~~~
  830. .. class:: Collect(geo_field)
  831. *Availability*: PostGIS, Spatialite
  832. Returns a ``GEOMETRYCOLLECTION`` or a ``MULTI`` geometry object from the geometry
  833. column. This is analogous to a simplified version of the :class:`Union`
  834. aggregate, except it can be several orders of magnitude faster than performing
  835. a union because it simply rolls up geometries into a collection or multi object,
  836. not caring about dissolving boundaries.
  837. ``Extent``
  838. ~~~~~~~~~~
  839. .. class:: Extent(geo_field)
  840. *Availability*: PostGIS, Oracle, Spatialite
  841. Returns the extent of all ``geo_field`` in the ``QuerySet`` as a four-tuple,
  842. comprising the lower left coordinate and the upper right coordinate.
  843. Example::
  844. >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
  845. >>> print(qs['poly__extent'])
  846. (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
  847. ``Extent3D``
  848. ~~~~~~~~~~~~
  849. .. class:: Extent3D(geo_field)
  850. *Availability*: PostGIS
  851. Returns the 3D extent of all ``geo_field`` in the ``QuerySet`` as a six-tuple,
  852. comprising the lower left coordinate and upper right coordinate (each with x, y,
  853. and z coordinates).
  854. Example::
  855. >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent3D('poly'))
  856. >>> print(qs['poly__extent3d'])
  857. (-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)
  858. ``MakeLine``
  859. ~~~~~~~~~~~~
  860. .. class:: MakeLine(geo_field)
  861. *Availability*: PostGIS, SpatiaLite
  862. Returns a ``LineString`` constructed from the point field geometries in the
  863. ``QuerySet``. Currently, ordering the queryset has no effect.
  864. .. versionchanged:: 1.10
  865. SpatiaLite support was added.
  866. Example::
  867. >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(MakeLine('poly'))
  868. >>> print(qs['poly__makeline'])
  869. LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)
  870. ``Union``
  871. ~~~~~~~~~
  872. .. class:: Union(geo_field)
  873. *Availability*: PostGIS, Oracle, SpatiaLite
  874. This method returns a :class:`~django.contrib.gis.geos.GEOSGeometry` object
  875. comprising the union of every geometry in the queryset. Please note that use of
  876. ``Union`` is processor intensive and may take a significant amount of time on
  877. large querysets.
  878. .. note::
  879. If the computation time for using this method is too expensive, consider
  880. using :class:`Collect` instead.
  881. Example::
  882. >>> u = Zipcode.objects.aggregate(Union(poly)) # This may take a long time.
  883. >>> u = Zipcode.objects.filter(poly__within=bbox).aggregate(Union(poly)) # A more sensible approach.
  884. .. rubric:: Footnotes
  885. .. [#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).
  886. .. [#fnsdorelate] *See* `SDO_RELATE documentation <https://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.
  887. .. [#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).
  888. .. [#fncontainsproperly] Refer to the PostGIS ``ST_ContainsProperly`` `documentation <http://postgis.net/docs/manual-1.5/ST_ContainsProperly.html>`_ for more details.