2010-03-26 20:14:53 +00:00
=========================
GeoQuerySet API Reference
=========================
.. currentmodule:: django.contrib.gis.db.models
2015-07-27 12:35:21 +00:00
.. class:: GeoQuerySet(model=None)
2010-03-26 20:14:53 +00:00
.. _spatial-lookups:
Spatial Lookups
===============
2011-02-14 23:18:30 +00:00
Just like when using the :ref:`queryset-api`, interaction
2010-03-26 20:14:53 +00:00
with ``GeoQuerySet`` by :ref:`chaining filters <chaining-filters>`.
Instead of the regular Django :ref:`field-lookups`, the
spatial lookups in this section are available for :class:`GeometryField`.
2010-08-19 19:27:44 +00:00
For an introduction, see the :ref:`spatial lookups introduction
2010-03-26 20:14:53 +00:00
<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>`.
.. fieldlookup:: bbcontains
bbcontains
----------
*Availability*: PostGIS, MySQL, SpatiaLite
2010-08-19 19:27:44 +00:00
Tests if the geometry field's bounding box completely contains the lookup
2010-03-26 20:14:53 +00:00
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
2010-08-19 19:27:44 +00:00
Tests if the geometry field's bounding box overlaps the lookup geometry's
2010-03-26 20:14:53 +00:00
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
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
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
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
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
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
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
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)``
========== =================================================
2015-08-11 13:33:06 +00:00
.. fieldlookup:: equals
2010-03-26 20:14:53 +00:00
equals
------
*Availability*: PostGIS, Oracle, MySQL, SpatiaLite
.. fieldlookup:: exact
.. fieldlookup:: same_as
exact, same_as
--------------
*Availability*: PostGIS, Oracle, MySQL, SpatiaLite
.. fieldlookup:: intersects
intersects
----------
*Availability*: PostGIS, Oracle, MySQL, SpatiaLite
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:: overlaps
overlaps
--------
*Availability*: PostGIS, Oracle, MySQL, SpatiaLite
.. fieldlookup:: relate
relate
------
*Availability*: PostGIS, Oracle, SpatiaLite
2011-02-14 23:18:30 +00:00
Tests if the geometry field is spatially related to the lookup geometry by
2010-03-26 20:14:53 +00:00
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
~~~~~~~~~~~~~~~~~~~~
2010-08-19 19:27:44 +00:00
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.
2010-03-26 20:14:53 +00:00
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]_
Example::
# A tuple lookup parameter is used to specify the geometry and
# the intersection pattern (the pattern here is for 'contains').
2015-05-06 13:51:01 +00:00
Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))
2010-03-26 20:14:53 +00:00
PostGIS SQL equivalent::
SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')
SpatiaLite SQL equivalent::
SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')
Oracle
~~~~~~
2011-08-13 11:58:19 +00:00
Here the relation pattern is comprised at least one of the nine relation
2010-08-19 19:27:44 +00:00
strings: ``TOUCH``, ``OVERLAPBDYDISJOINT``, ``OVERLAPBDYINTERSECT``,
2010-03-26 20:14:53 +00:00
``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::
2015-05-06 13:51:01 +00:00
Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))
2010-03-26 20:14:53 +00:00
2010-08-19 19:27:44 +00:00
Oracle SQL equivalent::
2010-03-26 20:14:53 +00:00
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
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
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
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
2010-08-19 19:27:44 +00:00
Tests if the geometry field's bounding box overlaps or is to the left of the lookup
2010-03-26 20:14:53 +00:00
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
2010-08-19 19:27:44 +00:00
Tests if the geometry field's bounding box overlaps or is to the right of the lookup
2010-03-26 20:14:53 +00:00
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
2010-08-19 19:27:44 +00:00
Tests if the geometry field's bounding box overlaps or is above the lookup
2010-03-26 20:14:53 +00:00
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
2010-08-19 19:27:44 +00:00
Tests if the geometry field's bounding box overlaps or is below the lookup
2010-03-26 20:14:53 +00:00
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
2010-08-19 19:27:44 +00:00
Tests if the geometry field's bounding box is strictly above the lookup
2010-03-26 20:14:53 +00:00
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
2014-05-19 13:02:42 +00:00
Tests if the geometry field's bounding box is strictly below the lookup
2010-03-26 20:14:53 +00:00
geometry's bounding box.
Example::
2014-05-19 13:02:42 +00:00
Zipcode.objects.filter(poly__strictly_below=geom)
2010-03-26 20:14:53 +00:00
PostGIS equivalent::
2014-05-19 13:02:42 +00:00
SELECT ... WHERE poly <<| geom
2010-03-26 20:14:53 +00:00
.. _distance-lookups:
Distance Lookups
================
*Availability*: PostGIS, Oracle, SpatiaLite
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>, <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,
2015-10-06 20:05:53 +00:00
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>`).
With PostGIS, on every distance lookup but :lookup:`dwithin`, an optional
2010-03-26 20:14:53 +00:00
third 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``
2015-10-09 15:15:16 +00:00
would be used instead of ``ST_Distance_Sphere``). The simpler ``ST_Distance``
function is used with projected coordinate systems.
2010-03-26 20:14:53 +00:00
2015-10-06 20:05:53 +00:00
.. versionadded:: 1.10
The ability to pass an expression as the distance value was added.
2010-03-26 20:14:53 +00:00
.. 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)))
2015-10-09 15:15:16 +00:00
========== ==================================================
2010-03-26 20:14:53 +00:00
Backend SQL Equivalent
2015-10-09 15:15:16 +00:00
========== ==================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) > 5``
2010-03-26 20:14:53 +00:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5``
SpatiaLite ``Distance(poly, geom) > 5``
2015-10-09 15:15:16 +00:00
========== ==================================================
2010-03-26 20:14:53 +00:00
.. 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)))
2015-10-09 15:15:16 +00:00
========== ===================================================
2010-03-26 20:14:53 +00:00
Backend SQL Equivalent
2015-10-09 15:15:16 +00:00
========== ===================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) >= 5``
2010-03-26 20:14:53 +00:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5``
SpatiaLite ``Distance(poly, geom) >= 5``
2015-10-09 15:15:16 +00:00
========== ===================================================
2010-03-26 20:14:53 +00:00
.. 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)))
2015-10-09 15:15:16 +00:00
========== ==================================================
2010-03-26 20:14:53 +00:00
Backend SQL Equivalent
2015-10-09 15:15:16 +00:00
========== ==================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) < 5``
2010-03-26 20:14:53 +00:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5``
SpatiaLite ``Distance(poly, geom) < 5``
2015-10-09 15:15:16 +00:00
========== ==================================================
2010-03-26 20:14:53 +00:00
.. 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)))
2015-10-09 15:15:16 +00:00
========== ===================================================
2010-03-26 20:14:53 +00:00
Backend SQL Equivalent
2015-10-09 15:15:16 +00:00
========== ===================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) <= 5``
2010-03-26 20:14:53 +00:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5``
SpatiaLite ``Distance(poly, geom) <= 5``
2015-10-09 15:15:16 +00:00
========== ===================================================
2010-03-26 20:14:53 +00:00
.. fieldlookup:: dwithin
dwithin
-------
2014-06-14 18:52:37 +00:00
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``) .
2010-03-26 20:14:53 +00:00
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.
2015-05-08 17:52:15 +00:00
.. _geoqueryset-methods:
2010-03-26 20:14:53 +00:00
``GeoQuerySet`` Methods
=======================
2015-01-30 19:23:23 +00:00
.. 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.
2010-03-26 20:14:53 +00:00
``GeoQuerySet`` methods specify that a spatial operation be performed
2014-02-22 17:30:28 +00:00
on each spatial operation on each geographic
2010-03-26 20:14:53 +00:00
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
2015-01-30 19:23:23 +00:00
the :ref:`compatibility table <database-functions-compatibility>`
2010-03-26 20:14:53 +00:00
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
2010-08-19 19:27:44 +00:00
keyword should be used to specify another
2010-03-26 20:14:53 +00:00
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'``).
2010-08-19 19:27:44 +00:00
2010-03-26 20:14:53 +00:00
``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
2010-08-19 19:27:44 +00:00
``centroid`` attribute.
2010-03-26 20:14:53 +00:00
2010-08-19 19:27:44 +00:00
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``
2013-01-01 13:12:42 +00:00
named ``area``, for example.
2010-03-26 20:14:53 +00:00
===================== =====================================================
Measurement
-----------
*Availability*: PostGIS, Oracle, SpatiaLite
``area``
~~~~~~~~
.. method:: GeoQuerySet.area(**kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Area` function
instead.
2010-03-26 20:14:53 +00:00
Returns the area of the geographic field in an ``area`` attribute on
each element of this GeoQuerySet.
``distance``
~~~~~~~~~~~~
.. method:: GeoQuerySet.distance(geom, **kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Distance` function
instead.
2010-08-19 19:27:44 +00:00
This method takes a geometry as a parameter, and attaches a ``distance``
attribute to every model in the returned queryset that contains the
2010-03-26 20:14:53 +00:00
distance (as a :class:`~django.contrib.gis.measure.Distance` object) to the given geometry.
2010-08-19 19:27:44 +00:00
In the following example (taken from the `GeoDjango distance tests`__),
the distance from the `Tasmanian`__ city of Hobart to every other
2010-03-26 20:14:53 +00:00
:class:`PointField` in the ``AustraliaCity`` queryset is calculated::
>>> pnt = AustraliaCity.objects.get(name='Hobart').point
2012-04-28 16:02:01 +00:00
>>> for city in AustraliaCity.objects.distance(pnt): print(city.name, city.distance)
2010-03-26 20:14:53 +00:00
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
2010-08-19 19:27:44 +00:00
the distance value in miles and ``city.distance.km`` is the distance value
2014-11-26 17:46:06 +00:00
in kilometers. See :doc:`measure` for usage details and the list of
2010-03-26 20:14:53 +00:00
:ref:`supported_units`.
2015-03-10 16:59:27 +00:00
__ https://github.com/django/django/blob/master/tests/gis_tests/distapp/models.py
2015-08-08 10:02:32 +00:00
__ https://en.wikipedia.org/wiki/Tasmania
2010-03-26 20:14:53 +00:00
``length``
~~~~~~~~~~
.. method:: GeoQuerySet.length(**kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Length` function
instead.
2010-03-26 20:14:53 +00:00
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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Perimeter` function
instead.
2010-03-26 20:14:53 +00:00
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
2011-02-14 23:18:30 +00:00
function evaluated on the geometry field.
2010-03-26 20:14:53 +00:00
``centroid``
~~~~~~~~~~~~
.. method:: GeoQuerySet.centroid(**kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Centroid` function
instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Envelope` function
instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.PointOnSurface`
function instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.ForceRHR` function
instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Reverse` function
instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Scale` function
instead.
2010-03-26 20:14:53 +00:00
*Availability*: PostGIS, SpatiaLite
``snap_to_grid``
~~~~~~~~~~~~~~~~
.. method:: GeoQuerySet.snap_to_grid(*args, **kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.SnapToGrid` function
instead.
2010-03-26 20:14:53 +00:00
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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Transform` function
instead.
2010-03-26 20:14:53 +00:00
*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::
2010-08-19 19:27:44 +00:00
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
2010-03-26 20:14:53 +00:00
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"
2012-04-28 16:02:01 +00:00
>>> print(qs[0].poly.srid)
2010-03-26 20:14:53 +00:00
32140
2012-04-28 16:02:01 +00:00
>>> print(qs[0].poly)
2010-03-26 20:14:53 +00:00
POLYGON ((234055.1698884720099159 4937796.9232223574072123 ...
``translate``
~~~~~~~~~~~~~
.. method:: GeoQuerySet.translate(x, y, z=0.0, **kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Translate` function
instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Difference` function
instead.
2010-03-26 20:14:53 +00:00
Returns the spatial difference of the geographic field with the given
2010-08-19 19:27:44 +00:00
geometry in a ``difference`` attribute on each element of the
2010-03-26 20:14:53 +00:00
``GeoQuerySet``.
``intersection``
~~~~~~~~~~~~~~~~
.. method:: GeoQuerySet.intersection(geom)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Intersection`
function instead.
2010-03-26 20:14:53 +00:00
Returns the spatial intersection of the geographic field with the
2010-08-19 19:27:44 +00:00
given geometry in an ``intersection`` attribute on each element of the
2010-03-26 20:14:53 +00:00
``GeoQuerySet``.
``sym_difference``
~~~~~~~~~~~~~~~~~~
.. method:: GeoQuerySet.sym_difference(geom)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.SymDifference`
function instead.
2010-03-26 20:14:53 +00:00
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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.Union` function
instead.
2010-03-26 20:14:53 +00:00
Returns the union of the geographic field with the given
geometry in an ``union`` attribute on each element of the
``GeoQuerySet``.
Geometry Output
---------------
2010-08-19 19:27:44 +00:00
The following ``GeoQuerySet`` methods will return an attribute that has the value
2010-03-26 20:14:53 +00:00
of the geometry field in each model converted to the requested output format.
``geohash``
~~~~~~~~~~~
2012-05-18 21:56:40 +00:00
.. method:: GeoQuerySet.geohash(precision=20, **kwargs)
2010-03-26 20:14:53 +00:00
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.GeoHash` function
instead.
2011-02-14 23:18:30 +00:00
Attaches a ``geohash`` attribute to every model the queryset
2010-03-26 20:14:53 +00:00
containing the `GeoHash`__ representation of the geometry.
__ http://geohash.org/
``geojson``
~~~~~~~~~~~
.. method:: GeoQuerySet.geojson(**kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.AsGeoJSON` function
instead.
2012-10-12 15:22:20 +00:00
*Availability*: PostGIS, SpatiaLite
2010-03-26 20:14:53 +00:00
Attaches a ``geojson`` attribute to every model in the queryset that contains the
`GeoJSON`__ representation of the geometry.
===================== =====================================================
Keyword Argument Description
===================== =====================================================
2010-08-19 19:27:44 +00:00
``precision`` It may be used to specify the number of significant
digits for the coordinates in the GeoJSON
2010-03-26 20:14:53 +00:00
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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.AsGML` function
instead.
2011-09-11 00:52:08 +00:00
*Availability*: PostGIS, Oracle, SpatiaLite
2010-03-26 20:14:53 +00:00
2010-08-19 19:27:44 +00:00
Attaches a ``gml`` attribute to every model in the queryset that contains the
`Geographic Markup Language (GML)`__ representation of the geometry.
2010-03-26 20:14:53 +00:00
Example::
>>> qs = Zipcode.objects.all().gml()
2012-04-28 16:02:01 +00:00
>>> print(qs[0].gml)
2010-03-26 20:14:53 +00:00
<gml:Polygon srsName="EPSG:4326"><gml:OuterBoundaryIs>-147.78711,70.245363 ... -147.78711,70.245363</gml:OuterBoundaryIs></gml:Polygon>
===================== =====================================================
Keyword Argument Description
===================== =====================================================
2010-08-19 19:27:44 +00:00
``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
2010-03-26 20:14:53 +00:00
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.
===================== =====================================================
2015-08-08 10:02:32 +00:00
__ https://en.wikipedia.org/wiki/Geography_Markup_Language
2010-03-26 20:14:53 +00:00
``kml``
~~~~~~~
.. method:: GeoQuerySet.kml(**kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.AsKML` function
instead.
2011-09-11 00:52:08 +00:00
*Availability*: PostGIS, SpatiaLite
2010-03-26 20:14:53 +00:00
2010-08-19 19:27:44 +00:00
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
2010-03-26 20:14:53 +00:00
necessary.
Example::
>>> qs = Zipcode.objects.all().kml()
2012-04-28 16:02:01 +00:00
>>> print(qs[0].kml)
2010-03-26 20:14:53 +00:00
<Polygon><outerBoundaryIs><LinearRing><coordinates>-103.04135,36.217596,0 ... -103.04135,36.217596,0</coordinates></LinearRing></outerBoundaryIs></Polygon>
===================== =====================================================
Keyword Argument Description
===================== =====================================================
2010-08-19 19:27:44 +00:00
``precision`` This keyword may be used to specify the number of
significant digits for the coordinates in the KML
2010-03-26 20:14:53 +00:00
representation -- the default value is 8.
===================== =====================================================
2012-06-28 08:49:07 +00:00
__ https://developers.google.com/kml/documentation/
2010-03-26 20:14:53 +00:00
``svg``
~~~~~~~
.. method:: GeoQuerySet.svg(**kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.AsSVG` function
instead.
2010-03-26 20:14:53 +00:00
*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
2010-08-19 19:27:44 +00:00
in terms of relative moves. Defaults to ``False``,
2013-01-01 13:12:42 +00:00
meaning that absolute moves are used instead.
2010-03-26 20:14:53 +00:00
2010-08-19 19:27:44 +00:00
``precision`` This keyword may be used to specify the number of
significant digits for the coordinates in the SVG
2010-03-26 20:14:53 +00:00
representation -- the default value is 8.
===================== =====================================================
__ http://www.w3.org/Graphics/SVG/
Miscellaneous
-------------
``mem_size``
~~~~~~~~~~~~
.. method:: GeoQuerySet.mem_size(**kwargs)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.MemSize` function
instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.NumGeometries`
function instead.
2010-03-26 20:14:53 +00:00
*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)
2015-01-30 19:23:23 +00:00
.. deprecated:: 1.9
Use the :class:`~django.contrib.gis.db.models.functions.NumPoints` function
instead.
2010-03-26 20:14:53 +00:00
*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``.
2015-01-14 19:48:55 +00:00
Aggregate Functions
-------------------
2010-08-19 19:27:44 +00:00
2015-01-14 19:48:55 +00:00
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>`.
2010-03-26 20:14:53 +00:00
===================== =====================================================
Keyword Argument Description
===================== =====================================================
2010-08-19 19:27:44 +00:00
``tolerance`` This keyword is for Oracle only. It is for the
2010-03-26 20:14:53 +00:00
tolerance value used by the ``SDOAGGRTYPE``
2010-08-19 19:27:44 +00:00
procedure; the `Oracle documentation`__ has more
2010-03-26 20:14:53 +00:00
details.
===================== =====================================================
2015-11-29 16:29:46 +00:00
__ https://docs.oracle.com/html/B14255_01/sdo_intro.htm#sthref150
2010-03-26 20:14:53 +00:00
Example::
>>> from django.contrib.gis.db.models import Extent, Union
2011-09-11 00:15:43 +00:00
>>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))
2010-03-26 20:14:53 +00:00
``Collect``
~~~~~~~~~~~
.. class:: Collect(geo_field)
2015-10-30 19:04:00 +00:00
*Availability*: PostGIS, Spatialite
2015-01-14 19:48:55 +00:00
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.
2010-03-26 20:14:53 +00:00
``Extent``
~~~~~~~~~~
2015-01-14 19:48:55 +00:00
2010-03-26 20:14:53 +00:00
.. class:: Extent(geo_field)
2015-10-30 19:04:00 +00:00
*Availability*: PostGIS, Oracle, Spatialite
2015-01-14 19:48:55 +00:00
Returns the extent of all ``geo_field`` in the ``QuerySet`` as a four-tuple,
comprising the lower left coordinate and the upper right coordinate.
2010-03-26 20:14:53 +00:00
2015-01-14 19:48:55 +00:00
Example::
>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
2015-10-13 18:07:53 +00:00
>>> print(qs['poly__extent'])
2015-01-14 19:48:55 +00:00
(-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
2010-03-26 20:14:53 +00:00
``Extent3D``
~~~~~~~~~~~~
.. class:: Extent3D(geo_field)
2015-01-14 19:48:55 +00:00
*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'))
2015-10-13 18:07:53 +00:00
>>> print(qs['poly__extent3d'])
2015-01-14 19:48:55 +00:00
(-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)
2010-03-26 20:14:53 +00:00
``MakeLine``
~~~~~~~~~~~~
.. class:: MakeLine(geo_field)
2015-12-01 03:08:41 +00:00
*Availability*: PostGIS, SpatiaLite
2015-01-14 19:48:55 +00:00
Returns a ``LineString`` constructed from the point field geometries in the
``QuerySet``. Currently, ordering the queryset has no effect.
2015-12-01 03:08:41 +00:00
.. versionchanged:: 1.10
SpatiaLite support was added.
2015-01-14 19:48:55 +00:00
Example::
>>> print(City.objects.filter(name__in=('Houston', 'Dallas')
2015-10-13 18:07:53 +00:00
... ).aggregate(MakeLine('poly'))['poly__makeline']
2015-01-14 19:48:55 +00:00
LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)
2010-03-26 20:14:53 +00:00
``Union``
~~~~~~~~~
.. class:: Union(geo_field)
2015-01-14 19:48:55 +00:00
*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.
2010-03-26 20:14:53 +00:00
.. 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).
2015-11-29 16:29:46 +00:00
.. [#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.
2010-03-26 20:14:53 +00:00
.. [#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).
2014-12-19 17:07:03 +00:00
.. [#fncontainsproperly] Refer to the PostGIS ``ST_ContainsProperly`` `documentation <http://postgis.net/docs/manual-1.5/ST_ContainsProperly.html>`_ for more details.