123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419 |
- =========================
- GeoQuerySet API Reference
- =========================
- .. currentmodule:: django.contrib.gis.db.models
- .. class:: GeoQuerySet(model=None)
- .. _spatial-lookups:
- Spatial Lookups
- ===============
- The spatial lookups in this section are available for :class:`GeometryField`
- and :class:`RasterField`.
- For an introduction, see the :ref:`spatial lookups introduction
- <spatial-lookups-intro>`. For an overview of what lookups are
- compatible with a particular spatial backend, refer to the
- :ref:`spatial lookup compatibility table <spatial-lookup-compatibility>`.
- .. versionchanged:: 1.10
- Spatial lookups now support raster input.
- Lookups with rasters
- --------------------
- All examples in the reference below are given for geometry fields and inputs,
- but the lookups can be used the same way with rasters on both sides. Whenever
- a lookup doesn't support raster input, the input is automatically
- converted to a geometry where necessary using the `ST_Polygon
- <http://postgis.net/docs/RT_ST_Polygon.html>`_ function. See also the
- :ref:`introduction to raster lookups <spatial-lookup-raster>`.
- The database operators used by the lookups can be divided into three categories:
- - Native raster support ``N``: the operator accepts rasters natively on both
- sides of the lookup, and raster input can be mixed with geometry inputs.
- - Bilateral raster support ``B``: the operator supports rasters only if both
- sides of the lookup receive raster inputs. Raster data is automatically
- converted to geometries for mixed lookups.
- - Geometry conversion support ``C``. The lookup does not have native raster
- support, all raster data is automatically converted to geometries.
- The examples below show the SQL equivalent for the lookups in the different
- types of raster support. The same pattern applies to all spatial lookups.
- ==== ============================== =======================================================
- Case Lookup SQL Equivalent
- ==== ============================== =======================================================
- N, B ``rast__contains=rst`` ``ST_Contains(rast, rst)``
- N, B ``rast__1__contains=(rst, 2)`` ``ST_Contains(rast, 1, rst, 2)``
- B, C ``rast__contains=geom`` ``ST_Contains(ST_Polygon(rast), geom)``
- B, C ``rast__1__contains=geom`` ``ST_Contains(ST_Polygon(rast, 1), geom)``
- B, C ``poly__contains=rst`` ``ST_Contains(poly, ST_Polygon(rst))``
- B, C ``poly__contains=(rst, 1)`` ``ST_Contains(poly, ST_Polygon(rst, 1))``
- C ``rast__crosses=rst`` ``ST_Crosses(ST_Polygon(rast), ST_Polygon(rst))``
- C ``rast__1__crosses=(rst, 2)`` ``ST_Crosses(ST_Polygon(rast, 1), ST_Polygon(rst, 2))``
- C ``rast__crosses=geom`` ``ST_Crosses(ST_Polygon(rast), geom)``
- C ``poly__crosses=rst`` ``ST_Crosses(poly, ST_Polygon(rst))``
- ==== ============================== =======================================================
- Spatial lookups with rasters are only supported for PostGIS backends
- (denominated as PGRaster in this section).
- .. fieldlookup:: bbcontains
- ``bbcontains``
- --------------
- *Availability*: PostGIS, MySQL, SpatiaLite, PGRaster (Native)
- Tests if the geometry or raster field's bounding box completely contains the
- lookup geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__bbcontains=geom)
- ========== ==========================
- Backend SQL Equivalent
- ========== ==========================
- PostGIS ``poly ~ geom``
- MySQL ``MBRContains(poly, geom)``
- SpatiaLite ``MbrContains(poly, geom)``
- ========== ==========================
- .. fieldlookup:: bboverlaps
- ``bboverlaps``
- --------------
- *Availability*: PostGIS, MySQL, SpatiaLite, PGRaster (Native)
- Tests if the geometry field's bounding box overlaps the lookup geometry's
- bounding box.
- Example::
- Zipcode.objects.filter(poly__bboverlaps=geom)
- ========== ==========================
- Backend SQL Equivalent
- ========== ==========================
- PostGIS ``poly && geom``
- MySQL ``MBROverlaps(poly, geom)``
- SpatiaLite ``MbrOverlaps(poly, geom)``
- ========== ==========================
- .. fieldlookup:: contained
- ``contained``
- -------------
- *Availability*: PostGIS, MySQL, SpatiaLite, PGRaster (Native)
- Tests if the geometry field's bounding box is completely contained by the
- lookup geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__contained=geom)
- ========== ==========================
- Backend SQL Equivalent
- ========== ==========================
- PostGIS ``poly @ geom``
- MySQL ``MBRWithin(poly, geom)``
- SpatiaLite ``MbrWithin(poly, geom)``
- ========== ==========================
- .. fieldlookup:: gis-contains
- ``contains``
- ------------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)
- Tests if the geometry field spatially contains the lookup geometry.
- Example::
- Zipcode.objects.filter(poly__contains=geom)
- ========== ============================
- Backend SQL Equivalent
- ========== ============================
- PostGIS ``ST_Contains(poly, geom)``
- Oracle ``SDO_CONTAINS(poly, geom)``
- MySQL ``MBRContains(poly, geom)``
- SpatiaLite ``Contains(poly, geom)``
- ========== ============================
- .. fieldlookup:: contains_properly
- ``contains_properly``
- ---------------------
- *Availability*: PostGIS, PGRaster (Bilateral)
- Returns true if the lookup geometry intersects the interior of the
- geometry field, but not the boundary (or exterior). [#fncontainsproperly]_
- Example::
- Zipcode.objects.filter(poly__contains_properly=geom)
- ========== ===================================
- Backend SQL Equivalent
- ========== ===================================
- PostGIS ``ST_ContainsProperly(poly, geom)``
- ========== ===================================
- .. fieldlookup:: coveredby
- ``coveredby``
- -------------
- *Availability*: PostGIS, Oracle, PGRaster (Bilateral)
- Tests if no point in the geometry field is outside the lookup geometry.
- [#fncovers]_
- Example::
- Zipcode.objects.filter(poly__coveredby=geom)
- ========== =============================
- Backend SQL Equivalent
- ========== =============================
- PostGIS ``ST_CoveredBy(poly, geom)``
- Oracle ``SDO_COVEREDBY(poly, geom)``
- ========== =============================
- .. fieldlookup:: covers
- ``covers``
- ----------
- *Availability*: PostGIS, Oracle, PGRaster (Bilateral)
- Tests if no point in the lookup geometry is outside the geometry field.
- [#fncovers]_
- Example::
- Zipcode.objects.filter(poly__covers=geom)
- ========== ==========================
- Backend SQL Equivalent
- ========== ==========================
- PostGIS ``ST_Covers(poly, geom)``
- Oracle ``SDO_COVERS(poly, geom)``
- ========== ==========================
- .. fieldlookup:: crosses
- ``crosses``
- -----------
- *Availability*: PostGIS, SpatiaLite, PGRaster (Conversion)
- Tests if the geometry field spatially crosses the lookup geometry.
- Example::
- Zipcode.objects.filter(poly__crosses=geom)
- ========== ==========================
- Backend SQL Equivalent
- ========== ==========================
- PostGIS ``ST_Crosses(poly, geom)``
- SpatiaLite ``Crosses(poly, geom)``
- ========== ==========================
- .. fieldlookup:: disjoint
- ``disjoint``
- ------------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)
- Tests if the geometry field is spatially disjoint from the lookup geometry.
- Example::
- Zipcode.objects.filter(poly__disjoint=geom)
- ========== =================================================
- Backend SQL Equivalent
- ========== =================================================
- PostGIS ``ST_Disjoint(poly, geom)``
- Oracle ``SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)``
- MySQL ``MBRDisjoint(poly, geom)``
- SpatiaLite ``Disjoint(poly, geom)``
- ========== =================================================
- .. fieldlookup:: equals
- ``equals``
- ----------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite, PGRaster (Conversion)
- .. fieldlookup:: exact
- .. fieldlookup:: same_as
- ``exact``, ``same_as``
- ----------------------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)
- .. fieldlookup:: intersects
- ``intersects``
- --------------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)
- Tests if the geometry field spatially intersects the lookup geometry.
- Example::
- Zipcode.objects.filter(poly__intersects=geom)
- ========== =================================================
- Backend SQL Equivalent
- ========== =================================================
- PostGIS ``ST_Intersects(poly, geom)``
- Oracle ``SDO_OVERLAPBDYINTERSECT(poly, geom)``
- MySQL ``MBRIntersects(poly, geom)``
- SpatiaLite ``Intersects(poly, geom)``
- ========== =================================================
- .. fieldlookup:: isvalid
- ``isvalid``
- -----------
- .. versionadded:: 1.10
- *Availability*: PostGIS
- Tests if the geometry is valid.
- Example::
- Zipcode.objects.filter(poly__isvalid=True)
- PostGIS equivalent::
- SELECT ... WHERE ST_IsValid(poly)
- .. fieldlookup:: overlaps
- ``overlaps``
- ------------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)
- .. fieldlookup:: relate
- ``relate``
- ----------
- *Availability*: PostGIS, Oracle, SpatiaLite, PGRaster (Conversion)
- Tests if the geometry field is spatially related to the lookup geometry by
- the values given in the given pattern. This lookup requires a tuple parameter,
- ``(geom, pattern)``; the form of ``pattern`` will depend on the spatial backend:
- PostGIS & SpatiaLite
- ~~~~~~~~~~~~~~~~~~~~
- On these spatial backends the intersection pattern is a string comprising
- nine characters, which define intersections between the interior, boundary,
- and exterior of the geometry field and the lookup geometry.
- The intersection pattern matrix may only use the following characters:
- ``1``, ``2``, ``T``, ``F``, or ``*``. This lookup type allows users to "fine tune"
- a specific geometric relationship consistent with the DE-9IM model. [#fnde9im]_
- Geometry example::
- # A tuple lookup parameter is used to specify the geometry and
- # the intersection pattern (the pattern here is for 'contains').
- Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))
- PostGIS SQL equivalent::
- SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')
- SpatiaLite SQL equivalent::
- SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')
- Raster example::
- Zipcode.objects.filter(poly__relate=(rast, 1, 'T*T***FF*'))
- Zipcode.objects.filter(rast__2__relate=(rast, 1, 'T*T***FF*'))
- PostGIS SQL equivalent::
- SELECT ... WHERE ST_Relate(poly, ST_Polygon(rast, 1), 'T*T***FF*')
- SELECT ... WHERE ST_Relate(ST_Polygon(rast, 2), ST_Polygon(rast, 1), 'T*T***FF*')
- Oracle
- ~~~~~~
- Here the relation pattern is comprised of at least one of the nine relation
- strings: ``TOUCH``, ``OVERLAPBDYDISJOINT``, ``OVERLAPBDYINTERSECT``,
- ``EQUAL``, ``INSIDE``, ``COVEREDBY``, ``CONTAINS``, ``COVERS``, ``ON``, and
- ``ANYINTERACT``. Multiple strings may be combined with the logical Boolean
- operator OR, for example, ``'inside+touch'``. [#fnsdorelate]_ The relation
- strings are case-insensitive.
- Example::
- Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))
- Oracle SQL equivalent::
- SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')
- .. fieldlookup:: touches
- ``touches``
- -----------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite
- Tests if the geometry field spatially touches the lookup geometry.
- Example::
- Zipcode.objects.filter(poly__touches=geom)
- ========== ==========================
- Backend SQL Equivalent
- ========== ==========================
- PostGIS ``ST_Touches(poly, geom)``
- MySQL ``MBRTouches(poly, geom)``
- Oracle ``SDO_TOUCH(poly, geom)``
- SpatiaLite ``Touches(poly, geom)``
- ========== ==========================
- .. fieldlookup:: within
- ``within``
- ----------
- *Availability*: PostGIS, Oracle, MySQL, SpatiaLite, PGRaster (Bilateral)
- Tests if the geometry field is spatially within the lookup geometry.
- Example::
- Zipcode.objects.filter(poly__within=geom)
- ========== ==========================
- Backend SQL Equivalent
- ========== ==========================
- PostGIS ``ST_Within(poly, geom)``
- MySQL ``MBRWithin(poly, geom)``
- Oracle ``SDO_INSIDE(poly, geom)``
- SpatiaLite ``Within(poly, geom)``
- ========== ==========================
- .. fieldlookup:: left
- ``left``
- --------
- *Availability*: PostGIS, PGRaster (Conversion)
- Tests if the geometry field's bounding box is strictly to the left of the
- lookup geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__left=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly << geom
- .. fieldlookup:: right
- ``right``
- ---------
- *Availability*: PostGIS, PGRaster (Conversion)
- Tests if the geometry field's bounding box is strictly to the right of the
- lookup geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__right=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly >> geom
- .. fieldlookup:: overlaps_left
- ``overlaps_left``
- -----------------
- *Availability*: PostGIS, PGRaster (Bilateral)
- Tests if the geometry field's bounding box overlaps or is to the left of the lookup
- geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__overlaps_left=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly &< geom
- .. fieldlookup:: overlaps_right
- ``overlaps_right``
- ------------------
- *Availability*: PostGIS, PGRaster (Bilateral)
- Tests if the geometry field's bounding box overlaps or is to the right of the lookup
- geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__overlaps_right=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly &> geom
- .. fieldlookup:: overlaps_above
- ``overlaps_above``
- ------------------
- *Availability*: PostGIS, PGRaster (Conversion)
- Tests if the geometry field's bounding box overlaps or is above the lookup
- geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__overlaps_above=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly |&> geom
- .. fieldlookup:: overlaps_below
- ``overlaps_below``
- ------------------
- *Availability*: PostGIS, PGRaster (Conversion)
- Tests if the geometry field's bounding box overlaps or is below the lookup
- geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__overlaps_below=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly &<| geom
- .. fieldlookup:: strictly_above
- ``strictly_above``
- ------------------
- *Availability*: PostGIS, PGRaster (Conversion)
- Tests if the geometry field's bounding box is strictly above the lookup
- geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__strictly_above=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly |>> geom
- .. fieldlookup:: strictly_below
- ``strictly_below``
- ------------------
- *Availability*: PostGIS, PGRaster (Conversion)
- Tests if the geometry field's bounding box is strictly below the lookup
- geometry's bounding box.
- Example::
- Zipcode.objects.filter(poly__strictly_below=geom)
- PostGIS equivalent::
- SELECT ... WHERE poly <<| geom
- .. _distance-lookups:
- Distance Lookups
- ================
- *Availability*: PostGIS, Oracle, SpatiaLite, PGRaster (Native)
- For an overview on performing distance queries, please refer to
- the :ref:`distance queries introduction <distance-queries>`.
- Distance lookups take the following form::
- <field>__<distance lookup>=(<geometry/raster>, <distance value>[, 'spheroid'])
- <field>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])
- <field>__<band_index>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])
- The value passed into a distance lookup is a tuple; the first two
- values are mandatory, and are the geometry to calculate distances to,
- and a distance value (either a number in units of the field, a
- :class:`~django.contrib.gis.measure.Distance` object, or a `query expression
- <ref/models/expressions>`). To pass a band index to the lookup, use a 3-tuple
- where the second entry is the band index.
- With PostGIS, on every distance lookup but :lookup:`dwithin`, an optional
- element, ``'spheroid'``, may be included to tell GeoDjango to use the more
- accurate spheroid distance calculation functions on fields with a geodetic
- coordinate system (e.g., ``ST_Distance_Spheroid`` would be used instead of
- ``ST_Distance_Sphere``). The simpler ``ST_Distance`` function is used with
- projected coordinate systems. Rasters are converted to geometries for spheroid
- based lookups.
- .. versionadded:: 1.10
- The ability to pass an expression as the distance value was added.
- .. fieldlookup:: distance_gt
- ``distance_gt``
- ---------------
- Returns models where the distance to the geometry field from the lookup
- geometry is greater than the given distance value.
- Example::
- Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))
- ========== ==================================================
- Backend SQL Equivalent
- ========== ==================================================
- PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) > 5``
- Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5``
- SpatiaLite ``Distance(poly, geom) > 5``
- ========== ==================================================
- .. fieldlookup:: distance_gte
- ``distance_gte``
- ----------------
- Returns models where the distance to the geometry field from the lookup
- geometry is greater than or equal to the given distance value.
- Example::
- Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))
- ========== ===================================================
- Backend SQL Equivalent
- ========== ===================================================
- PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) >= 5``
- Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5``
- SpatiaLite ``Distance(poly, geom) >= 5``
- ========== ===================================================
- .. fieldlookup:: distance_lt
- ``distance_lt``
- ---------------
- Returns models where the distance to the geometry field from the lookup
- geometry is less than the given distance value.
- Example::
- Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))
- ========== ==================================================
- Backend SQL Equivalent
- ========== ==================================================
- PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) < 5``
- Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5``
- SpatiaLite ``Distance(poly, geom) < 5``
- ========== ==================================================
- .. fieldlookup:: distance_lte
- ``distance_lte``
- ----------------
- Returns models where the distance to the geometry field from the lookup
- geometry is less than or equal to the given distance value.
- Example::
- Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))
- ========== ===================================================
- Backend SQL Equivalent
- ========== ===================================================
- PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) <= 5``
- Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5``
- SpatiaLite ``Distance(poly, geom) <= 5``
- ========== ===================================================
- .. fieldlookup:: dwithin
- ``dwithin``
- -----------
- Returns models where the distance to the geometry field from the lookup
- geometry are within the given distance from one another. Note that you can only
- provide :class:`~django.contrib.gis.measure.Distance` objects if the targeted
- geometries are in a projected system. For geographic geometries, you should use
- units of the geometry field (e.g. degrees for ``WGS84``) .
- Example::
- Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
- ========== ======================================
- Backend SQL Equivalent
- ========== ======================================
- PostGIS ``ST_DWithin(poly, geom, 5)``
- Oracle ``SDO_WITHIN_DISTANCE(poly, geom, 5)``
- ========== ======================================
- .. note::
- This lookup is not available on SpatiaLite.
- .. _geoqueryset-methods:
- ``GeoQuerySet`` Methods
- =======================
- .. deprecated:: 1.9
- Using ``GeoQuerySet`` methods is now deprecated in favor of the new
- :doc:`functions`. Albeit a little more verbose, they are much more powerful
- in how it is possible to combine them to build more complex queries.
- ``GeoQuerySet`` methods specify that a spatial operation be performed
- on each spatial operation on each geographic
- field in the queryset and store its output in a new attribute on the model
- (which is generally the name of the ``GeoQuerySet`` method).
- There are also aggregate ``GeoQuerySet`` methods which return a single value
- instead of a queryset. This section will describe the API and availability
- of every ``GeoQuerySet`` method available in GeoDjango.
- .. note::
- What methods are available depend on your spatial backend. See
- the :ref:`compatibility table <database-functions-compatibility>`
- for more details.
- With a few exceptions, the following keyword arguments may be used with all
- ``GeoQuerySet`` methods:
- ===================== =====================================================
- Keyword Argument Description
- ===================== =====================================================
- ``field_name`` By default, ``GeoQuerySet`` methods use the first
- geographic field encountered in the model. This
- keyword should be used to specify another
- geographic field (e.g., ``field_name='point2'``)
- when there are multiple geographic fields in a model.
- On PostGIS, the ``field_name`` keyword may also be
- used on geometry fields in models that are related
- via a ``ForeignKey`` relation (e.g.,
- ``field_name='related__point'``).
- ``model_att`` By default, ``GeoQuerySet`` methods typically attach
- their output in an attribute with the same name as
- the ``GeoQuerySet`` method. Setting this keyword
- with the desired attribute name will override this
- default behavior. For example,
- ``qs = Zipcode.objects.centroid(model_att='c')`` will
- attach the centroid of the ``Zipcode`` geometry field
- in a ``c`` attribute on every model rather than in a
- ``centroid`` attribute.
- This keyword is required if
- a method name clashes with an existing
- ``GeoQuerySet`` method -- if you wanted to use the
- ``area()`` method on model with a ``PolygonField``
- named ``area``, for example.
- ===================== =====================================================
- Measurement
- -----------
- *Availability*: PostGIS, Oracle, SpatiaLite
- ``area``
- ~~~~~~~~
- .. method:: GeoQuerySet.area(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Area` function
- instead.
- Returns the area of the geographic field in an ``area`` attribute on
- each element of this GeoQuerySet.
- ``distance``
- ~~~~~~~~~~~~
- .. method:: GeoQuerySet.distance(geom, **kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Distance` function
- instead.
- This method takes a geometry as a parameter, and attaches a ``distance``
- attribute to every model in the returned queryset that contains the
- distance (as a :class:`~django.contrib.gis.measure.Distance` object) to the given geometry.
- In the following example (taken from the `GeoDjango distance tests`__),
- the distance from the `Tasmanian`__ city of Hobart to every other
- :class:`PointField` in the ``AustraliaCity`` queryset is calculated::
- >>> pnt = AustraliaCity.objects.get(name='Hobart').point
- >>> for city in AustraliaCity.objects.distance(pnt): print(city.name, city.distance)
- Wollongong 990071.220408 m
- Shellharbour 972804.613941 m
- Thirroul 1002334.36351 m
- Mittagong 975691.632637 m
- Batemans Bay 834342.185561 m
- Canberra 598140.268959 m
- Melbourne 575337.765042 m
- Sydney 1056978.87363 m
- Hobart 0.0 m
- Adelaide 1162031.83522 m
- Hillsdale 1049200.46122 m
- .. note::
- Because the ``distance`` attribute is a
- :class:`~django.contrib.gis.measure.Distance` object, you can easily express
- the value in the units of your choice. For example, ``city.distance.mi`` is
- the distance value in miles and ``city.distance.km`` is the distance value
- in kilometers. See :doc:`measure` for usage details and the list of
- :ref:`supported_units`.
- __ https://github.com/django/django/blob/master/tests/gis_tests/distapp/models.py
- __ https://en.wikipedia.org/wiki/Tasmania
- ``length``
- ~~~~~~~~~~
- .. method:: GeoQuerySet.length(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Length` function
- instead.
- Returns the length of the geometry field in a ``length`` attribute
- (a :class:`~django.contrib.gis.measure.Distance` object) on each model in
- the queryset.
- ``perimeter``
- ~~~~~~~~~~~~~
- .. method:: GeoQuerySet.perimeter(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Perimeter` function
- instead.
- Returns the perimeter of the geometry field in a ``perimeter`` attribute
- (a :class:`~django.contrib.gis.measure.Distance` object) on each model in
- the queryset.
- Geometry Relationships
- ----------------------
- The following methods take no arguments, and attach geometry objects
- each element of the :class:`GeoQuerySet` that is the result of relationship
- function evaluated on the geometry field.
- ``centroid``
- ~~~~~~~~~~~~
- .. method:: GeoQuerySet.centroid(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Centroid` function
- instead.
- *Availability*: PostGIS, Oracle, SpatiaLite
- Returns the ``centroid`` value for the geographic field in a ``centroid``
- attribute on each element of the ``GeoQuerySet``.
- ``envelope``
- ~~~~~~~~~~~~
- .. method:: GeoQuerySet.envelope(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Envelope` function
- instead.
- *Availability*: PostGIS, SpatiaLite
- Returns a geometry representing the bounding box of the geometry field in
- an ``envelope`` attribute on each element of the ``GeoQuerySet``.
- ``point_on_surface``
- ~~~~~~~~~~~~~~~~~~~~
- .. method:: GeoQuerySet.point_on_surface(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.PointOnSurface`
- function instead.
- *Availability*: PostGIS, Oracle, SpatiaLite
- Returns a Point geometry guaranteed to lie on the surface of the
- geometry field in a ``point_on_surface`` attribute on each element
- of the queryset; otherwise sets with None.
- Geometry Editors
- ----------------
- ``force_rhr``
- ~~~~~~~~~~~~~
- .. method:: GeoQuerySet.force_rhr(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.ForceRHR` function
- instead.
- *Availability*: PostGIS
- Returns a modified version of the polygon/multipolygon in which all
- of the vertices follow the Right-Hand-Rule, and attaches as a
- ``force_rhr`` attribute on each element of the queryset.
- ``reverse_geom``
- ~~~~~~~~~~~~~~~~
- .. method:: GeoQuerySet.reverse_geom(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Reverse` function
- instead.
- *Availability*: PostGIS, Oracle
- Reverse the coordinate order of the geometry field, and attaches as a
- ``reverse`` attribute on each element of the queryset.
- ``scale``
- ~~~~~~~~~
- .. method:: GeoQuerySet.scale(x, y, z=0.0, **kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Scale` function
- instead.
- *Availability*: PostGIS, SpatiaLite
- ``snap_to_grid``
- ~~~~~~~~~~~~~~~~
- .. method:: GeoQuerySet.snap_to_grid(*args, **kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.SnapToGrid` function
- instead.
- Snap all points of the input geometry to the grid. How the
- geometry is snapped to the grid depends on how many numeric
- (either float, integer, or long) arguments are given.
- =================== =====================================================
- Number of Arguments Description
- =================== =====================================================
- 1 A single size to snap bot the X and Y grids to.
- 2 X and Y sizes to snap the grid to.
- 4 X, Y sizes and the corresponding X, Y origins.
- =================== =====================================================
- ``transform``
- ~~~~~~~~~~~~~
- .. method:: GeoQuerySet.transform(srid=4326, **kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Transform` function
- instead.
- *Availability*: PostGIS, Oracle, SpatiaLite
- The ``transform`` method transforms the geometry field of a model to the spatial
- reference system specified by the ``srid`` parameter. If no ``srid`` is given,
- then 4326 (WGS84) is used by default.
- .. note::
- Unlike other ``GeoQuerySet`` methods, ``transform`` stores its output
- "in-place". In other words, no new attribute for the transformed
- geometry is placed on the models.
- .. note::
- What spatial reference system an integer SRID corresponds to may depend on
- the spatial database used. In other words, the SRID numbers used for Oracle
- are not necessarily the same as those used by PostGIS.
- Example::
- >>> qs = Zipcode.objects.all().transform() # Transforms to WGS84
- >>> qs = Zipcode.objects.all().transform(32140) # Transforming to "NAD83 / Texas South Central"
- >>> print(qs[0].poly.srid)
- 32140
- >>> print(qs[0].poly)
- POLYGON ((234055.1698884720099159 4937796.9232223574072123 ...
- ``translate``
- ~~~~~~~~~~~~~
- .. method:: GeoQuerySet.translate(x, y, z=0.0, **kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Translate` function
- instead.
- *Availability*: PostGIS, SpatiaLite
- Translates the geometry field to a new location using the given numeric
- parameters as offsets.
- Geometry Operations
- -------------------
- *Availability*: PostGIS, Oracle, SpatiaLite
- The following methods all take a geometry as a parameter and attach a geometry
- to each element of the ``GeoQuerySet`` that is the result of the operation.
- ``difference``
- ~~~~~~~~~~~~~~
- .. method:: GeoQuerySet.difference(geom)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Difference` function
- instead.
- Returns the spatial difference of the geographic field with the given
- geometry in a ``difference`` attribute on each element of the
- ``GeoQuerySet``.
- ``intersection``
- ~~~~~~~~~~~~~~~~
- .. method:: GeoQuerySet.intersection(geom)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Intersection`
- function instead.
- Returns the spatial intersection of the geographic field with the
- given geometry in an ``intersection`` attribute on each element of the
- ``GeoQuerySet``.
- ``sym_difference``
- ~~~~~~~~~~~~~~~~~~
- .. method:: GeoQuerySet.sym_difference(geom)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.SymDifference`
- function instead.
- Returns the symmetric difference of the geographic field with the
- given geometry in a ``sym_difference`` attribute on each element of the
- ``GeoQuerySet``.
- ``union``
- ~~~~~~~~~
- .. method:: GeoQuerySet.union(geom)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.Union` function
- instead.
- Returns the union of the geographic field with the given
- geometry in an ``union`` attribute on each element of the
- ``GeoQuerySet``.
- Geometry Output
- ---------------
- The following ``GeoQuerySet`` methods will return an attribute that has the value
- of the geometry field in each model converted to the requested output format.
- ``geohash``
- ~~~~~~~~~~~
- .. method:: GeoQuerySet.geohash(precision=20, **kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.GeoHash` function
- instead.
- Attaches a ``geohash`` attribute to every model the queryset
- containing the `GeoHash`__ representation of the geometry.
- __ http://geohash.org/
- ``geojson``
- ~~~~~~~~~~~
- .. method:: GeoQuerySet.geojson(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.AsGeoJSON` function
- instead.
- *Availability*: PostGIS, SpatiaLite
- Attaches a ``geojson`` attribute to every model in the queryset that contains the
- `GeoJSON`__ representation of the geometry.
- ===================== =====================================================
- Keyword Argument Description
- ===================== =====================================================
- ``precision`` It may be used to specify the number of significant
- digits for the coordinates in the GeoJSON
- representation -- the default value is 8.
- ``crs`` Set this to ``True`` if you want the coordinate
- reference system to be included in the returned
- GeoJSON.
- ``bbox`` Set this to ``True`` if you want the bounding box
- to be included in the returned GeoJSON.
- ===================== =====================================================
- __ http://geojson.org/
- ``gml``
- ~~~~~~~
- .. method:: GeoQuerySet.gml(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.AsGML` function
- instead.
- *Availability*: PostGIS, Oracle, SpatiaLite
- Attaches a ``gml`` attribute to every model in the queryset that contains the
- `Geographic Markup Language (GML)`__ representation of the geometry.
- Example::
- >>> qs = Zipcode.objects.all().gml()
- >>> print(qs[0].gml)
- <gml:Polygon srsName="EPSG:4326"><gml:OuterBoundaryIs>-147.78711,70.245363 ... -147.78711,70.245363</gml:OuterBoundaryIs></gml:Polygon>
- ===================== =====================================================
- Keyword Argument Description
- ===================== =====================================================
- ``precision`` This keyword is for PostGIS only. It may be used
- to specify the number of significant digits for the
- coordinates in the GML representation -- the default
- value is 8.
- ``version`` This keyword is for PostGIS only. It may be used to
- specify the GML version used, and may only be values
- of 2 or 3. The default value is 2.
- ===================== =====================================================
- __ https://en.wikipedia.org/wiki/Geography_Markup_Language
- ``kml``
- ~~~~~~~
- .. method:: GeoQuerySet.kml(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.AsKML` function
- instead.
- *Availability*: PostGIS, SpatiaLite
- Attaches a ``kml`` attribute to every model in the queryset that contains the
- `Keyhole Markup Language (KML)`__ representation of the geometry fields. It
- should be noted that the contents of the KML are transformed to WGS84 if
- necessary.
- Example::
- >>> qs = Zipcode.objects.all().kml()
- >>> print(qs[0].kml)
- <Polygon><outerBoundaryIs><LinearRing><coordinates>-103.04135,36.217596,0 ... -103.04135,36.217596,0</coordinates></LinearRing></outerBoundaryIs></Polygon>
- ===================== =====================================================
- Keyword Argument Description
- ===================== =====================================================
- ``precision`` This keyword may be used to specify the number of
- significant digits for the coordinates in the KML
- representation -- the default value is 8.
- ===================== =====================================================
- __ https://developers.google.com/kml/documentation/
- ``svg``
- ~~~~~~~
- .. method:: GeoQuerySet.svg(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.AsSVG` function
- instead.
- *Availability*: PostGIS, SpatiaLite
- Attaches a ``svg`` attribute to every model in the queryset that contains
- the `Scalable Vector Graphics (SVG)`__ path data of the geometry fields.
- ===================== =====================================================
- Keyword Argument Description
- ===================== =====================================================
- ``relative`` If set to ``True``, the path data will be implemented
- in terms of relative moves. Defaults to ``False``,
- meaning that absolute moves are used instead.
- ``precision`` This keyword may be used to specify the number of
- significant digits for the coordinates in the SVG
- representation -- the default value is 8.
- ===================== =====================================================
- __ http://www.w3.org/Graphics/SVG/
- Miscellaneous
- -------------
- ``mem_size``
- ~~~~~~~~~~~~
- .. method:: GeoQuerySet.mem_size(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.MemSize` function
- instead.
- *Availability*: PostGIS
- Returns the memory size (number of bytes) that the geometry field takes
- in a ``mem_size`` attribute on each element of the ``GeoQuerySet``.
- ``num_geom``
- ~~~~~~~~~~~~
- .. method:: GeoQuerySet.num_geom(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.NumGeometries`
- function instead.
- *Availability*: PostGIS, Oracle, SpatiaLite
- Returns the number of geometries in a ``num_geom`` attribute on
- each element of the ``GeoQuerySet`` if the geometry field is a
- collection (e.g., a ``GEOMETRYCOLLECTION`` or ``MULTI*`` field);
- otherwise sets with ``None``.
- ``num_points``
- ~~~~~~~~~~~~~~
- .. method:: GeoQuerySet.num_points(**kwargs)
- .. deprecated:: 1.9
- Use the :class:`~django.contrib.gis.db.models.functions.NumPoints` function
- instead.
- *Availability*: PostGIS, Oracle, SpatiaLite
- Returns the number of points in the first linestring in the
- geometry field in a ``num_points`` attribute on each element of
- the ``GeoQuerySet``; otherwise sets with ``None``.
- Aggregate Functions
- -------------------
- Django provides some GIS-specific aggregate functions. For details on how to
- use these aggregate functions, see :doc:`the topic guide on aggregation
- </topics/db/aggregation>`.
- ===================== =====================================================
- Keyword Argument Description
- ===================== =====================================================
- ``tolerance`` This keyword is for Oracle only. It is for the
- tolerance value used by the ``SDOAGGRTYPE``
- procedure; the `Oracle documentation`__ has more
- details.
- ===================== =====================================================
- __ https://docs.oracle.com/html/B14255_01/sdo_intro.htm#sthref150
- Example::
- >>> from django.contrib.gis.db.models import Extent, Union
- >>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))
- ``Collect``
- ~~~~~~~~~~~
- .. class:: Collect(geo_field)
- *Availability*: PostGIS, SpatiaLite
- Returns a ``GEOMETRYCOLLECTION`` or a ``MULTI`` geometry object from the geometry
- column. This is analogous to a simplified version of the :class:`Union`
- aggregate, except it can be several orders of magnitude faster than performing
- a union because it simply rolls up geometries into a collection or multi object,
- not caring about dissolving boundaries.
- ``Extent``
- ~~~~~~~~~~
- .. class:: Extent(geo_field)
- *Availability*: PostGIS, Oracle, SpatiaLite
- Returns the extent of all ``geo_field`` in the ``QuerySet`` as a four-tuple,
- comprising the lower left coordinate and the upper right coordinate.
- Example::
- >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
- >>> print(qs['poly__extent'])
- (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
- ``Extent3D``
- ~~~~~~~~~~~~
- .. class:: Extent3D(geo_field)
- *Availability*: PostGIS
- Returns the 3D extent of all ``geo_field`` in the ``QuerySet`` as a six-tuple,
- comprising the lower left coordinate and upper right coordinate (each with x, y,
- and z coordinates).
- Example::
- >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent3D('poly'))
- >>> print(qs['poly__extent3d'])
- (-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)
- ``MakeLine``
- ~~~~~~~~~~~~
- .. class:: MakeLine(geo_field)
- *Availability*: PostGIS, SpatiaLite
- Returns a ``LineString`` constructed from the point field geometries in the
- ``QuerySet``. Currently, ordering the queryset has no effect.
- .. versionchanged:: 1.10
- SpatiaLite support was added.
- Example::
- >>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(MakeLine('poly'))
- >>> print(qs['poly__makeline'])
- LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)
- ``Union``
- ~~~~~~~~~
- .. class:: Union(geo_field)
- *Availability*: PostGIS, Oracle, SpatiaLite
- This method returns a :class:`~django.contrib.gis.geos.GEOSGeometry` object
- comprising the union of every geometry in the queryset. Please note that use of
- ``Union`` is processor intensive and may take a significant amount of time on
- large querysets.
- .. note::
- If the computation time for using this method is too expensive, consider
- using :class:`Collect` instead.
- Example::
- >>> u = Zipcode.objects.aggregate(Union(poly)) # This may take a long time.
- >>> u = Zipcode.objects.filter(poly__within=bbox).aggregate(Union(poly)) # A more sensible approach.
- .. rubric:: Footnotes
- .. [#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).
- .. [#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.
- .. [#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).
- .. [#fncontainsproperly] Refer to the PostGIS ``ST_ContainsProperly`` `documentation <http://postgis.net/docs/manual-1.5/ST_ContainsProperly.html>`_ for more details.
|