geoquerysets.txt 35 KB

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