2016-11-07 20:00:40 +08:00
==========================
GIS QuerySet API Reference
==========================
2010-03-27 04:14:53 +08:00
.. currentmodule:: django.contrib.gis.db.models
.. _spatial-lookups:
Spatial Lookups
===============
2016-04-22 00:03:14 +08:00
The spatial lookups in this section are available for :class:`GeometryField`
and :class:`RasterField`.
2010-03-27 04:14:53 +08:00
2010-08-20 03:27:44 +08:00
For an introduction, see the :ref:`spatial lookups introduction
2010-03-27 04:14:53 +08: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>`.
2016-04-22 00:03:14 +08:00
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
2017-03-17 02:01:45 +08:00
<https://postgis.net/docs/RT_ST_Polygon.html>`_ function. See also the
2016-04-22 00:03:14 +08:00
: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).
2010-03-27 04:14:53 +08:00
.. fieldlookup:: bbcontains
2016-01-25 05:26:11 +08:00
``bbcontains``
--------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Contain.html>`__,
2019-10-16 19:16:30 +08:00
MariaDB, MySQL, SpatiaLite, PGRaster (Native)
2010-03-27 04:14:53 +08:00
2016-04-22 00:03:14 +08:00
Tests if the geometry or raster field's bounding box completely contains the
lookup geometry's bounding box.
2010-03-27 04:14:53 +08:00
Example::
Zipcode.objects.filter(poly__bbcontains=geom)
========== ==========================
Backend SQL Equivalent
========== ==========================
PostGIS ``poly ~ geom``
2019-10-16 19:16:30 +08:00
MariaDB ``MBRContains(poly, geom)``
2010-03-27 04:14:53 +08:00
MySQL ``MBRContains(poly, geom)``
SpatiaLite ``MbrContains(poly, geom)``
========== ==========================
.. fieldlookup:: bboverlaps
2016-01-25 05:26:11 +08:00
``bboverlaps``
--------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/geometry_overlaps.html>`__,
2019-10-16 19:16:30 +08:00
MariaDB, MySQL, SpatiaLite, PGRaster (Native)
2010-03-27 04:14:53 +08:00
2010-08-20 03:27:44 +08:00
Tests if the geometry field's bounding box overlaps the lookup geometry's
2010-03-27 04:14:53 +08:00
bounding box.
Example::
Zipcode.objects.filter(poly__bboverlaps=geom)
========== ==========================
Backend SQL Equivalent
========== ==========================
PostGIS ``poly && geom``
2019-10-16 19:16:30 +08:00
MariaDB ``MBROverlaps(poly, geom)``
2010-03-27 04:14:53 +08:00
MySQL ``MBROverlaps(poly, geom)``
SpatiaLite ``MbrOverlaps(poly, geom)``
========== ==========================
.. fieldlookup:: contained
2016-01-25 05:26:11 +08:00
``contained``
-------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Contained.html>`__,
2019-10-16 19:16:30 +08:00
MariaDB, MySQL, SpatiaLite, PGRaster (Native)
2010-03-27 04:14:53 +08:00
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``
2019-10-16 19:16:30 +08:00
MariaDB ``MBRWithin(poly, geom)``
2010-03-27 04:14:53 +08:00
MySQL ``MBRWithin(poly, geom)``
SpatiaLite ``MbrWithin(poly, geom)``
========== ==========================
.. fieldlookup:: gis-contains
2016-01-25 05:26:11 +08:00
``contains``
------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Contains.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
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)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Contains(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Contains(poly, geom)``
2010-03-27 04:14:53 +08:00
SpatiaLite ``Contains(poly, geom)``
========== ============================
.. fieldlookup:: contains_properly
2016-01-25 05:26:11 +08:00
``contains_properly``
---------------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_ContainsProperly.html>`__,
PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
Returns true if the lookup geometry intersects the interior of the
2017-03-23 00:21:59 +08:00
geometry field, but not the boundary (or exterior).
2010-03-27 04:14:53 +08:00
Example::
Zipcode.objects.filter(poly__contains_properly=geom)
========== ===================================
Backend SQL Equivalent
========== ===================================
PostGIS ``ST_ContainsProperly(poly, geom)``
========== ===================================
.. fieldlookup:: coveredby
2016-01-25 05:26:11 +08:00
``coveredby``
-------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_CoveredBy.html>`__,
2018-06-21 21:25:31 +08:00
Oracle, PGRaster (Bilateral), SpatiaLite
2010-03-27 04:14:53 +08:00
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)``
2018-06-21 21:25:31 +08:00
SpatiaLite ``CoveredBy(poly, geom)``
2010-03-27 04:14:53 +08:00
========== =============================
.. fieldlookup:: covers
2016-01-25 05:26:11 +08:00
``covers``
----------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Covers.html>`__,
2018-06-21 21:25:31 +08:00
Oracle, PGRaster (Bilateral), SpatiaLite
2010-03-27 04:14:53 +08:00
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)``
2018-06-21 21:25:31 +08:00
SpatiaLite ``Covers(poly, geom)``
2010-03-27 04:14:53 +08:00
========== ==========================
.. fieldlookup:: crosses
2016-01-25 05:26:11 +08:00
``crosses``
-----------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Crosses.html>`__,
2019-10-16 19:16:30 +08:00
MariaDB, MySQL, SpatiaLite, PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
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)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Crosses(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Crosses(poly, geom)``
2010-03-27 04:14:53 +08:00
SpatiaLite ``Crosses(poly, geom)``
========== ==========================
.. fieldlookup:: disjoint
2016-01-25 05:26:11 +08:00
``disjoint``
------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Disjoint.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
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)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Disjoint(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Disjoint(poly, geom)``
2010-03-27 04:14:53 +08:00
SpatiaLite ``Disjoint(poly, geom)``
========== =================================================
2015-08-11 21:33:06 +08:00
.. fieldlookup:: equals
2016-01-25 05:26:11 +08:00
``equals``
----------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Equals.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
2018-06-22 17:13:49 +08:00
Tests if the geometry field is spatially equal to the lookup geometry.
Example::
Zipcode.objects.filter(poly__equals=geom)
========== =================================================
Backend SQL Equivalent
========== =================================================
PostGIS ``ST_Equals(poly, geom)``
Oracle ``SDO_EQUAL(poly, geom)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Equals(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Equals(poly, geom)``
2018-06-22 17:13:49 +08:00
SpatiaLite ``Equals(poly, geom)``
========== =================================================
2010-03-27 04:14:53 +08:00
.. fieldlookup:: exact
2020-04-06 13:41:43 +08:00
:noindex:
2010-03-27 04:14:53 +08:00
.. fieldlookup:: same_as
2016-01-25 05:26:11 +08:00
``exact``, ``same_as``
----------------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Same.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
2019-01-19 22:28:42 +08:00
Tests if the geometry field is "equal" to the lookup geometry. On Oracle,
MySQL, and SpatiaLite, it tests spatial equality, while on PostGIS it tests
2018-06-22 17:13:49 +08:00
equality of bounding boxes.
Example::
Zipcode.objects.filter(poly=geom)
========== =================================================
Backend SQL Equivalent
========== =================================================
PostGIS ``poly ~= geom``
Oracle ``SDO_EQUAL(poly, geom)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Equals(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Equals(poly, geom)``
2018-06-22 17:13:49 +08:00
SpatiaLite ``Equals(poly, geom)``
========== =================================================
2010-03-27 04:14:53 +08:00
.. fieldlookup:: intersects
2016-01-25 05:26:11 +08:00
``intersects``
--------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Intersects.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
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)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Intersects(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Intersects(poly, geom)``
2010-03-27 04:14:53 +08:00
SpatiaLite ``Intersects(poly, geom)``
========== =================================================
2016-04-06 19:50:25 +08:00
.. fieldlookup:: isvalid
``isvalid``
-----------
2017-04-02 01:43:53 +08:00
*Availability*: MySQL (≥ 5.7.5), `PostGIS
<https://postgis.net/docs/ST_IsValid.html>`__, Oracle, SpatiaLite
2016-04-06 19:50:25 +08:00
Tests if the geometry is valid.
Example::
Zipcode.objects.filter(poly__isvalid=True)
2017-04-02 01:43:53 +08:00
========================== ================================================================
Backend SQL Equivalent
========================== ================================================================
MySQL, PostGIS, SpatiaLite ``ST_IsValid(poly)``
Oracle ``SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE'``
========================== ================================================================
2016-04-06 19:50:25 +08:00
2010-03-27 04:14:53 +08:00
.. fieldlookup:: overlaps
2016-01-25 05:26:11 +08:00
``overlaps``
------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Overlaps.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
2019-02-13 04:41:50 +08:00
Tests if the geometry field spatially overlaps the lookup geometry.
========== ============================
Backend SQL Equivalent
========== ============================
PostGIS ``ST_Overlaps(poly, geom)``
Oracle ``SDO_OVERLAPS(poly, geom)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Overlaps(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Overlaps(poly, geom)``
2019-02-13 04:41:50 +08:00
SpatiaLite ``Overlaps(poly, geom)``
========== ============================
2010-03-27 04:14:53 +08:00
.. fieldlookup:: relate
2016-01-25 05:26:11 +08:00
``relate``
----------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Relate.html>`__,
2019-10-17 20:02:37 +08:00
MariaDB, Oracle, SpatiaLite, PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
2011-02-15 07:18:30 +08:00
Tests if the geometry field is spatially related to the lookup geometry by
2010-03-27 04:14:53 +08: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:
2019-10-17 20:02:37 +08:00
MariaDB, PostGIS, and SpatiaLite
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-08-20 03:27:44 +08: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-27 04:14:53 +08: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]_
2016-04-22 00:03:14 +08:00
Geometry example::
2010-03-27 04:14:53 +08:00
# A tuple lookup parameter is used to specify the geometry and
# the intersection pattern (the pattern here is for 'contains').
2015-05-06 21:51:01 +08:00
Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))
2010-03-27 04:14:53 +08:00
2019-10-17 20:02:37 +08:00
PostGIS and MariaDB SQL equivalent:
2019-04-13 21:49:55 +08:00
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')
2019-04-13 21:49:55 +08:00
SpatiaLite SQL equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')
2016-04-22 00:03:14 +08:00
Raster example::
Zipcode.objects.filter(poly__relate=(rast, 1, 'T*T***FF*'))
Zipcode.objects.filter(rast__2__relate=(rast, 1, 'T*T***FF*'))
2019-04-13 21:49:55 +08:00
PostGIS SQL equivalent:
.. code-block:: sql
2016-04-22 00:03:14 +08:00
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*')
2019-10-17 20:02:37 +08:00
.. versionchanged:: 3.1
MariaDB support was added.
2010-03-27 04:14:53 +08:00
Oracle
~~~~~~
2016-02-29 18:47:14 +08:00
Here the relation pattern is comprised of at least one of the nine relation
2010-08-20 03:27:44 +08:00
strings: ``TOUCH``, ``OVERLAPBDYDISJOINT``, ``OVERLAPBDYINTERSECT``,
2010-03-27 04:14:53 +08: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 21:51:01 +08:00
Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))
2010-03-27 04:14:53 +08:00
2019-04-13 21:49:55 +08:00
Oracle SQL equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')
.. fieldlookup:: touches
2016-01-25 05:26:11 +08:00
``touches``
-----------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Touches.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite
2010-03-27 04:14:53 +08:00
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)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Touches(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Touches(poly, geom)``
2010-03-27 04:14:53 +08:00
Oracle ``SDO_TOUCH(poly, geom)``
SpatiaLite ``Touches(poly, geom)``
========== ==========================
.. fieldlookup:: within
2016-01-25 05:26:11 +08:00
``within``
----------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Within.html>`__,
2019-10-16 19:16:30 +08:00
Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
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)``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Within(poly, geom)``
2019-01-19 22:28:42 +08:00
MySQL ``ST_Within(poly, geom)``
2010-03-27 04:14:53 +08:00
Oracle ``SDO_INSIDE(poly, geom)``
SpatiaLite ``Within(poly, geom)``
========== ==========================
.. fieldlookup:: left
2016-01-25 05:26:11 +08:00
``left``
--------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Left.html>`__,
PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
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)
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE poly << geom
.. fieldlookup:: right
2016-01-25 05:26:11 +08:00
``right``
---------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Right.html>`__,
PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
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)
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE poly >> geom
.. fieldlookup:: overlaps_left
2016-01-25 05:26:11 +08:00
``overlaps_left``
-----------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overleft.html>`__,
PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
2010-08-20 03:27:44 +08:00
Tests if the geometry field's bounding box overlaps or is to the left of the lookup
2010-03-27 04:14:53 +08:00
geometry's bounding box.
Example::
Zipcode.objects.filter(poly__overlaps_left=geom)
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE poly &< geom
.. fieldlookup:: overlaps_right
2016-01-25 05:26:11 +08:00
``overlaps_right``
------------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overright.html>`__,
PGRaster (Bilateral)
2010-03-27 04:14:53 +08:00
2010-08-20 03:27:44 +08:00
Tests if the geometry field's bounding box overlaps or is to the right of the lookup
2010-03-27 04:14:53 +08:00
geometry's bounding box.
Example::
Zipcode.objects.filter(poly__overlaps_right=geom)
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE poly &> geom
.. fieldlookup:: overlaps_above
2016-01-25 05:26:11 +08:00
``overlaps_above``
------------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overabove.html>`__,
PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
2010-08-20 03:27:44 +08:00
Tests if the geometry field's bounding box overlaps or is above the lookup
2010-03-27 04:14:53 +08:00
geometry's bounding box.
Example::
Zipcode.objects.filter(poly__overlaps_above=geom)
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE poly |&> geom
.. fieldlookup:: overlaps_below
2016-01-25 05:26:11 +08:00
``overlaps_below``
------------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Overbelow.html>`__,
PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
2010-08-20 03:27:44 +08:00
Tests if the geometry field's bounding box overlaps or is below the lookup
2010-03-27 04:14:53 +08:00
geometry's bounding box.
Example::
Zipcode.objects.filter(poly__overlaps_below=geom)
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE poly &<| geom
.. fieldlookup:: strictly_above
2016-01-25 05:26:11 +08:00
``strictly_above``
------------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Above.html>`__,
PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
2010-08-20 03:27:44 +08:00
Tests if the geometry field's bounding box is strictly above the lookup
2010-03-27 04:14:53 +08:00
geometry's bounding box.
Example::
Zipcode.objects.filter(poly__strictly_above=geom)
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
SELECT ... WHERE poly |>> geom
.. fieldlookup:: strictly_below
2016-01-25 05:26:11 +08:00
``strictly_below``
------------------
2010-03-27 04:14:53 +08:00
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Geometry_Below.html>`__,
PGRaster (Conversion)
2010-03-27 04:14:53 +08:00
2014-05-19 21:02:42 +08:00
Tests if the geometry field's bounding box is strictly below the lookup
2010-03-27 04:14:53 +08:00
geometry's bounding box.
Example::
2014-05-19 21:02:42 +08:00
Zipcode.objects.filter(poly__strictly_below=geom)
2010-03-27 04:14:53 +08:00
2019-04-13 21:49:55 +08:00
PostGIS equivalent:
.. code-block:: sql
2010-03-27 04:14:53 +08:00
2014-05-19 21:02:42 +08:00
SELECT ... WHERE poly <<| geom
2010-03-27 04:14:53 +08:00
.. _distance-lookups:
Distance Lookups
================
2019-10-16 19:16:30 +08:00
*Availability*: PostGIS, Oracle, MariaDB, MySQL, SpatiaLite, PGRaster (Native)
2010-03-27 04:14:53 +08:00
For an overview on performing distance queries, please refer to
the :ref:`distance queries introduction <distance-queries>`.
Distance lookups take the following form::
2016-04-22 00:03:14 +08:00
<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'])
2010-03-27 04:14:53 +08:00
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-07 04:05:53 +08:00
and a distance value (either a number in units of the field, a
2020-03-31 16:37:38 +08:00
:class:`~django.contrib.gis.measure.Distance` object, or a :doc:`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.
2015-10-07 04:05:53 +08:00
2017-01-06 20:55:00 +08:00
On every distance lookup except :lookup:`dwithin`, an optional element,
``'spheroid'``, may be included to use the more accurate spheroid distance
calculation functions on fields with a geodetic coordinate system.
2017-03-23 00:21:59 +08:00
On PostgreSQL, the ``'spheroid'`` option uses `ST_DistanceSpheroid
<https://postgis.net/docs/ST_Distance_Spheroid.html>`__ instead of
`ST_DistanceSphere <https://postgis.net/docs/ST_DistanceSphere.html>`__. The
simpler `ST_Distance <https://postgis.net/docs/ST_Distance.html>`__ function is
used with projected coordinate systems. Rasters are converted to geometries for
spheroid based lookups.
2010-03-27 04:14:53 +08:00
.. fieldlookup:: distance_gt
2016-01-25 05:26:11 +08:00
``distance_gt``
---------------
2010-03-27 04:14:53 +08:00
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 23:15:16 +08:00
========== ==================================================
2010-03-27 04:14:53 +08:00
Backend SQL Equivalent
2015-10-09 23:15:16 +08:00
========== ==================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) > 5``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Distance(poly, geom) > 5``
2019-10-16 20:39:13 +08:00
MySQL ``ST_Distance(poly, geom) > 5``
2010-03-27 04:14:53 +08:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5``
SpatiaLite ``Distance(poly, geom) > 5``
2015-10-09 23:15:16 +08:00
========== ==================================================
2010-03-27 04:14:53 +08:00
.. fieldlookup:: distance_gte
2016-01-25 05:26:11 +08:00
``distance_gte``
----------------
2010-03-27 04:14:53 +08:00
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 23:15:16 +08:00
========== ===================================================
2010-03-27 04:14:53 +08:00
Backend SQL Equivalent
2015-10-09 23:15:16 +08:00
========== ===================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) >= 5``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Distance(poly, geom) >= 5``
2019-10-16 20:39:13 +08:00
MySQL ``ST_Distance(poly, geom) >= 5``
2010-03-27 04:14:53 +08:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5``
SpatiaLite ``Distance(poly, geom) >= 5``
2015-10-09 23:15:16 +08:00
========== ===================================================
2010-03-27 04:14:53 +08:00
.. fieldlookup:: distance_lt
2016-01-25 05:26:11 +08:00
``distance_lt``
---------------
2010-03-27 04:14:53 +08:00
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 23:15:16 +08:00
========== ==================================================
2010-03-27 04:14:53 +08:00
Backend SQL Equivalent
2015-10-09 23:15:16 +08:00
========== ==================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) < 5``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Distance(poly, geom) < 5``
2019-10-16 20:39:13 +08:00
MySQL ``ST_Distance(poly, geom) < 5``
2010-03-27 04:14:53 +08:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5``
SpatiaLite ``Distance(poly, geom) < 5``
2015-10-09 23:15:16 +08:00
========== ==================================================
2010-03-27 04:14:53 +08:00
.. fieldlookup:: distance_lte
2016-01-25 05:26:11 +08:00
``distance_lte``
----------------
2010-03-27 04:14:53 +08:00
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 23:15:16 +08:00
========== ===================================================
2010-03-27 04:14:53 +08:00
Backend SQL Equivalent
2015-10-09 23:15:16 +08:00
========== ===================================================
PostGIS ``ST_Distance/ST_Distance_Sphere(poly, geom) <= 5``
2019-10-16 19:16:30 +08:00
MariaDB ``ST_Distance(poly, geom) <= 5``
2019-10-16 20:39:13 +08:00
MySQL ``ST_Distance(poly, geom) <= 5``
2010-03-27 04:14:53 +08:00
Oracle ``SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5``
SpatiaLite ``Distance(poly, geom) <= 5``
2015-10-09 23:15:16 +08:00
========== ===================================================
2010-03-27 04:14:53 +08:00
.. fieldlookup:: dwithin
2016-01-25 05:26:11 +08:00
``dwithin``
-----------
2010-03-27 04:14:53 +08:00
2014-06-15 02:52:37 +08: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-27 04:14:53 +08: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)``
2016-07-22 04:54:07 +08:00
SpatiaLite ``PtDistWithin(poly, geom, 5)``
2010-03-27 04:14:53 +08:00
========== ======================================
2015-01-15 03:48:55 +08:00
Aggregate Functions
-------------------
2010-08-20 03:27:44 +08:00
2015-01-15 03:48:55 +08: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-27 04:14:53 +08:00
===================== =====================================================
Keyword Argument Description
===================== =====================================================
2010-08-20 03:27:44 +08:00
``tolerance`` This keyword is for Oracle only. It is for the
2010-03-27 04:14:53 +08:00
tolerance value used by the ``SDOAGGRTYPE``
2010-08-20 03:27:44 +08:00
procedure; the `Oracle documentation`__ has more
2010-03-27 04:14:53 +08:00
details.
===================== =====================================================
2019-02-07 16:59:47 +08:00
__ https://docs.oracle.com/en/database/oracle/oracle-database/18/spatl/
spatial-concepts.html#GUID-CE10AB14-D5EA-43BA-A647-DAC9EEF41EE6
2010-03-27 04:14:53 +08:00
Example::
>>> from django.contrib.gis.db.models import Extent, Union
2011-09-11 08:15:43 +08:00
>>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))
2010-03-27 04:14:53 +08:00
``Collect``
~~~~~~~~~~~
.. class:: Collect(geo_field)
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Collect.html>`__,
SpatiaLite
2015-01-15 03:48:55 +08: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
2019-06-17 22:54:55 +08:00
a union because it rolls up geometries into a collection or multi object, not
caring about dissolving boundaries.
2010-03-27 04:14:53 +08:00
``Extent``
~~~~~~~~~~
2015-01-15 03:48:55 +08:00
2010-03-27 04:14:53 +08:00
.. class:: Extent(geo_field)
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Extent.html>`__,
Oracle, SpatiaLite
2015-01-15 03:48:55 +08: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-27 04:14:53 +08:00
2015-01-15 03:48:55 +08:00
Example::
>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
2015-10-14 02:07:53 +08:00
>>> print(qs['poly__extent'])
2015-01-15 03:48:55 +08:00
(-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
2010-03-27 04:14:53 +08:00
``Extent3D``
~~~~~~~~~~~~
.. class:: Extent3D(geo_field)
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_3DExtent.html>`__
2015-01-15 03:48:55 +08:00
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-14 02:07:53 +08:00
>>> print(qs['poly__extent3d'])
2015-01-15 03:48:55 +08:00
(-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)
2010-03-27 04:14:53 +08:00
``MakeLine``
~~~~~~~~~~~~
.. class:: MakeLine(geo_field)
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_MakeLine.html>`__,
SpatiaLite
2015-01-15 03:48:55 +08:00
Returns a ``LineString`` constructed from the point field geometries in the
``QuerySet``. Currently, ordering the queryset has no effect.
Example::
2016-02-19 15:31:25 +08:00
>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(MakeLine('poly'))
>>> print(qs['poly__makeline'])
LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)
2010-03-27 04:14:53 +08:00
``Union``
~~~~~~~~~
.. class:: Union(geo_field)
2017-03-23 00:21:59 +08:00
*Availability*: `PostGIS <https://postgis.net/docs/ST_Union.html>`__,
Oracle, SpatiaLite
2015-01-15 03:48:55 +08:00
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-27 04:14:53 +08: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).
2019-02-07 16:59:47 +08:00
.. [#fnsdorelate] *See* `SDO_RELATE documentation <https://docs.oracle.com/en/
database/oracle/oracle-database/18/spatl/spatial-operators-reference.html#
GUID-97C17C18-F05E-49B4-BE11-E89B972E2A02>`_, from the Oracle Spatial and
Graph Developer's Guide.
2018-01-07 21:28:41 +08:00
.. [#fncovers] For an explanation of this routine, read `Quirks of the "Contains" Spatial Predicate <https://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html>`_ by Martin Davis (a PostGIS developer).