{"title":"GeoDjango 数据库 API","version":"5.2","locale":"zh-hans","docname":"ref/contrib/gis/db-api","url":"/zh-hans/5.2/ref/contrib/gis/db-api/","canonical":"https://djangodocs.dev/zh-hans/5.2/ref/contrib/gis/db-api/","summary":"空间后端 Link to this heading # GeoDjango 目前提供以下空间数据库后端： django.contrib.gis.db.backends.postgis django.contrib.gis.db.backends.mysql…","html":"<h1>GeoDjango 数据库 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>空间后端<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 目前提供以下空间数据库后端：</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=\"id2\"></span><h3>MySQL 的空间限制<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>Django 支持在现代 MySQL 版本中操作真实几何图形的空间函数。然而，与其他后端如 PostGIS 相比，空间函数并不那么丰富。</p>\n</section>\n<section id=\"raster-support\">\n<h3>栅格支持<a class=\"heading-anchor\" href=\"#raster-support\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">RasterField</span></code> 目前仅在 PostGIS 后端中实现。对于栅格字段，可以使用空间查找，但尚未为栅格字段实现空间数据库函数和聚合函数。</p>\n</section>\n</section>\n<section id=\"creating-and-saving-models-with-geometry-fields\">\n<h2>创建和保存具有几何字段的模型<a class=\"heading-anchor\" href=\"#creating-and-saving-models-with-geometry-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>以下是如何创建一个几何对象的示例（假设有 <code class=\"docutils literal notranslate\"><span class=\"pre\">Zipcode</span></code> 模型）：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console 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=\"s2\">&quot;POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))&quot;</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=\"/zh-hans/5.2/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> 对象也可以用于保存几何模型：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console 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=\"s2\">&quot;POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))&quot;</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>此外，如果 <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> 的坐标系（SRID 值）与字段不同，它将会被隐式地转换为模型字段的 SRID，使用空间数据库的变换过程：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console 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>\n<span class=\"gp\">... </span>    <span class=\"s2\">&quot;POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))&quot;</span><span class=\"p\">,</span> <span class=\"n\">srid</span><span class=\"o\">=</span><span class=\"mi\">3084</span>\n<span class=\"gp\">... </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>\n<span class=\"gp\">... </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=\"s2\">&quot;sql&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">... </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>因此，可以使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> 对象、WKT（Well Known Text <a class=\"footnote-reference brackets\" href=\"#fnwkt\" id=\"id3\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>1<span class=\"fn-bracket\">]</span></a>）、HEXEWKB（PostGIS 特定的十六进制 WKB 几何图形[#fnewkb]_）和 GeoJSON（参见 <span class=\"target\" id=\"index-2\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7946.html\"><strong>RFC 7946</strong></a>）传递几何参数。基本上，如果输入不是 <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> 对象，几何字段将尝试从输入创建一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">GEOSGeometry</span></code> 实例。</p>\n<p>有关创建 <a class=\"reference internal\" href=\"/zh-hans/5.2/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> 对象的更多信息，请参考 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/geos/#geos-tutorial\"><span class=\"std std-ref\">GEOS 教程</span></a>。</p>\n</section>\n<section id=\"creating-and-saving-models-with-raster-fields\">\n<span id=\"creating-and-saving-raster-models\"></span><h2>使用栅格字段创建和保存模型<a class=\"heading-anchor\" href=\"#creating-and-saving-models-with-raster-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>创建栅格模型时，栅格字段将使用延迟计算将输入隐式转换为 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/gdal/#django.contrib.gis.gdal.GDALRaster\" title=\"django.contrib.gis.gdal.GDALRaster\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GDALRaster</span></code></a>。因此，栅格字段将接受任何被 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/gdal/#django.contrib.gis.gdal.GDALRaster\" title=\"django.contrib.gis.gdal.GDALRaster\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GDALRaster</span></code></a> 构造函数接受的输入。</p>\n<p>以下是如何从栅格文件 <code class=\"docutils literal notranslate\"><span class=\"pre\">volcano.tif</span></code> 创建一个栅格对象的示例（假设有 <code class=\"docutils literal notranslate\"><span class=\"pre\">Elevation</span></code> 模型）：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">elevation.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Elevation</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">dem</span> <span class=\"o\">=</span> <span class=\"n\">Elevation</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;Volcano&quot;</span><span class=\"p\">,</span> <span class=\"n\">rast</span><span class=\"o\">=</span><span class=\"s2\">&quot;/path/to/raster/volcano.tif&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">dem</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=\"/zh-hans/5.2/ref/contrib/gis/gdal/#django.contrib.gis.gdal.GDALRaster\" title=\"django.contrib.gis.gdal.GDALRaster\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GDALRaster</span></code></a> 对象也可以用于保存栅格模型：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.gis.gdal</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">GDALRaster</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">rast</span> <span class=\"o\">=</span> <span class=\"n\">GDALRaster</span><span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"p\">{</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;width&quot;</span><span class=\"p\">:</span> <span class=\"mi\">10</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;height&quot;</span><span class=\"p\">:</span> <span class=\"mi\">10</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Canyon&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;srid&quot;</span><span class=\"p\">:</span> <span class=\"mi\">4326</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;scale&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"mf\">0.1</span><span class=\"p\">,</span> <span class=\"o\">-</span><span class=\"mf\">0.1</span><span class=\"p\">],</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;bands&quot;</span><span class=\"p\">:</span> <span class=\"p\">[{</span><span class=\"s2\">&quot;data&quot;</span><span class=\"p\">:</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"mi\">100</span><span class=\"p\">)}],</span>\n<span class=\"gp\">... </span>    <span class=\"p\">}</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">dem</span> <span class=\"o\">=</span> <span class=\"n\">Elevation</span><span class=\"p\">(</span><span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;Canyon&quot;</span><span class=\"p\">,</span> <span class=\"n\">rast</span><span class=\"o\">=</span><span class=\"n\">rast</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">dem</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>请注意，这相当于：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">dem</span> <span class=\"o\">=</span> <span class=\"n\">Elevation</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span>\n<span class=\"gp\">... </span>    <span class=\"n\">name</span><span class=\"o\">=</span><span class=\"s2\">&quot;Canyon&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>    <span class=\"n\">rast</span><span class=\"o\">=</span><span class=\"p\">{</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;width&quot;</span><span class=\"p\">:</span> <span class=\"mi\">10</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;height&quot;</span><span class=\"p\">:</span> <span class=\"mi\">10</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;name&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Canyon&quot;</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;srid&quot;</span><span class=\"p\">:</span> <span class=\"mi\">4326</span><span class=\"p\">,</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;scale&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"mf\">0.1</span><span class=\"p\">,</span> <span class=\"o\">-</span><span class=\"mf\">0.1</span><span class=\"p\">],</span>\n<span class=\"gp\">... </span>        <span class=\"s2\">&quot;bands&quot;</span><span class=\"p\">:</span> <span class=\"p\">[{</span><span class=\"s2\">&quot;data&quot;</span><span class=\"p\">:</span> <span class=\"nb\">range</span><span class=\"p\">(</span><span class=\"mi\">100</span><span class=\"p\">)}],</span>\n<span class=\"gp\">... </span>    <span class=\"p\">},</span>\n<span class=\"gp\">... </span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"spatial-lookups\">\n<span id=\"spatial-lookups-intro\"></span><h2>空间查找<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 的查找类型可以与任何管理器方法一起使用，如 <code class=\"docutils literal notranslate\"><span class=\"pre\">filter()</span></code>、<code class=\"docutils literal notranslate\"><span class=\"pre\">exclude()</span></code> 等。然而，独特于 GeoDjango 的查找类型仅在空间字段上可用。</p>\n<p>Filters on 'normal' fields (e.g. <a class=\"reference internal\" href=\"/zh-hans/5.2/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. Geographic lookups accept\ngeometry and raster input on both sides, and input types can be mixed freely in\nmost cases. However, unlike assignments to model fields, with lookups,\ntypes such as <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>, <a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#pathlib.Path\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">pathlib.Path</span></code></a>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> must be wrapped by\n<a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/gdal/#django.contrib.gis.gdal.GDALRaster\" title=\"django.contrib.gis.gdal.GDALRaster\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GDALRaster</span></code></a> to signify that the potential for\nfile writing or network fetching is acceptable. For the rationale, see\n<a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/gdal/#raster-security\"><span class=\"std std-ref\">raster security considerations</span></a>.</p>\n<p>地理查找的一般结构如下所述。完整的参考信息可以在 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/geoquerysets/#spatial-lookups\"><span class=\"std std-ref\">空间查找参考</span></a> 中找到。</p>\n<aside class=\"version-note version-changed\" data-version=\"5.2.17\">\n<p class=\"version-note-title\">Changed in Django 5.2.17</p><p>In earlier versions, spatial lookups accepted <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code> types\nfor new rasters, allowing file writes and network fetches.</p>\n</aside>\n<section id=\"geometry-lookups\">\n<h3>空间查找<a class=\"heading-anchor\" href=\"#geometry-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>使用几何图形的地理查询通常采用以下一般形式（假设在 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/model-api/\"><span class=\"doc\">GeoDjango 模型 API</span></a> 中使用了 <code class=\"docutils literal notranslate\"><span class=\"pre\">Zipcode</span></code> 模型）：</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>&gt;&gt;&gt; qs = Zipcode.objects.filter(&lt;field&gt;__&lt;lookup_type&gt;=&lt;parameter&gt;)\n&gt;&gt;&gt; qs = Zipcode.objects.exclude(...)\n</code></pre></div>\n<p>例如：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console 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<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">Elevation</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\">rst</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>在这种情况下，<code class=\"docutils literal notranslate\"><span class=\"pre\">poly</span></code> 是地理字段，<a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/geoquerysets/#std-fieldlookup-gis-contains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">contains</span></code></a> 是空间查找类型，<code class=\"docutils literal notranslate\"><span class=\"pre\">pnt</span></code> 是参数（可以是一个 <a class=\"reference internal\" href=\"/zh-hans/5.2/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> 对象或 GeoJSON、WKT 或 HEXEWKB 的字符串），<code class=\"docutils literal notranslate\"><span class=\"pre\">rst</span></code> 是一个 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/gdal/#django.contrib.gis.gdal.GDALRaster\" title=\"django.contrib.gis.gdal.GDALRaster\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GDALRaster</span></code></a> 对象。</p>\n</section>\n<section id=\"raster-lookups\">\n<span id=\"spatial-lookup-raster\"></span><h3>栅格查找<a class=\"heading-anchor\" href=\"#raster-lookups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>栅格查找的语法与几何图形的语法类似。唯一的区别是可以指定一个带索引作为附加输入。如果未指定带索引，则默认使用第一个带（索引 <code class=\"docutils literal notranslate\"><span class=\"pre\">0</span></code>）。在这种情况下，语法与几何查找的语法相同。</p>\n<p>要指定带索引，可以在查找的两侧指定附加参数。在左侧，使用双下划线语法传递带索引。在右侧，可以指定栅格和带索引的元组。</p>\n<p>这导致了涉及栅格的查找的以下一般形式（假设在 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/model-api/\"><span class=\"doc\">GeoDjango 模型 API</span></a> 中使用了 <code class=\"docutils literal notranslate\"><span class=\"pre\">Elevation</span></code> 模型）：</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>&gt;&gt;&gt; qs = Elevation.objects.filter(&lt;field&gt;__&lt;lookup_type&gt;=&lt;parameter&gt;)\n&gt;&gt;&gt; qs = Elevation.objects.filter(&lt;field&gt;__&lt;band_index&gt;__&lt;lookup_type&gt;=&lt;parameter&gt;)\n&gt;&gt;&gt; qs = Elevation.objects.filter(&lt;field&gt;__&lt;lookup_type&gt;=(&lt;raster_input, &lt;band_index&gt;)\n</code></pre></div>\n<p>例如：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">qs</span> <span class=\"o\">=</span> <span class=\"n\">Elevation</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\">rast__contains</span><span class=\"o\">=</span><span class=\"n\">geom</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\">Elevation</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\">rast__contains</span><span class=\"o\">=</span><span class=\"n\">rst</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\">Elevation</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\">rast__1__contains</span><span class=\"o\">=</span><span class=\"n\">geom</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\">Elevation</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\">rast__contains</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">rst</span><span class=\"p\">,</span> <span class=\"mi\">1</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\">Elevation</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\">rast__1__contains</span><span class=\"o\">=</span><span class=\"p\">(</span><span class=\"n\">rst</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>在示例的左侧，<code class=\"docutils literal notranslate\"><span class=\"pre\">rast</span></code> 是地理栅格字段，<a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/geoquerysets/#std-fieldlookup-gis-contains\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">contains</span></code></a> 是空间查找类型。在右侧，<code class=\"docutils literal notranslate\"><span class=\"pre\">geom</span></code> 是一个几何输入，<code class=\"docutils literal notranslate\"><span class=\"pre\">rst</span></code> 是一个 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/gdal/#django.contrib.gis.gdal.GDALRaster\" title=\"django.contrib.gis.gdal.GDALRaster\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GDALRaster</span></code></a> 对象。带索引在前两个查询中默认为 <code class=\"docutils literal notranslate\"><span class=\"pre\">0</span></code>，在其他查询中设置为 <code class=\"docutils literal notranslate\"><span class=\"pre\">1</span></code>。</p>\n<p>虽然所有空间查找都可以在两侧使用栅格对象，但并不是所有底层运算符本身都能接受栅格输入。对于期望几何输入的运算符，栅格会自动转换为几何图形。在解释查找结果时，需要牢记这一点。</p>\n<p>有关所有查找的栅格支持类型，请参阅 <a class=\"reference internal\" href=\"#spatial-lookup-compatibility\"><span class=\"std std-ref\">兼容性表</span></a>。涉及栅格的查找目前仅适用于 PostGIS 后端。</p>\n</section>\n</section>\n<section id=\"distance-queries\">\n<span id=\"id5\"></span><h2>距离查询<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>介绍<a class=\"heading-anchor\" href=\"#introduction\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>使用空间数据进行距离计算很棘手，因为不幸的是，地球不是平的。由于 PostGIS 的限制，一些在地理坐标系统中的距离查询可能必须以不同的方式表示。有关更多详细信息，请参阅 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/model-api/\"><span class=\"doc\">GeoDjango 模型 API</span></a> 文档中的 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/model-api/#selecting-an-srid\"><span class=\"std std-ref\">选择一个 SRID</span></a> 部分。</p>\n</section>\n<section id=\"distance-lookups\">\n<span id=\"distance-lookups-intro\"></span><h3>距离查找<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>可用性</em> ：PostGIS, MariaDB, MySQL, Oracle, SpatiaLite, PGRaster (Native)</p>\n<p>以下是可用的距离查找：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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=\"/zh-hans/5.2/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=\"/zh-hans/5.2/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=\"/zh-hans/5.2/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=\"/zh-hans/5.2/ref/contrib/gis/geoquerysets/#std-fieldlookup-dwithin\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">dwithin</span></code></a> （除了 MariaDB 和 MySQL）</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>对于 <em>测量</em>，而不是查询距离，使用 <code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Distance</span></code> 函数。</p>\n</aside>\n<p>距离查找需要一个元组参数，包括：</p>\n<ol class=\"arabic simple\">\n<li><p>用于计算的几何体或栅格；和</p></li>\n<li><p>一个数字或 <a class=\"reference internal\" href=\"/zh-hans/5.2/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> 对象，包含距离。</p></li>\n</ol>\n<p>如果使用了 <a class=\"reference internal\" href=\"/zh-hans/5.2/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> 对象，它可以用任何单位表示（生成的 SQL 将使用转换为字段单位的单位）；否则，数字参数被认为是字段的单位。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>在 PostGIS 中，<code class=\"docutils literal notranslate\"><span class=\"pre\">ST_Distance_Sphere</span></code> <em>不</em> 限制地理距离查询所使用的几何类型。<a class=\"footnote-reference brackets\" href=\"#fndistsphere15\" id=\"id6\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>3<span class=\"fn-bracket\">]</span></a> 然而，这些查询可能会花费很长时间，因为必须为查询中的 <em>每</em> 一行动态计算大圆距离。这是因为传统几何字段上的空间索引无法使用。</p>\n<p>为了在 WGS84 距离查询中获得更好的性能，考虑在数据库中使用 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/model-api/#geography-type\"><span class=\"std std-ref\">地理列</span></a>，因为它们能够在距离查询中使用它们的空间索引。您可以通过在字段定义中设置 <code class=\"docutils literal notranslate\"><span class=\"pre\">geography=True</span></code> 来告诉 GeoDjango 使用地理列。</p>\n</aside>\n<p>例如，假设我们有一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">SouthTexasCity</span></code> 模型（来自 <a class=\"extlink-source reference external\" href=\"https://github.com/django/django/blob/stable/5.2.x/tests/gis_tests/distapp/models.py\">GeoDjango 距离测试</a> ），在一个适用于德克萨斯南部城市的 <em>投影</em> 坐标系统中：</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\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</code></pre></div>\n<p>然后可以执行以下距离查询：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console 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=\"s2\">&quot;POINT(-96.876369 29.905320)&quot;</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<p>栅格查询的工作方式相同，只需将几何字段 <code class=\"docutils literal notranslate\"><span class=\"pre\">point</span></code> 替换为栅格字段，或将 <code class=\"docutils literal notranslate\"><span class=\"pre\">pnt</span></code> 对象替换为栅格对象，或两者都替换。要在右侧指定栅格输入的带索引，可以将一个 3-元组传递给查找，如下所示：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><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\">rst</span><span class=\"p\">,</span> <span class=\"mi\">2</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</code></pre></div>\n<p>将使用栅格 <code class=\"docutils literal notranslate\"><span class=\"pre\">rst</span></code> 的带索引为 2（第三个带）进行查找。</p>\n</section>\n</section>\n<section id=\"compatibility-tables\">\n<span id=\"compatibility-table\"></span><h2>兼容性表<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=\"id7\"></span><h3>空间查找<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>以下表格提供了每个空间数据库后端可用的空间查找的摘要。PostGIS 栅格（PGRaster）查找分为三个类别，如 <a class=\"reference internal\" href=\"#spatial-lookup-raster\"><span class=\"std std-ref\">栅格查找详细信息</span></a> 中所述：本机支持 <code class=\"docutils literal notranslate\"><span class=\"pre\">N</span></code>、双边本机支持 <code class=\"docutils literal notranslate\"><span class=\"pre\">B</span></code> 和几何转换支持 <code class=\"docutils literal notranslate\"><span class=\"pre\">C</span></code>。</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>查找类型</p></th>\n<th class=\"head\"><p>PostGIS</p></th>\n<th class=\"head\"><p>Oracle</p></th>\n<th class=\"head\"><p>MariaDB</p></th>\n<th class=\"head\"><p>MySQL <a class=\"footnote-reference brackets\" href=\"#id9\" id=\"id8\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>4<span class=\"fn-bracket\">]</span></a></p></th>\n<th class=\"head\"><p>SpatiaLite</p></th>\n<th class=\"head\"><p>PGRaster</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>N</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>N</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>N</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>N</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>N</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>N</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td><p>N</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/geoquerysets/#std-fieldlookup-same_as\"><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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/geoquerysets/#std-fieldlookup-isempty\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">isempty</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/geoquerysets/#std-fieldlookup-isvalid\"><code class=\"xref std std-lookup docutils literal notranslate\"><span class=\"pre\">isvalid</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<td><p>X</p></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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><p>X</p></td>\n<td></td>\n<td><p>X</p></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>B</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>C</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n<td><p>C</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</section>\n<section id=\"database-functions\">\n<span id=\"database-functions-compatibility\"></span><h3>数据库函数<a class=\"heading-anchor\" href=\"#database-functions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>以下表格提供了每个空间后端可用的地理专用数据库函数的摘要。</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>函数</p></th>\n<th class=\"head\"><p>PostGIS</p></th>\n<th class=\"head\"><p>Oracle</p></th>\n<th class=\"head\"><p>MariaDB</p></th>\n<th class=\"head\"><p>MySQL</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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Area\" title=\"django.contrib.gis.db.models.functions.Area\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">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<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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.AsGeoJSON\" title=\"django.contrib.gis.db.models.functions.AsGeoJSON\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AsGeoJSON</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.AsGML\" title=\"django.contrib.gis.db.models.functions.AsGML\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AsGML</span></code></a></p></td>\n<td><p>X</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-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.AsKML\" title=\"django.contrib.gis.db.models.functions.AsKML\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AsKML</span></code></a></p></td>\n<td><p>X</p></td>\n<td></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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.AsSVG\" title=\"django.contrib.gis.db.models.functions.AsSVG\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AsSVG</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.AsWKB\" title=\"django.contrib.gis.db.models.functions.AsWKB\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AsWKB</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.AsWKT\" title=\"django.contrib.gis.db.models.functions.AsWKT\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AsWKT</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Azimuth\" title=\"django.contrib.gis.db.models.functions.Azimuth\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Azimuth</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td><p>X (LWGEOM/RTTOPO)</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.BoundingCircle\" title=\"django.contrib.gis.db.models.functions.BoundingCircle\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">BoundingCircle</span></code></a></p></td>\n<td><p>X</p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td><p>X (≥ 5.1)</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Centroid\" title=\"django.contrib.gis.db.models.functions.Centroid\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">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<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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.ClosestPoint\" title=\"django.contrib.gis.db.models.functions.ClosestPoint\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ClosestPoint</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Difference\" title=\"django.contrib.gis.db.models.functions.Difference\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">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<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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Distance\" title=\"django.contrib.gis.db.models.functions.Distance\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">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<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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Envelope\" title=\"django.contrib.gis.db.models.functions.Envelope\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Envelope</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.ForcePolygonCW\" title=\"django.contrib.gis.db.models.functions.ForcePolygonCW\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ForcePolygonCW</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.FromWKB\" title=\"django.contrib.gis.db.models.functions.FromWKB\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FromWKB</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.FromWKT\" title=\"django.contrib.gis.db.models.functions.FromWKT\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">FromWKT</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.GeoHash\" title=\"django.contrib.gis.db.models.functions.GeoHash\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GeoHash</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td><p>X</p></td>\n<td><p>X (LWGEOM/RTTOPO)</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.GeometryDistance\" title=\"django.contrib.gis.db.models.functions.GeometryDistance\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">GeometryDistance</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Intersection\" title=\"django.contrib.gis.db.models.functions.Intersection\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">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<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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.IsEmpty\" title=\"django.contrib.gis.db.models.functions.IsEmpty\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">IsEmpty</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.IsValid\" title=\"django.contrib.gis.db.models.functions.IsValid\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">IsValid</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Length\" title=\"django.contrib.gis.db.models.functions.Length\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">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<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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.LineLocatePoint\" title=\"django.contrib.gis.db.models.functions.LineLocatePoint\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LineLocatePoint</span></code></a></p></td>\n<td><p>X</p></td>\n<td></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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.MakeValid\" title=\"django.contrib.gis.db.models.functions.MakeValid\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MakeValid</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td><p>X (LWGEOM/RTTOPO)</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.MemSize\" title=\"django.contrib.gis.db.models.functions.MemSize\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MemSize</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.NumGeometries\" title=\"django.contrib.gis.db.models.functions.NumGeometries\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">NumGeometries</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.NumPoints\" title=\"django.contrib.gis.db.models.functions.NumPoints\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">NumPoints</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Perimeter\" title=\"django.contrib.gis.db.models.functions.Perimeter\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Perimeter</span></code></a></p></td>\n<td><p>X</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-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.PointOnSurface\" title=\"django.contrib.gis.db.models.functions.PointOnSurface\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">PointOnSurface</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></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Reverse\" title=\"django.contrib.gis.db.models.functions.Reverse\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Reverse</span></code></a></p></td>\n<td><p>X</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-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Scale\" title=\"django.contrib.gis.db.models.functions.Scale\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Scale</span></code></a></p></td>\n<td><p>X</p></td>\n<td></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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.SnapToGrid\" title=\"django.contrib.gis.db.models.functions.SnapToGrid\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SnapToGrid</span></code></a></p></td>\n<td><p>X</p></td>\n<td></td>\n<td></td>\n<td></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.SymDifference\" title=\"django.contrib.gis.db.models.functions.SymDifference\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">SymDifference</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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Transform\" title=\"django.contrib.gis.db.models.functions.Transform\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Transform</span></code></a></p></td>\n<td><p>X</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-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Translate\" title=\"django.contrib.gis.db.models.functions.Translate\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Translate</span></code></a></p></td>\n<td><p>X</p></td>\n<td></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=\"/zh-hans/5.2/ref/contrib/gis/functions/#django.contrib.gis.db.models.functions.Union\" title=\"django.contrib.gis.db.models.functions.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<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>聚合函数<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>下表总结了每个空间后端上可用的 GIS 特定聚合函数。请注意，MariaDB 不支持这些聚合函数，因此未包含在表中。</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>聚合</p></th>\n<th class=\"head\"><p>PostGIS</p></th>\n<th class=\"head\"><p>Oracle</p></th>\n<th class=\"head\"><p>MySQL</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=\"/zh-hans/5.2/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>X (≥ 8.0.24)</p></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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></td>\n<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td></td>\n</tr>\n<tr class=\"row-odd\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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<td><p>X</p></td>\n</tr>\n<tr class=\"row-even\"><td><p><a class=\"reference internal\" href=\"/zh-hans/5.2/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></td>\n<td><p>X</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p class=\"rubric\">脚注</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=\"#id3\">1</a><span class=\"fn-bracket\">]</span></span>\n<p>请参阅 Open Geospatial Consortium, Inc. 的 <a class=\"reference external\" href=\"https://portal.ogc.org/files/?artifact_id=829\">OpenGIS Simple Feature Specification For SQL</a>，文件编号 99-049（1999 年 5 月 5 日），第 3.2.5 章，第 3-11 页（几何图形的 SQL 文本表示）。</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=\"#id4\">2</a><span class=\"fn-bracket\">]</span></span>\n<p>请参阅 <a class=\"reference external\" href=\"https://postgis.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT\">PostGIS EWKB、EWKT 和规范形式</a>，PostGIS 文档第 4.1.2 章。</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=\"#id6\">3</a><span class=\"fn-bracket\">]</span></span>\n<p>请参阅 <a class=\"reference external\" href=\"https://postgis.net/docs/ST_DistanceSphere.html\">PostGIS 文档</a> 中关于 <code class=\"docutils literal notranslate\"><span class=\"pre\">ST_DistanceSphere</span></code> 的部分。</p>\n</aside>\n<aside class=\"footnote brackets\" id=\"id9\" 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>详情请参考 <a class=\"reference internal\" href=\"#mysql-spatial-limitations\"><span class=\"std std-ref\">MySQL 的空间限制</span></a> 部分。</p>\n</aside>\n</aside>\n</section>\n</section>","rootId":"geodjango-database-api","toc":[{"title":"空间后端","anchor":"module-django.contrib.gis.db.backends","children":[{"title":"MySQL 的空间限制","anchor":"mysql-spatial-limitations","children":[]},{"title":"栅格支持","anchor":"raster-support","children":[]}]},{"title":"创建和保存具有几何字段的模型","anchor":"creating-and-saving-models-with-geometry-fields","children":[]},{"title":"使用栅格字段创建和保存模型","anchor":"creating-and-saving-models-with-raster-fields","children":[]},{"title":"空间查找","anchor":"spatial-lookups","children":[{"title":"空间查找","anchor":"geometry-lookups","children":[]},{"title":"栅格查找","anchor":"raster-lookups","children":[]}]},{"title":"距离查询","anchor":"distance-queries","children":[{"title":"介绍","anchor":"introduction","children":[]},{"title":"距离查找","anchor":"distance-lookups","children":[]}]},{"title":"兼容性表","anchor":"compatibility-tables","children":[{"title":"空间查找","anchor":"spatial-lookup-compatibility","children":[]},{"title":"数据库函数","anchor":"database-functions","children":[]},{"title":"聚合函数","anchor":"aggregate-functions","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/5.2/ref/"},{"docname":"ref/contrib/index","title":"contrib 包","url":"/zh-hans/5.2/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/zh-hans/5.2/ref/contrib/gis/"}],"prev":{"docname":"ref/contrib/gis/model-api","title":"GeoDjango 模型 API","url":"/zh-hans/5.2/ref/contrib/gis/model-api/"},"next":{"docname":"ref/contrib/gis/forms-api","title":"GeoDjango 表单 API","url":"/zh-hans/5.2/ref/contrib/gis/forms-api/"},"formats":{"html":"/zh-hans/5.2/ref/contrib/gis/db-api/","markdown":"/zh-hans/5.2/ref/contrib/gis/db-api.md","json":"/zh-hans/5.2/ref/contrib/gis/db-api.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/ref/contrib/gis/db-api.txt","official":"https://docs.djangoproject.com/zh-hans/5.2/ref/contrib/gis/db-api/","inVersions":["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"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}