geoquerysets.txt 39 KB

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