{"title":"GeoDjango Database API","version":"1.8","locale":"en","docname":"ref/contrib/gis/db-api","url":"/en/1.8/ref/contrib/gis/db-api/","canonical":"https://djangodocs.dev/en/1.8/ref/contrib/gis/db-api/","summary":"Spatial Backends Link to this heading # GeoDjango currently provides the following spatial database backends: django.contrib.gis.db.backends.postgis…","html":"<h1>GeoDjango Database API<a class=\"heading-anchor\" href=\"#geodjango-database-api\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"module-django.contrib.gis.db.backends\">\n<span id=\"id1\"></span><span id=\"spatial-backends\"></span><h2>Spatial Backends<a class=\"heading-anchor\" href=\"#module-django.contrib.gis.db.backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>GeoDjango currently provides the following spatial database backends:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.gis.db.backends.postgis</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.gis.db.backends.mysql</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.gis.db.backends.oracle</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.gis.db.backends.spatialite</span></code></p></li>\n</ul>\n<section id=\"mysql-spatial-limitations\">\n<span id=\"module-django.contrib.gis.db.models\"></span><span id=\"id2\"></span><h3>MySQL Spatial Limitations<a class=\"heading-anchor\" href=\"#mysql-spatial-limitations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>MySQL’s spatial extensions only support bounding box operations\n(what MySQL calls minimum bounding rectangles, or MBR).  Specifically,\n<a class=\"reference external\" href=\"http://dev.mysql.com/doc/refman/5.6/en/spatial-relation-functions.html\">MySQL does not conform to the OGC standard</a>:</p>\n<blockquote>\n<div><p>Currently, MySQL does not implement these functions\n[<code class=\"docutils literal notranslate\"><span class=\"pre\">Contains</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">Crosses</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">Disjoint</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">Intersects</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">Overlaps</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Touches</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">Within</span></code>]\naccording to the specification.  Those that are implemented return\nthe same result as the corresponding MBR-based functions.</p>\n</div></blockquote>\n<p>In other words, while spatial lookups such as <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-gis-contains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">contains</span></code></a>\nare available in GeoDjango when using MySQL, the results returned are really\nequivalent to what would be returned when using <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-bbcontains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">bbcontains</span></code></a>\non a different spatial backend.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>True spatial indexes (R-trees) are only supported with\nMyISAM tables on MySQL. <a class=\"footnote-reference brackets\" href=\"#fnmysqlidx\" id=\"id3\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>5<span class=\"fn-bracket\">]</span></a> In other words, when using\nMySQL spatial extensions you have to choose between fast spatial\nlookups and the integrity of your data – MyISAM tables do\nnot support transactions or foreign key constraints.</p>\n</aside>\n</section>\n</section>\n<section id=\"creating-and-saving-geographic-models\">\n<h2>Creating and Saving Geographic Models<a class=\"heading-anchor\" href=\"#creating-and-saving-geographic-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Here is an example of how to create a geometry object (assuming the <code class=\"docutils literal notranslate\"><span class=\"pre\">Zipcode</span></code>\nmodel):</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">zipcode.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Zipcode</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">z</span> <span class=\"o\">=</span> <span class=\"n\">Zipcode</span><span class=\"p\">(</span><span class=\"n\">code</span><span class=\"o\">=</span><span class=\"mi\">77096</span><span class=\"p\">,</span> <span class=\"n\">poly</span><span class=\"o\">=</span><span class=\"s1\">&#39;POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">z</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n</code></pre></div>\n<p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geos/#django.contrib.gis.geos.GEOSGeometry\" title=\"django.contrib.gis.geos.GEOSGeometry\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code></a> objects may also be used to save geometric models:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.gis.geos</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">GEOSGeometry</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">poly</span> <span class=\"o\">=</span> <span class=\"n\">GEOSGeometry</span><span class=\"p\">(</span><span class=\"s1\">&#39;POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">z</span> <span class=\"o\">=</span> <span class=\"n\">Zipcode</span><span class=\"p\">(</span><span class=\"n\">code</span><span class=\"o\">=</span><span class=\"mi\">77096</span><span class=\"p\">,</span> <span class=\"n\">poly</span><span class=\"o\">=</span><span class=\"n\">poly</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">z</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>Moreover, if the <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> is in a different coordinate system (has a\ndifferent SRID value) than that of the field, then it will be implicitly\ntransformed into the SRID of the model’s field, using the spatial database’s\ntransform procedure:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">poly_3084</span> <span class=\"o\">=</span> <span class=\"n\">GEOSGeometry</span><span class=\"p\">(</span><span class=\"s1\">&#39;POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))&#39;</span><span class=\"p\">,</span> <span class=\"n\">srid</span><span class=\"o\">=</span><span class=\"mi\">3084</span><span class=\"p\">)</span>  <span class=\"c1\"># SRID 3084 is &#39;NAD83(HARN) / Texas Centric Lambert Conformal&#39;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">z</span> <span class=\"o\">=</span> <span class=\"n\">Zipcode</span><span class=\"p\">(</span><span class=\"n\">code</span><span class=\"o\">=</span><span class=\"mi\">78212</span><span class=\"p\">,</span> <span class=\"n\">poly</span><span class=\"o\">=</span><span class=\"n\">poly_3084</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">z</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">connection</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">connection</span><span class=\"o\">.</span><span class=\"n\">queries</span><span class=\"p\">[</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">][</span><span class=\"s1\">&#39;sql&#39;</span><span class=\"p\">])</span> <span class=\"c1\"># printing the last SQL statement executed (requires DEBUG=True)</span>\n<span class=\"go\">INSERT INTO &quot;geoapp_zipcode&quot; (&quot;code&quot;, &quot;poly&quot;) VALUES (78212, ST_Transform(ST_GeomFromWKB(&#39;\\\\001 ... &#39;, 3084), 4326))</span>\n</code></pre></div>\n<p>Thus, geometry parameters may be passed in using the <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> object, WKT\n(Well Known Text <a class=\"footnote-reference brackets\" href=\"#fnwkt\" id=\"id4\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>1<span class=\"fn-bracket\">]</span></a>), HEXEWKB (PostGIS specific – a WKB geometry in\nhexadecimal <a class=\"footnote-reference brackets\" href=\"#fnewkb\" id=\"id5\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>2<span class=\"fn-bracket\">]</span></a>), and GeoJSON <a class=\"footnote-reference brackets\" href=\"#fngeojson\" id=\"id6\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>3<span class=\"fn-bracket\">]</span></a> (requires GDAL). Essentially,\nif the input is not a <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> object, the geometry field will attempt to\ncreate a <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> instance from the input.</p>\n<p>For more information creating <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geos/#django.contrib.gis.geos.GEOSGeometry\" title=\"django.contrib.gis.geos.GEOSGeometry\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code></a>\nobjects, refer to the <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geos/#geos-tutorial\"><span class=\"std std-ref\">GEOS tutorial</span></a>.</p>\n</section>\n<section id=\"spatial-lookups\">\n<span id=\"spatial-lookups-intro\"></span><h2>Spatial Lookups<a class=\"heading-anchor\" href=\"#spatial-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>GeoDjango’s lookup types may be used with any manager method like\n<code class=\"docutils literal notranslate\"><span class=\"pre\">filter()</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">exclude()</span></code>, etc.  However, the lookup types unique to\nGeoDjango are only available on geometry fields.\nFilters on ‘normal’ fields (e.g. <a class=\"reference internal\" href=\"/en/1.8/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a>)\nmay be chained with those on geographic fields.  Thus, geographic queries\ntake the following general form (assuming  the <code class=\"docutils literal notranslate\"><span class=\"pre\">Zipcode</span></code> model used in the\n<a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/model-api/\"><span class=\"doc\">GeoDjango Model API</span></a>):</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">Zipcode</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"o\">&lt;</span><span class=\"n\">field</span><span class=\"o\">&gt;</span><span class=\"n\">__</span><span class=\"o\">&lt;</span><span class=\"n\">lookup_type</span><span class=\"o\">&gt;=&lt;</span><span class=\"n\">parameter</span><span class=\"o\">&gt;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">Zipcode</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">exclude</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">Zipcode</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">poly__contains</span><span class=\"o\">=</span><span class=\"n\">pnt</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>In this case, <code class=\"docutils literal notranslate\"><span class=\"pre\">poly</span></code> is the geographic field, <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-gis-contains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">contains</span></code></a>\nis the spatial lookup type, and <code class=\"docutils literal notranslate\"><span class=\"pre\">pnt</span></code> is the parameter (which may be a\n<a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geos/#django.contrib.gis.geos.GEOSGeometry\" title=\"django.contrib.gis.geos.GEOSGeometry\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code></a> object or a string of\nGeoJSON , WKT, or HEXEWKB).</p>\n<p>A complete reference can be found in the <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#spatial-lookups\"><span class=\"std std-ref\">spatial lookup reference</span></a>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>GeoDjango constructs spatial SQL with the <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet\" title=\"django.contrib.gis.db.models.GeoQuerySet\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GeoQuerySet</span></code></a>, a\nsubclass of <a class=\"reference internal\" href=\"/en/1.8/ref/models/querysets/#django.db.models.query.QuerySet\" title=\"django.db.models.query.QuerySet\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">QuerySet</span></code></a>.  The\n<a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/model-api/#django.contrib.gis.db.models.GeoManager\" title=\"django.contrib.gis.db.models.GeoManager\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GeoManager</span></code></a> instance attached to your model is what\nenables use of <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet\" title=\"django.contrib.gis.db.models.GeoQuerySet\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GeoQuerySet</span></code></a>.</p>\n</aside>\n</section>\n<section id=\"distance-queries\">\n<span id=\"id7\"></span><h2>Distance Queries<a class=\"heading-anchor\" href=\"#distance-queries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"introduction\">\n<h3>Introduction<a class=\"heading-anchor\" href=\"#introduction\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Distance calculations with spatial data is tricky because, unfortunately,\nthe Earth is not flat.  Some distance queries with fields in a geographic\ncoordinate system may have to be expressed differently because of\nlimitations in PostGIS.  Please see the <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/model-api/#selecting-an-srid\"><span class=\"std std-ref\">Selecting an SRID</span></a> section\nin the <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/model-api/\"><span class=\"doc\">GeoDjango Model API</span></a> documentation for more details.</p>\n</section>\n<section id=\"distance-lookups\">\n<span id=\"distance-lookups-intro\"></span><h3>Distance Lookups<a class=\"heading-anchor\" href=\"#distance-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><em>Availability</em>: PostGIS, Oracle, SpatiaLite</p>\n<p>The following distance lookups are available:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_lt\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_lt</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_lte\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_lte</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_gt\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_gt</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_gte\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_gte</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-dwithin\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">dwithin</span></code></a></p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>For <em>measuring</em>, rather than querying on distances, use the\n<a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.distance\" title=\"django.contrib.gis.db.models.GeoQuerySet.distance\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.distance()</span></code></a> method.</p>\n</aside>\n<p>Distance lookups take a tuple parameter comprising:</p>\n<ol class=\"arabic simple\">\n<li><p>A geometry to base calculations from; and</p></li>\n<li><p>A number or <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/measure/#django.contrib.gis.measure.Distance\" title=\"django.contrib.gis.measure.Distance\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Distance</span></code></a> object containing the distance.</p></li>\n</ol>\n<p>If a <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/measure/#django.contrib.gis.measure.Distance\" title=\"django.contrib.gis.measure.Distance\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Distance</span></code></a> object is used,\nit may be expressed in any units (the SQL generated will use units\nconverted to those of the field); otherwise, numeric parameters are assumed\nto be in the units of the field.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>In PostGIS, <code class=\"docutils literal notranslate\"><span class=\"pre\">ST_Distance_Sphere</span></code> does <em>not</em> limit the geometry types\ngeographic distance queries are performed with. <a class=\"footnote-reference brackets\" href=\"#fndistsphere15\" id=\"id8\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>4<span class=\"fn-bracket\">]</span></a>  However,\nthese queries may take a long time, as great-circle distances must be\ncalculated on the fly for <em>every</em> row in the query.  This is because the\nspatial index on traditional geometry fields cannot be used.</p>\n<p>For much better performance on WGS84 distance queries, consider using\n<a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/model-api/#geography-type\"><span class=\"std std-ref\">geography columns</span></a> in your database instead because\nthey are able to use their spatial index in distance queries.\nYou can tell GeoDjango to use a geography column by setting <code class=\"docutils literal notranslate\"><span class=\"pre\">geography=True</span></code>\nin your field definition.</p>\n</aside>\n<p>For example, let’s say we have a <code class=\"docutils literal notranslate\"><span class=\"pre\">SouthTexasCity</span></code> model (from the\n<a class=\"reference external\" href=\"https://github.com/django/django/blob/master/tests/gis_tests/distapp/models.py\">GeoDjango distance tests</a> ) on a <em>projected</em> coordinate system valid for cities\nin southern Texas:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.gis.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">models</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">SouthTexasCity</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n    <span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">30</span><span class=\"p\">)</span>\n    <span class=\"c1\"># A projected coordinate system (only valid for South Texas!)</span>\n    <span class=\"c1\"># is used, units are in meters.</span>\n    <span class=\"n\">point</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">PointField</span><span class=\"p\">(</span><span class=\"n\">srid</span><span class=\"o\">=</span><span class=\"mi\">32140</span><span class=\"p\">)</span>\n    <span class=\"n\">objects</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">GeoManager</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>Then distance queries may be performed as follows:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.gis.geos</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">GEOSGeometry</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.gis.measure</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">D</span> <span class=\"c1\"># ``D`` is a shortcut for ``Distance``</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">geoapp.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">SouthTexasCity</span>\n<span class=\"go\"># Distances will be calculated from this point, which does not have to be projected.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">pnt</span> <span class=\"o\">=</span> <span class=\"n\">GEOSGeometry</span><span class=\"p\">(</span><span class=\"s1\">&#39;POINT(-96.876369 29.905320)&#39;</span><span class=\"p\">,</span> <span class=\"n\">srid</span><span class=\"o\">=</span><span class=\"mi\">4326</span><span class=\"p\">)</span>\n<span class=\"go\"># If numeric parameter, units of field (meters in this case) are assumed.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">SouthTexasCity</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">point__distance_lte</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">pnt</span><span class=\"p\">,</span> <span class=\"mi\">7000</span><span class=\"p\">))</span>\n<span class=\"go\"># Find all Cities within 7 km, &gt; 20 miles away, and &gt; 100 chains away (an obscure unit)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">SouthTexasCity</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">point__distance_lte</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">pnt</span><span class=\"p\">,</span> <span class=\"n\">D</span><span class=\"p\">(</span><span class=\"n\">km</span><span class=\"o\">=</span><span class=\"mi\">7</span><span class=\"p\">)))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">SouthTexasCity</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">point__distance_gte</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">pnt</span><span class=\"p\">,</span> <span class=\"n\">D</span><span class=\"p\">(</span><span class=\"n\">mi</span><span class=\"o\">=</span><span class=\"mi\">20</span><span class=\"p\">)))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">SouthTexasCity</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">point__distance_gte</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">pnt</span><span class=\"p\">,</span> <span class=\"n\">D</span><span class=\"p\">(</span><span class=\"n\">chain</span><span class=\"o\">=</span><span class=\"mi\">100</span><span class=\"p\">)))</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"compatibility-tables\">\n<span id=\"compatibility-table\"></span><h2>Compatibility Tables<a class=\"heading-anchor\" href=\"#compatibility-tables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"spatial-lookup-compatibility\">\n<span id=\"id10\"></span><h3>Spatial Lookups<a class=\"heading-anchor\" href=\"#spatial-lookup-compatibility\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The following table provides a summary of what spatial lookups are available\nfor each spatial database backend.</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Lookup Type</p></th>\n<th class=\"head\"><p>PostGIS</p></th>\n<th class=\"head\"><p>Oracle</p></th>\n<th class=\"head\"><p>MySQL <a class=\"footnote-reference brackets\" href=\"#id12\" id=\"id11\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>6<span class=\"fn-bracket\">]</span></a></p></th>\n<th class=\"head\"><p>SpatiaLite</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-bbcontains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">bbcontains</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-bboverlaps\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">bboverlaps</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-contained\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">contained</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-gis-contains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">contains</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-contains_properly\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">contains_properly</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-coveredby\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">coveredby</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-covers\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">covers</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-crosses\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">crosses</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-disjoint\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">disjoint</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_gt\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_gt</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_gte\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_gte</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_lt\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_lt</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-distance_lte\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">distance_lte</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-dwithin\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">dwithin</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-equals\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">equals</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/models/querysets/#std-fieldlookup-exact\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">exact</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-intersects\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">intersects</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-overlaps\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">overlaps</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-relate\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">relate</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-same_as\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">same_as</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-touches\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">touches</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-within\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">within</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-left\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">left</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-right\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">right</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-overlaps_left\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">overlaps_left</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-overlaps_right\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">overlaps_right</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-overlaps_above\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">overlaps_above</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-overlaps_below\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">overlaps_below</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-strictly_above\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">strictly_above</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#std-fieldlookup-strictly_below\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">strictly_below</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n</tbody>\n</table>\n</div>\n</section>\n<section id=\"geoqueryset-methods\">\n<span id=\"geoqueryset-method-compatibility\"></span><h3><code class=\"docutils literal notranslate\"><span class=\"pre\">GeoQuerySet</span></code> Methods<a class=\"heading-anchor\" href=\"#geoqueryset-methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The following table provides a summary of what <a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet\" title=\"django.contrib.gis.db.models.GeoQuerySet\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GeoQuerySet</span></code></a> methods\nare available on each spatial backend.  Please note that MySQL does not\nsupport any of these methods, and is thus excluded from the table.</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Method</p></th>\n<th class=\"head\"><p>PostGIS</p></th>\n<th class=\"head\"><p>Oracle</p></th>\n<th class=\"head\"><p>SpatiaLite</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.area\" title=\"django.contrib.gis.db.models.GeoQuerySet.area\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.area()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.centroid\" title=\"django.contrib.gis.db.models.GeoQuerySet.centroid\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.centroid()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.difference\" title=\"django.contrib.gis.db.models.GeoQuerySet.difference\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.difference()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.distance\" title=\"django.contrib.gis.db.models.GeoQuerySet.distance\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.distance()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.envelope\" title=\"django.contrib.gis.db.models.GeoQuerySet.envelope\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.envelope()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.force_rhr\" title=\"django.contrib.gis.db.models.GeoQuerySet.force_rhr\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.force_rhr()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.geohash\" title=\"django.contrib.gis.db.models.GeoQuerySet.geohash\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.geohash()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.geojson\" title=\"django.contrib.gis.db.models.GeoQuerySet.geojson\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.geojson()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.gml\" title=\"django.contrib.gis.db.models.GeoQuerySet.gml\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.gml()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.intersection\" title=\"django.contrib.gis.db.models.GeoQuerySet.intersection\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.intersection()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.kml\" title=\"django.contrib.gis.db.models.GeoQuerySet.kml\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.kml()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.length\" title=\"django.contrib.gis.db.models.GeoQuerySet.length\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.length()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.mem_size\" title=\"django.contrib.gis.db.models.GeoQuerySet.mem_size\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.mem_size()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.num_geom\" title=\"django.contrib.gis.db.models.GeoQuerySet.num_geom\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.num_geom()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.num_points\" title=\"django.contrib.gis.db.models.GeoQuerySet.num_points\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.num_points()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.perimeter\" title=\"django.contrib.gis.db.models.GeoQuerySet.perimeter\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.perimeter()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.point_on_surface\" title=\"django.contrib.gis.db.models.GeoQuerySet.point_on_surface\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.point_on_surface()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.reverse_geom\" title=\"django.contrib.gis.db.models.GeoQuerySet.reverse_geom\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.reverse_geom()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.scale\" title=\"django.contrib.gis.db.models.GeoQuerySet.scale\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.scale()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.snap_to_grid\" title=\"django.contrib.gis.db.models.GeoQuerySet.snap_to_grid\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.snap_to_grid()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.svg\" title=\"django.contrib.gis.db.models.GeoQuerySet.svg\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.svg()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.sym_difference\" title=\"django.contrib.gis.db.models.GeoQuerySet.sym_difference\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.sym_difference()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.transform\" title=\"django.contrib.gis.db.models.GeoQuerySet.transform\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.transform()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.translate\" title=\"django.contrib.gis.db.models.GeoQuerySet.translate\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.translate()</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.GeoQuerySet.union\" title=\"django.contrib.gis.db.models.GeoQuerySet.union\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">GeoQuerySet.union()</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</section>\n<section id=\"aggregate-functions\">\n<h3>Aggregate Functions<a class=\"heading-anchor\" href=\"#aggregate-functions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The following table provides a summary of what GIS-specific aggregate functions\nare available on each spatial backend. Please note that MySQL does not\nsupport any of these aggregates, and is thus excluded from the table.</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>Aggregate</p></th>\n<th class=\"head\"><p>PostGIS</p></th>\n<th class=\"head\"><p>Oracle</p></th>\n<th class=\"head\"><p>SpatiaLite</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.Collect\" title=\"django.contrib.gis.db.models.Collect\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Collect</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td><p>(from v3.0)</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.Extent\" title=\"django.contrib.gis.db.models.Extent\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Extent</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>(from v3.0)</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.Extent3D\" title=\"django.contrib.gis.db.models.Extent3D\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Extent3D</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.MakeLine\" title=\"django.contrib.gis.db.models.MakeLine\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MakeLine</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/en/1.8/ref/contrib/gis/geoquerysets/#django.contrib.gis.db.models.Union\" title=\"django.contrib.gis.db.models.Union\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Union</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p class=\"rubric\">Footnotes</p>\n<aside class=\"footnote-list brackets\">\n<aside class=\"footnote brackets\" id=\"fnwkt\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id4\">1</a><span class=\"fn-bracket\">]</span></span>\n<p><em>See</em> Open Geospatial Consortium, Inc., <a class=\"reference external\" href=\"http://www.opengis.org/docs/99-049.pdf\">OpenGIS Simple Feature Specification For SQL</a>, Document 99-049 (May 5, 1999), at  Ch. 3.2.5, p. 3-11 (SQL Textual Representation of Geometry).</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fnewkb\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id5\">2</a><span class=\"fn-bracket\">]</span></span>\n<p><em>See</em> <a class=\"reference external\" href=\"http://postgis.net/docs/manual-2.1/using_postgis_dbmanagement.html#EWKB_EWKT\">PostGIS EWKB, EWKT and Canonical Forms</a>, PostGIS documentation at Ch. 4.1.2.</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fngeojson\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id6\">3</a><span class=\"fn-bracket\">]</span></span>\n<p><em>See</em> Howard Butler, Martin Daly, Allan Doyle, Tim Schaub, &amp; Christopher Schmidt, <a class=\"reference external\" href=\"http://geojson.org/geojson-spec.html\">The GeoJSON Format Specification</a>, Revision 1.0 (June 16, 2008).</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fndistsphere15\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id8\">4</a><span class=\"fn-bracket\">]</span></span>\n<p><em>See</em> <a class=\"reference external\" href=\"http://postgis.net/docs/manual-2.1/ST_Distance_Sphere.html\">PostGIS documentation</a> on <code class=\"docutils literal notranslate\"><span class=\"pre\">ST_distance_sphere</span></code>.</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"fnmysqlidx\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id3\">5</a><span class=\"fn-bracket\">]</span></span>\n<p><em>See</em> <a class=\"reference external\" href=\"http://dev.mysql.com/doc/refman/5.6/en/creating-spatial-indexes.html\">Creating Spatial Indexes</a>\nin the MySQL Reference Manual:</p>\n<blockquote>\n<div><p>For MyISAM tables, <code class=\"docutils literal notranslate\"><span class=\"pre\">SPATIAL</span> <span class=\"pre\">INDEX</span></code> creates an R-tree index. For storage\nengines that support nonspatial indexing of spatial columns, the engine\ncreates a B-tree index. A B-tree index on spatial values will be useful\nfor exact-value lookups, but not for range scans.</p>\n</div></blockquote>\n</aside>\n<aside class=\"footnote brackets\" id=\"id12\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id11\">6</a><span class=\"fn-bracket\">]</span></span>\n<p>Refer <a class=\"reference internal\" href=\"#mysql-spatial-limitations\"><span class=\"std std-ref\">MySQL Spatial Limitations</span></a> section for more details.</p>\n</aside>\n</aside>\n</section>\n</section>","rootId":"geodjango-database-api","toc":[{"title":"Spatial Backends","anchor":"module-django.contrib.gis.db.backends","children":[{"title":"MySQL Spatial Limitations","anchor":"mysql-spatial-limitations","children":[]}]},{"title":"Creating and Saving Geographic Models","anchor":"creating-and-saving-geographic-models","children":[]},{"title":"Spatial Lookups","anchor":"spatial-lookups","children":[]},{"title":"Distance Queries","anchor":"distance-queries","children":[{"title":"Introduction","anchor":"introduction","children":[]},{"title":"Distance Lookups","anchor":"distance-lookups","children":[]}]},{"title":"Compatibility Tables","anchor":"compatibility-tables","children":[{"title":"Spatial Lookups","anchor":"spatial-lookup-compatibility","children":[]},{"title":"GeoQuerySet Methods","anchor":"geoqueryset-methods","children":[]},{"title":"Aggregate Functions","anchor":"aggregate-functions","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/en/1.8/ref/"},{"docname":"ref/contrib/index","title":"contrib packages","url":"/en/1.8/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/en/1.8/ref/contrib/gis/"}],"prev":{"docname":"ref/contrib/gis/model-api","title":"GeoDjango Model API","url":"/en/1.8/ref/contrib/gis/model-api/"},"next":{"docname":"ref/contrib/gis/forms-api","title":"GeoDjango Forms API","url":"/en/1.8/ref/contrib/gis/forms-api/"},"formats":{"html":"/en/1.8/ref/contrib/gis/db-api/","markdown":"/en/1.8/ref/contrib/gis/db-api.md","json":"/en/1.8/ref/contrib/gis/db-api.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/ref/contrib/gis/db-api.txt","official":"https://docs.djangoproject.com/en/1.8/ref/contrib/gis/db-api/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en"]}