“Django/docs/3.0.x/ref/contrib/gis/geoquerysets”的版本间差异
(autoload) |
小 (Page commit) |
||
第1行: | 第1行: | ||
+ | {{DISPLAYTITLE:GIS QuerySet API 参考 — Django 文档}} | ||
<div id="gis-queryset-api-reference" class="section"> | <div id="gis-queryset-api-reference" class="section"> | ||
− | = GIS QuerySet API | + | = GIS QuerySet API 参考 = |
<div id="spatial-lookups" class="section"> | <div id="spatial-lookups" class="section"> | ||
<span id="id1"></span> | <span id="id1"></span> | ||
− | == | + | == 空间查找 == |
− | + | 本节中的空间查找可用于 [[../model-api#django.contrib.gis.db.models|GeometryField]] 和 [[../model-api#django.contrib.gis.db.models|RasterField]]。 | |
− | |||
− | + | 有关介绍,请参阅 [[../db-api#spatial-lookups-intro|空间查找介绍]] 。 有关哪些查找与特定空间后端兼容的概述,请参阅 [[../db-api#spatial-lookup-compatibility|空间查找兼容性表]] 。 | |
− | |||
− | [[../db-api#spatial-lookup-compatibility| | ||
<div id="lookups-with-rasters" class="section"> | <div id="lookups-with-rasters" class="section"> | ||
− | === | + | === 使用栅格查找 === |
− | + | 下面参考中的所有示例都是针对几何字段和输入给出的,但查找可以以相同的方式用于两侧的栅格。 每当查找不支持栅格输入时,输入会在必要时使用 [https://postgis.net/docs/RT_ST_Polygon.html ST_Polygon] 函数自动转换为几何图形。 另请参阅 [[../db-api#spatial-lookup-raster|对栅格查找的介绍]] 。 | |
− | |||
− | |||
− | |||
− | [[../db-api#spatial-lookup-raster| | ||
− | + | 查找使用的数据库运算符可以分为三类: | |
− | * | + | * 原生栅格支持 <code>N</code>:运算符在查找的两侧原生接受栅格,并且栅格输入可以与几何输入混合。 |
− | * | + | * 双边栅格支持 <code>B</code>:仅当查找的两侧都接收栅格输入时,运算符才支持栅格。 栅格数据会自动转换为几何图形以进行混合查找。 |
− | * | + | * 几何转换支持 <code>C</code>。 查找没有原生栅格支持,所有栅格数据都会自动转换为几何图形。 |
− | + | 下面的示例显示了在不同类型的栅格支持中查找的 SQL 等效项。 相同的模式适用于所有空间查找。 | |
− | |||
{| | {| | ||
− | !width="4%"| | + | !width="4%"| 案例 |
− | !width="34%"| | + | !width="34%"| 查找 |
− | !width="62%"| SQL | + | !width="62%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 否,乙 |
| <code>rast__contains=rst</code> | | <code>rast__contains=rst</code> | ||
| <code>ST_Contains(rast, rst)</code> | | <code>ST_Contains(rast, rst)</code> | ||
|- | |- | ||
− | | | + | | 否,乙 |
| <code>rast__1__contains=(rst, 2)</code> | | <code>rast__1__contains=(rst, 2)</code> | ||
| <code>ST_Contains(rast, 1, rst, 2)</code> | | <code>ST_Contains(rast, 1, rst, 2)</code> | ||
|- | |- | ||
− | | | + | | 乙、丙 |
| <code>rast__contains=geom</code> | | <code>rast__contains=geom</code> | ||
| <code>ST_Contains(ST_Polygon(rast), geom)</code> | | <code>ST_Contains(ST_Polygon(rast), geom)</code> | ||
|- | |- | ||
− | | | + | | 乙、丙 |
| <code>rast__1__contains=geom</code> | | <code>rast__1__contains=geom</code> | ||
| <code>ST_Contains(ST_Polygon(rast, 1), geom)</code> | | <code>ST_Contains(ST_Polygon(rast, 1), geom)</code> | ||
|- | |- | ||
− | | | + | | 乙、丙 |
| <code>poly__contains=rst</code> | | <code>poly__contains=rst</code> | ||
| <code>ST_Contains(poly, ST_Polygon(rst))</code> | | <code>ST_Contains(poly, ST_Polygon(rst))</code> | ||
|- | |- | ||
− | | | + | | 乙、丙 |
| <code>poly__contains=(rst, 1)</code> | | <code>poly__contains=(rst, 1)</code> | ||
| <code>ST_Contains(poly, ST_Polygon(rst, 1))</code> | | <code>ST_Contains(poly, ST_Polygon(rst, 1))</code> | ||
第80行: | 第73行: | ||
|} | |} | ||
− | + | 仅 PostGIS 后端(在本节中称为 PGRaster)支持使用栅格进行空间查找。 | |
− | |||
第87行: | 第79行: | ||
<div id="bbcontains" class="section"> | <div id="bbcontains" class="section"> | ||
− | + | === bbcontains === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Contain.html PostGIS]、MariaDB、MySQL、SpatiaLite、PGRaster(原生) |
− | |||
− | + | 测试几何或栅格字段的边界框是否完全包含查找几何的边界框。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第102行: | 第91行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__bbcontains=geom)</syntaxhighlight> |
</div> | </div> | ||
第108行: | 第97行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="27%"| | + | !width="27%"| 后端 |
− | !width="73%"| SQL | + | !width="73%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>poly ~ geom</code> | | <code>poly ~ geom</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>MBRContains(poly, geom)</code> | | <code>MBRContains(poly, geom)</code> | ||
|- | |- | ||
第128行: | 第117行: | ||
<div id="bboverlaps" class="section"> | <div id="bboverlaps" class="section"> | ||
− | + | === bboverlaps === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/geometry_overlaps.html PostGIS]、MariaDB、MySQL、SpatiaLite、PGRaster(原生) |
− | |||
− | + | 测试几何字段的边界框是否与查找几何的边界框重叠。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第143行: | 第129行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__bboverlaps=geom)</syntaxhighlight> |
</div> | </div> | ||
第149行: | 第135行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="27%"| | + | !width="27%"| 后端 |
− | !width="73%"| SQL | + | !width="73%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>poly && geom</code> | | <code>poly && geom</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>MBROverlaps(poly, geom)</code> | | <code>MBROverlaps(poly, geom)</code> | ||
|- | |- | ||
第169行: | 第155行: | ||
<div id="contained" class="section"> | <div id="contained" class="section"> | ||
− | + | === contained === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Contained.html PostGIS]、MariaDB、MySQL、SpatiaLite、PGRaster(原生) |
− | |||
− | + | 测试几何字段的边界框是否完全包含在查找几何的边界框内。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第184行: | 第167行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__contained=geom)</syntaxhighlight> |
</div> | </div> | ||
第190行: | 第173行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>poly @ geom</code> | | <code>poly @ geom</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>MBRWithin(poly, geom)</code> | | <code>MBRWithin(poly, geom)</code> | ||
|- | |- | ||
第210行: | 第193行: | ||
<div id="contains" class="section"> | <div id="contains" class="section"> | ||
− | + | === contains === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Contains.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段在空间上是否包含查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第224行: | 第205行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__contains=geom)</syntaxhighlight> |
</div> | </div> | ||
第230行: | 第211行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="26%"| | + | !width="26%"| 后端 |
− | !width="74%"| SQL | + | !width="74%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Contains(poly, geom)</code> | | <code>ST_Contains(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_CONTAINS(poly, geom)</code> | | <code>SDO_CONTAINS(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Contains(poly, geom)</code> | | <code>ST_Contains(poly, geom)</code> | ||
|- | |- | ||
第251行: | 第232行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBRContains</code> 并且仅对边界框进行操作。 | |
− | |||
第260行: | 第240行: | ||
<div id="contains-properly" class="section"> | <div id="contains-properly" class="section"> | ||
− | + | === contains_properly === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_ContainsProperly.html PostGIS],PGRaster(双边) |
− | |||
− | + | 如果查找几何与几何字段的内部相交,但不与边界(或外部)相交,则返回 true。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第275行: | 第252行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__contains_properly=geom)</syntaxhighlight> |
</div> | </div> | ||
第281行: | 第258行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="22%"| | + | !width="22%"| 后端 |
− | !width="78%"| SQL | + | !width="78%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_ContainsProperly(poly, geom)</code> | | <code>ST_ContainsProperly(poly, geom)</code> | ||
|} | |} | ||
第292行: | 第269行: | ||
<div id="coveredby" class="section"> | <div id="coveredby" class="section"> | ||
− | + | === coveredby === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_CoveredBy.html PostGIS]、Oracle、PGRaster(双边)、SpatiaLite |
− | |||
− | + | 测试几何字段中是否没有点位于查找几何之外。 [[#fncovers|3]] | |
− | [[#fncovers|3]] | ||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第307行: | 第281行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__coveredby=geom)</syntaxhighlight> |
</div> | </div> | ||
第314行: | 第288行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | SpatiaLite | + | <span class="versionmodified changed"> 2.2 版更改:添加了 </span> SpatiaLite 支持。 |
</div> | </div> | ||
{| | {| | ||
− | !width="26%"| | + | !width="26%"| 后端 |
− | !width="74%"| SQL | + | !width="74%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_CoveredBy(poly, geom)</code> | | <code>ST_CoveredBy(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_COVEREDBY(poly, geom)</code> | | <code>SDO_COVEREDBY(poly, geom)</code> | ||
|- | |- | ||
第336行: | 第310行: | ||
<div id="covers" class="section"> | <div id="covers" class="section"> | ||
− | + | === covers === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Covers.html PostGIS]、Oracle、PGRaster(双边)、SpatiaLite |
− | |||
− | + | 测试查找几何中是否没有点位于几何字段之外。 [[#fncovers|3]] | |
− | [[#fncovers|3]] | ||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第351行: | 第322行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__covers=geom)</syntaxhighlight> |
</div> | </div> | ||
第358行: | 第329行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | SpatiaLite | + | <span class="versionmodified changed"> 2.2 版更改:添加了 </span> SpatiaLite 支持。 |
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Covers(poly, geom)</code> | | <code>ST_Covers(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_COVERS(poly, geom)</code> | | <code>SDO_COVERS(poly, geom)</code> | ||
|- | |- | ||
第380行: | 第351行: | ||
<div id="crosses" class="section"> | <div id="crosses" class="section"> | ||
− | + | === crosses === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Crosses.html PostGIS]、MariaDB、MySQL、SpatiaLite、PGRaster(转换) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上交叉。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第394行: | 第363行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__crosses=geom)</syntaxhighlight> |
</div> | </div> | ||
第400行: | 第369行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Crosses(poly, geom)</code> | | <code>ST_Crosses(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Crosses(poly, geom)</code> | | <code>ST_Crosses(poly, geom)</code> | ||
|- | |- | ||
第418行: | 第387行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | MySQL | + | <span class="versionmodified changed"> 3.0 版更改:</span> 增加了 MySQL 支持。 |
第426行: | 第395行: | ||
<div id="disjoint" class="section"> | <div id="disjoint" class="section"> | ||
− | + | === disjoint === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Disjoint.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上不相交。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第440行: | 第407行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__disjoint=geom)</syntaxhighlight> |
</div> | </div> | ||
第446行: | 第413行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Disjoint(poly, geom)</code> | | <code>ST_Disjoint(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)</code> | | <code>SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Disjoint(poly, geom)</code> | | <code>ST_Disjoint(poly, geom)</code> | ||
|- | |- | ||
第467行: | 第434行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBRDisjoint</code> 并且仅对边界框进行操作。 | |
− | |||
第476行: | 第442行: | ||
<div id="equals" class="section"> | <div id="equals" class="section"> | ||
− | + | === equals === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Equals.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(转换) |
− | |||
− | + | 测试几何字段在空间上是否等于查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第490行: | 第454行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__equals=geom)</syntaxhighlight> |
</div> | </div> | ||
第496行: | 第460行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Equals(poly, geom)</code> | | <code>ST_Equals(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_EQUAL(poly, geom)</code> | | <code>SDO_EQUAL(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Equals(poly, geom)</code> | | <code>ST_Equals(poly, geom)</code> | ||
|- | |- | ||
第517行: | 第481行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBREquals</code> 并且仅对边界框进行操作。 | |
− | |||
</div> | </div> | ||
− | |||
</div> | </div> | ||
<div id="exact-same-as" class="section"> | <div id="exact-same-as" class="section"> | ||
− | + | === exact、same_as === | |
− | = | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Same.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否“等于”查找几何。 在 Oracle、MySQL 和 SpatiaLite 上,它测试空间相等性,而在 PostGIS 上,它测试边界框的相等性。 | |
− | |||
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第543行: | 第501行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly=geom)</syntaxhighlight> |
</div> | </div> | ||
第549行: | 第507行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>poly ~= geom</code> | | <code>poly ~= geom</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_EQUAL(poly, geom)</code> | | <code>SDO_EQUAL(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Equals(poly, geom)</code> | | <code>ST_Equals(poly, geom)</code> | ||
|- | |- | ||
第570行: | 第528行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBREquals</code> 并且仅对边界框进行操作。 | |
− | |||
第579行: | 第536行: | ||
<div id="intersects" class="section"> | <div id="intersects" class="section"> | ||
− | + | === intersects === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Intersects.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上相交。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第593行: | 第548行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__intersects=geom)</syntaxhighlight> |
</div> | </div> | ||
第599行: | 第554行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Intersects(poly, geom)</code> | | <code>ST_Intersects(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_OVERLAPBDYINTERSECT(poly, geom)</code> | | <code>SDO_OVERLAPBDYINTERSECT(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Intersects(poly, geom)</code> | | <code>ST_Intersects(poly, geom)</code> | ||
|- | |- | ||
第620行: | 第575行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBRIntersects</code> 并且仅对边界框进行操作。 | |
− | |||
第629行: | 第583行: | ||
<div id="isvalid" class="section"> | <div id="isvalid" class="section"> | ||
− | + | === isvalid === | |
− | === | ||
− | '' | + | ''可用性'':MySQL (≥ 5.7.5)、[https://postgis.net/docs/ST_IsValid.html PostGIS]、Oracle、SpatiaLite |
− | + | 测试几何图形是否有效。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第642行: | 第595行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__isvalid=True)</syntaxhighlight> |
</div> | </div> | ||
第648行: | 第601行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="71%"| SQL | + | !width="71%"| SQL 等价物 |
|- | |- | ||
− | | | + | | MySQL、PostGIS、SpatiaLite |
| <code>ST_IsValid(poly)</code> | | <code>ST_IsValid(poly)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE'</code> | | <code>SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE'</code> | ||
|} | |} | ||
第662行: | 第615行: | ||
<div id="overlaps" class="section"> | <div id="overlaps" class="section"> | ||
− | + | === overlaps === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Overlaps.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上重叠。 | |
{| | {| | ||
− | !width="26%"| | + | !width="26%"| 后端 |
− | !width="74%"| SQL | + | !width="74%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Overlaps(poly, geom)</code> | | <code>ST_Overlaps(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_OVERLAPS(poly, geom)</code> | | <code>SDO_OVERLAPS(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Overlaps(poly, geom)</code> | | <code>ST_Overlaps(poly, geom)</code> | ||
|- | |- | ||
第692行: | 第643行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBROverlaps</code> 并且仅对边界框进行操作。 | |
− | |||
第701行: | 第651行: | ||
<div id="relate" class="section"> | <div id="relate" class="section"> | ||
− | + | === relate === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Relate.html PostGIS]、Oracle、SpatiaLite、PGRaster(转换) |
− | |||
− | + | 通过给定模式中给定的值测试几何字段是否与查找几何在空间上相关。 这个查找需要一个元组参数,<code>(geom, pattern)</code>; <code>pattern</code> 的形式将取决于空间后端: | |
− | |||
− | <code>(geom, pattern)</code> | ||
<div id="postgis-spatialite" class="section"> | <div id="postgis-spatialite" class="section"> | ||
− | ==== PostGIS | + | ==== PostGIS 和 SpatiaLite ==== |
− | + | 在这些空间后端上,相交模式是一个由九个字符组成的字符串,用于定义几何字段与查找几何的内部、边界和外部之间的交集。 交集模式矩阵只能使用以下字符:<code>1</code>、<code>2</code>、<code>T</code>、<code>F</code>或<code>*</code>。 这种查找类型允许用户“微调”与 DE-9IM 模型一致的特定几何关系。 [[#fnde9im|1]] | |
− | |||
− | |||
− | |||
− | <code>1</code> | ||
− | |||
− | + | 几何示例: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第728行: | 第669行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python"># A tuple lookup parameter is used to specify the geometry and |
# the intersection pattern (the pattern here is for 'contains'). | # the intersection pattern (the pattern here is for 'contains'). | ||
− | Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))</ | + | Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS SQL | + | PostGIS SQL 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第741行: | 第682行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | SpatiaLite SQL | + | SpatiaLite SQL 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第752行: | 第693行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 光栅示例: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第763行: | 第704行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__relate=(rast, 1, 'T*T***FF*')) |
− | Zipcode.objects.filter(rast__2__relate=(rast, 1, 'T*T***FF*'))</ | + | Zipcode.objects.filter(rast__2__relate=(rast, 1, 'T*T***FF*'))</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS SQL | + | PostGIS SQL 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第775行: | 第716行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE ST_Relate(poly, ST_Polygon(rast, 1), 'T*T***FF*') |
− | SELECT ... WHERE ST_Relate(ST_Polygon(rast, 2), ST_Polygon(rast, 1), 'T*T***FF*')</ | + | SELECT ... WHERE ST_Relate(ST_Polygon(rast, 2), ST_Polygon(rast, 1), 'T*T***FF*')</syntaxhighlight> |
</div> | </div> | ||
第785行: | 第726行: | ||
<div id="oracle" class="section"> | <div id="oracle" class="section"> | ||
− | ==== | + | ==== 甲骨文 ==== |
− | + | 这里的关系模式由以下九个关系串中的至少一个组成:<code>TOUCH</code>、<code>OVERLAPBDYDISJOINT</code>、<code>OVERLAPBDYINTERSECT</code>、<code>EQUAL</code>、<code>INSIDE</code> 、<code>COVEREDBY</code>、<code>CONTAINS</code>、<code>COVERS</code>、<code>ON</code> 和 <code>ANYINTERACT</code>。 多个字符串可以用逻辑布尔运算符 OR 组合,例如,<code>'inside+touch'</code>。 [[#fnsdorelate|2]] 关系字符串不区分大小写。 | |
− | |||
− | <code>EQUAL</code> | ||
− | <code>ANYINTERACT</code> | ||
− | |||
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第800行: | 第736行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | Oracle SQL | + | Oracle SQL 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第811行: | 第747行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')</syntaxhighlight> |
</div> | </div> | ||
第822行: | 第758行: | ||
<div id="touches" class="section"> | <div id="touches" class="section"> | ||
− | + | === touches === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Touches.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite |
− | |||
− | + | 测试几何字段是否在空间上接触查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第836行: | 第770行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__touches=geom)</syntaxhighlight> |
</div> | </div> | ||
第842行: | 第776行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Touches(poly, geom)</code> | | <code>ST_Touches(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Touches(poly, geom)</code> | | <code>ST_Touches(poly, geom)</code> | ||
|- | |- | ||
第854行: | 第788行: | ||
| <code>ST_Touches(poly, geom)</code> | | <code>ST_Touches(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_TOUCH(poly, geom)</code> | | <code>SDO_TOUCH(poly, geom)</code> | ||
|- | |- | ||
第863行: | 第797行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBRTouches</code> 并且仅对边界框进行操作。 | |
− | |||
第872行: | 第805行: | ||
<div id="within" class="section"> | <div id="within" class="section"> | ||
− | + | === within === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Within.html PostGIS]、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否在空间上查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第886行: | 第817行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__within=geom)</syntaxhighlight> |
</div> | </div> | ||
第892行: | 第823行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Within(poly, geom)</code> | | <code>ST_Within(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Within(poly, geom)</code> | | <code>ST_Within(poly, geom)</code> | ||
|- | |- | ||
第904行: | 第835行: | ||
| <code>ST_Within(poly, geom)</code> | | <code>ST_Within(poly, geom)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_INSIDE(poly, geom)</code> | | <code>SDO_INSIDE(poly, geom)</code> | ||
|- | |- | ||
第913行: | 第844行: | ||
<div class="versionchanged"> | <div class="versionchanged"> | ||
− | + | <span class="versionmodified changed"> 3.0 版本变化: </span> 在旧版本中,MySQL 使用 <code>MBRWithin</code> 并且仅对边界框进行操作。 | |
− | |||
第922行: | 第852行: | ||
<div id="left" class="section"> | <div id="left" class="section"> | ||
− | + | === left === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Left.html PostGIS],PGRaster(转换) |
− | |||
− | + | 测试几何字段的边界框是否严格位于查找几何的边界框的左侧。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第937行: | 第864行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__left=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第948行: | 第875行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly << geom</syntaxhighlight> |
</div> | </div> | ||
第957行: | 第884行: | ||
<div id="right" class="section"> | <div id="right" class="section"> | ||
− | + | === right === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Right.html PostGIS],PGRaster(转换) |
− | |||
− | + | 测试几何字段的边界框是否严格位于查找几何的边界框的右侧。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第972行: | 第896行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__right=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第983行: | 第907行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly >> geom</syntaxhighlight> |
</div> | </div> | ||
第992行: | 第916行: | ||
<div id="overlaps-left" class="section"> | <div id="overlaps-left" class="section"> | ||
− | + | === overlaps_left === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Overleft.html PostGIS],PGRaster(双边) |
− | |||
− | + | 测试几何字段的边界框是否与查找几何的边界框重叠或位于其左侧。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,007行: | 第928行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__overlaps_left=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第1,018行: | 第939行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly &< geom</syntaxhighlight> |
</div> | </div> | ||
第1,027行: | 第948行: | ||
<div id="overlaps-right" class="section"> | <div id="overlaps-right" class="section"> | ||
− | + | === overlaps_right === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Overright.html PostGIS],PGRaster(双边) |
− | |||
− | + | 测试几何字段的边界框是否与查找几何的边界框重叠或位于其右侧。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,042行: | 第960行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__overlaps_right=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第1,053行: | 第971行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly &> geom</syntaxhighlight> |
</div> | </div> | ||
第1,062行: | 第980行: | ||
<div id="overlaps-above" class="section"> | <div id="overlaps-above" class="section"> | ||
− | + | === overlaps_above === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Overabove.html PostGIS],PGRaster(转换) |
− | |||
− | + | 测试几何字段的边界框是否与查找几何的边界框重叠或位于其上方。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,077行: | 第992行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__overlaps_above=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第1,088行: | 第1,003行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly |&> geom</syntaxhighlight> |
</div> | </div> | ||
第1,097行: | 第1,012行: | ||
<div id="overlaps-below" class="section"> | <div id="overlaps-below" class="section"> | ||
− | + | === overlaps_below === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Overbelow.html PostGIS],PGRaster(转换) |
− | |||
− | + | 测试几何字段的边界框是否重叠或低于查找几何的边界框。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,112行: | 第1,024行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__overlaps_below=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第1,123行: | 第1,035行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly &<| geom</syntaxhighlight> |
</div> | </div> | ||
第1,132行: | 第1,044行: | ||
<div id="strictly-above" class="section"> | <div id="strictly-above" class="section"> | ||
− | + | === strictly_above === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Above.html PostGIS],PGRaster(转换) |
− | |||
− | + | 测试几何字段的边界框是否严格高于查找几何的边界框。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,147行: | 第1,056行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__strictly_above=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第1,158行: | 第1,067行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly |>> geom</syntaxhighlight> |
</div> | </div> | ||
第1,167行: | 第1,076行: | ||
<div id="strictly-below" class="section"> | <div id="strictly-below" class="section"> | ||
− | + | === strictly_below === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Below.html PostGIS],PGRaster(转换) |
− | |||
− | + | 测试几何字段的边界框是否严格低于查找几何的边界框。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,182行: | 第1,088行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__strictly_below=geom)</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | PostGIS | + | PostGIS 等效项: |
<div class="highlight-sql notranslate"> | <div class="highlight-sql notranslate"> | ||
第1,193行: | 第1,099行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="sql">SELECT ... WHERE poly <<| geom</syntaxhighlight> |
</div> | </div> | ||
第1,205行: | 第1,111行: | ||
<span id="id6"></span> | <span id="id6"></span> | ||
− | == | + | == 距离查找 == |
− | '' | + | ''可用性'':PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(原生) |
− | + | 有关执行距离查询的概述,请参阅 [[../db-api#distance-queries|距离查询介绍]] 。 | |
− | |||
− | + | 距离查找采用以下形式: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,218行: | 第1,123行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python"><field>__<distance lookup>=(<geometry/raster>, <distance value>[, 'spheroid']) |
− | + | <field>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid']) | |
− | + | <field>__<band_index>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])</syntaxhighlight> | |
</div> | </div> | ||
</div> | </div> | ||
− | + | 传递给距离查找的值是一个元组; 前两个值是强制性的,并且是计算到的距离的几何图形和一个距离值(以字段为单位的数字、[[../measure#django.contrib.gis.measure|Distance]] 对象或 [[../../../models/expressions|查询表达式]] )。 要将波段索引传递给查找,请使用 3 元组,其中第二个条目是波段索引。 | |
− | |||
− | |||
− | [[../measure#django.contrib.gis.measure| | ||
− | |||
− | |||
− | + | 在除 [[#id7|:lookup:`dwithin`]] 之外的每个距离查找中,可以包含一个可选元素 <code>'spheroid'</code>,以便在具有大地坐标系的字段上使用更准确的球体距离计算函数。 | |
− | <code>'spheroid'</code> | ||
− | |||
− | + | 在 PostgreSQL 上,<code>'spheroid'</code> 选项使用 [https://postgis.net/docs/ST_Distance_Spheroid.html ST_DistanceSpheroid] 而不是 [https://postgis.net/docs/ST_DistanceSphere.html ST_DistanceSphere]。 更简单的 [https://postgis.net/docs/ST_Distance.html ST_Distance] 函数用于投影坐标系。 栅格被转换为几何图形以进行基于球体的查找。 | |
− | [https://postgis.net/docs/ST_DistanceSphere.html ST_DistanceSphere] | ||
− | |||
− | |||
− | |||
<div id="distance-gt" class="section"> | <div id="distance-gt" class="section"> | ||
− | + | === distance_gt === | |
− | === | ||
− | + | 返回从查找几何到几何字段的距离大于给定距离值的模型。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,256行: | 第1,148行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,262行: | 第1,154行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Distance/ST_Distance_Sphere(poly, geom) > 5</code> | | <code>ST_Distance/ST_Distance_Sphere(poly, geom) > 5</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Distance(poly, geom) > 5</code> | | <code>ST_Distance(poly, geom) > 5</code> | ||
|- | |- | ||
第1,274行: | 第1,166行: | ||
| <code>ST_Distance(poly, geom) > 5</code> | | <code>ST_Distance(poly, geom) > 5</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5</code> | | <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5</code> | ||
|- | |- | ||
第1,285行: | 第1,177行: | ||
<div id="distance-gte" class="section"> | <div id="distance-gte" class="section"> | ||
− | + | === distance_gte === | |
− | === | ||
− | + | 返回从查找几何到几何字段的距离大于或等于给定距离值的模型。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,297行: | 第1,187行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,303行: | 第1,193行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="16%"| | + | !width="16%"| 后端 |
− | !width="84%"| SQL | + | !width="84%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Distance/ST_Distance_Sphere(poly, geom) >= 5</code> | | <code>ST_Distance/ST_Distance_Sphere(poly, geom) >= 5</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Distance(poly, geom) >= 5</code> | | <code>ST_Distance(poly, geom) >= 5</code> | ||
|- | |- | ||
第1,315行: | 第1,205行: | ||
| <code>ST_Distance(poly, geom) >= 5</code> | | <code>ST_Distance(poly, geom) >= 5</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5</code> | | <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5</code> | ||
|- | |- | ||
第1,326行: | 第1,216行: | ||
<div id="distance-lt" class="section"> | <div id="distance-lt" class="section"> | ||
− | + | === distance_lt === | |
− | === | ||
− | + | 返回从查找几何到几何字段的距离小于给定距离值的模型。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,338行: | 第1,226行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,344行: | 第1,232行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Distance/ST_Distance_Sphere(poly, geom) < 5</code> | | <code>ST_Distance/ST_Distance_Sphere(poly, geom) < 5</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Distance(poly, geom) < 5</code> | | <code>ST_Distance(poly, geom) < 5</code> | ||
|- | |- | ||
第1,356行: | 第1,244行: | ||
| <code>ST_Distance(poly, geom) < 5</code> | | <code>ST_Distance(poly, geom) < 5</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5</code> | | <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5</code> | ||
|- | |- | ||
第1,367行: | 第1,255行: | ||
<div id="distance-lte" class="section"> | <div id="distance-lte" class="section"> | ||
− | + | === distance_lte === | |
− | === | ||
− | + | 返回从查找几何到几何字段的距离小于或等于给定距离值的模型。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,379行: | 第1,265行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,385行: | 第1,271行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="16%"| | + | !width="16%"| 后端 |
− | !width="84%"| SQL | + | !width="84%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_Distance/ST_Distance_Sphere(poly, geom) <= 5</code> | | <code>ST_Distance/ST_Distance_Sphere(poly, geom) <= 5</code> | ||
|- | |- | ||
− | | | + | | 玛丽亚数据库 |
| <code>ST_Distance(poly, geom) <= 5</code> | | <code>ST_Distance(poly, geom) <= 5</code> | ||
|- | |- | ||
第1,397行: | 第1,283行: | ||
| <code>ST_Distance(poly, geom) <= 5</code> | | <code>ST_Distance(poly, geom) <= 5</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5</code> | | <code>SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5</code> | ||
|- | |- | ||
第1,408行: | 第1,294行: | ||
<div id="dwithin" class="section"> | <div id="dwithin" class="section"> | ||
− | + | === dwithin === | |
− | === | ||
− | + | 返回从查找几何到几何字段的距离彼此在给定距离内的模型。 请注意,如果目标几何图形在投影系统中,您只能提供 [[../measure#django.contrib.gis.measure|Distance]] 对象。 对于地理几何,您应该使用几何字段的单位(例如 <code>WGS84</code>) 的度数。 | |
− | |||
− | |||
− | |||
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,423行: | 第1,304行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,429行: | 第1,310行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="21%"| | + | !width="21%"| 后端 |
− | !width="79%"| SQL | + | !width="79%"| SQL 等价物 |
|- | |- | ||
− | | | + | | 地理信息系统 |
| <code>ST_DWithin(poly, geom, 5)</code> | | <code>ST_DWithin(poly, geom, 5)</code> | ||
|- | |- | ||
− | | | + | | 甲骨文 |
| <code>SDO_WITHIN_DISTANCE(poly, geom, 5)</code> | | <code>SDO_WITHIN_DISTANCE(poly, geom, 5)</code> | ||
|- | |- | ||
第1,446行: | 第1,327行: | ||
<div id="aggregate-functions" class="section"> | <div id="aggregate-functions" class="section"> | ||
− | === | + | === 聚合函数 === |
− | Django | + | Django 提供了一些特定于 GIS 的聚合函数。 有关如何使用这些聚合函数的详细信息,请参阅 [[../../../../topics/db/aggregation|聚合主题指南]] 。 |
− | |||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 关键字参数 |
− | !width="72%"| | + | !width="72%"| 说明 |
|- | |- | ||
| <code>tolerance</code> | | <code>tolerance</code> | ||
− | | | + | | 此关键字仅适用于 Oracle。 用于<code>SDOAGGRTYPE</code>程序使用的公差值; [https://docs.oracle.com/en/database/oracle/oracle-database/18/spatl/spatial-concepts.html#GUID-CE10AB14-D5EA-43BA-A647-DAC9EEF41EE6 Oracle 文档] 有更多细节。 |
− | |||
− | |||
− | |||
|} | |} | ||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,468行: | 第1,345行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> from django.contrib.gis.db.models import Extent, Union |
− | + | >>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))</syntaxhighlight> | |
</div> | </div> | ||
第1,476行: | 第1,353行: | ||
<div id="collect" class="section"> | <div id="collect" class="section"> | ||
− | ==== | + | ==== Collect ==== |
− | ; ''class'' < | + | ; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">Collect</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">geo_field</span></span>''<span class="sig-paren">)</span> |
: | : | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Collect.html PostGIS],SpatiaLite |
− | |||
− | + | 从几何列返回 <code>GEOMETRYCOLLECTION</code> 或 <code>MULTI</code> 几何对象。 这类似于 [[#django.contrib.gis.db.models.Union|Union]] 聚合的简化版本,但它可以比执行联合快几个数量级,因为它将几何图形汇总到一个集合或多对象中,而不关心分解边界。 | |
− | |||
− | |||
− | |||
− | |||
第1,494行: | 第1,366行: | ||
<div id="extent" class="section"> | <div id="extent" class="section"> | ||
− | ==== | + | ==== Extent ==== |
− | ; ''class'' < | + | ; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">Extent</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">geo_field</span></span>''<span class="sig-paren">)</span> |
: | : | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Extent.html PostGIS]、Oracle、SpatiaLite |
− | |||
− | + | 以四元组形式返回 <code>QuerySet</code> 中所有 <code>geo_field</code> 的范围,包括左下坐标和右上坐标。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,511行: | 第1,381行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly')) |
− | + | >>> print(qs['poly__extent']) | |
− | (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)</ | + | (-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)</syntaxhighlight> |
</div> | </div> | ||
第1,522行: | 第1,392行: | ||
<div id="extent3d" class="section"> | <div id="extent3d" class="section"> | ||
− | ==== | + | ==== Extent3D ==== |
− | ; ''class'' < | + | ; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">Extent3D</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">geo_field</span></span>''<span class="sig-paren">)</span> |
: | : | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_3DExtent.html PostGIS] |
− | + | 以六元组形式返回 <code>QuerySet</code> 中所有 <code>geo_field</code> 的 3D 范围,包括左下坐标和右上坐标(每个坐标都有 x、y 和 z 坐标)。 | |
− | |||
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,539行: | 第1,407行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent3D('poly')) |
− | + | >>> print(qs['poly__extent3d']) | |
− | (-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)</ | + | (-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)</syntaxhighlight> |
</div> | </div> | ||
第1,550行: | 第1,418行: | ||
<div id="makeline" class="section"> | <div id="makeline" class="section"> | ||
− | ==== | + | ==== MakeLine ==== |
− | ; ''class'' < | + | ; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">MakeLine</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">geo_field</span></span>''<span class="sig-paren">)</span> |
: | : | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_MakeLine.html PostGIS],SpatiaLite |
− | |||
− | + | 返回从 <code>QuerySet</code> 中的点场几何构造的 <code>LineString</code>。 目前,对查询集进行排序无效。 | |
− | <code> | ||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,567行: | 第1,433行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(MakeLine('poly')) |
− | + | >>> print(qs['poly__makeline']) | |
− | LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)</ | + | LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)</syntaxhighlight> |
</div> | </div> | ||
第1,578行: | 第1,444行: | ||
<div id="union" class="section"> | <div id="union" class="section"> | ||
− | ==== | + | ==== Union ==== |
− | ; ''class'' < | + | ; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">Union</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">geo_field</span></span>''<span class="sig-paren">)</span> |
: | : | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Union.html PostGIS]、Oracle、SpatiaLite |
− | |||
− | + | 此方法返回一个 [[../geos#django.contrib.gis.geos|GEOSGeometry]] 对象,该对象包含查询集中每个几何体的并集。 请注意,使用 <code>Union</code> 是处理器密集型的,并且可能会在大型查询集上花费大量时间。 | |
− | |||
− | <code>Union</code> | ||
− | |||
<div class="admonition note"> | <div class="admonition note"> | ||
− | + | 笔记 | |
− | + | 如果使用此方法的计算时间太昂贵,请考虑使用 [[#django.contrib.gis.db.models.Collect|Collect]] 代替。 | |
− | |||
</div> | </div> | ||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,606行: | 第1,467行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">>>> u = Zipcode.objects.aggregate(Union(poly)) # This may take a long time. |
− | + | >>> u = Zipcode.objects.filter(poly__within=bbox).aggregate(Union(poly)) # A more sensible approach.</syntaxhighlight> | |
</div> | </div> | ||
</div> | </div> | ||
− | + | 脚注 | |
; <span class="brackets">[[#id4|1]]</span> | ; <span class="brackets">[[#id4|1]]</span> | ||
− | : '' | + | : ''看'' [http://www.opengis.org/docs/99-049.pdf 用于 SQL 的 OpenGIS 简单特征规范], at Ch. 2.1.13.2, p. 2-13 (The Dimensionally Extended Nine-Intersection Model). |
; <span class="brackets">[[#id5|2]]</span> | ; <span class="brackets">[[#id5|2]]</span> | ||
− | : '' | + | : ''看'' [https://docs.oracle.com/en/database/oracle/oracle-database/18/spatl/spatial-operators-reference.html#GUID-97C17C18-F05E-49B4-BE11-E89B972E2A02 SDO_RELATE 文档], from the Oracle Spatial and Graph Developer’s Guide. |
; <span class="brackets">3</span><span class="fn-backref">([[#id2|1]],[[#id3|2]])</span> | ; <span class="brackets">3</span><span class="fn-backref">([[#id2|1]],[[#id3|2]])</span> | ||
− | : | + | : 有关此例程的解释,请阅读 Martin Davis(PostGIS 开发人员)撰写的 [https://lin-ear-th-inking.blogspot.com/2007/06/subtleties-of-ogc-covers-spatial.html Quirks of the “Contains” Spatial Predicate]。 |
第1,629行: | 第1,490行: | ||
</div> | </div> | ||
+ | <div class="clearer"> | ||
− | [[Category:Django 3.0.x | + | |
+ | |||
+ | </div> | ||
+ | |||
+ | [[Category:Django 3.0.x 文档]] |
2021年10月31日 (日) 04:09的最新版本
GIS QuerySet API 参考
空间查找
本节中的空间查找可用于 GeometryField 和 RasterField。
有关介绍,请参阅 空间查找介绍 。 有关哪些查找与特定空间后端兼容的概述,请参阅 空间查找兼容性表 。
使用栅格查找
下面参考中的所有示例都是针对几何字段和输入给出的,但查找可以以相同的方式用于两侧的栅格。 每当查找不支持栅格输入时,输入会在必要时使用 ST_Polygon 函数自动转换为几何图形。 另请参阅 对栅格查找的介绍 。
查找使用的数据库运算符可以分为三类:
- 原生栅格支持
N
:运算符在查找的两侧原生接受栅格,并且栅格输入可以与几何输入混合。 - 双边栅格支持
B
:仅当查找的两侧都接收栅格输入时,运算符才支持栅格。 栅格数据会自动转换为几何图形以进行混合查找。 - 几何转换支持
C
。 查找没有原生栅格支持,所有栅格数据都会自动转换为几何图形。
下面的示例显示了在不同类型的栅格支持中查找的 SQL 等效项。 相同的模式适用于所有空间查找。
案例 | 查找 | SQL 等价物 |
---|---|---|
否,乙 | rast__contains=rst
|
ST_Contains(rast, rst)
|
否,乙 | rast__1__contains=(rst, 2)
|
ST_Contains(rast, 1, rst, 2)
|
乙、丙 | rast__contains=geom
|
ST_Contains(ST_Polygon(rast), geom)
|
乙、丙 | rast__1__contains=geom
|
ST_Contains(ST_Polygon(rast, 1), geom)
|
乙、丙 | poly__contains=rst
|
ST_Contains(poly, ST_Polygon(rst))
|
乙、丙 | poly__contains=(rst, 1)
|
ST_Contains(poly, ST_Polygon(rst, 1))
|
C | rast__crosses=rst
|
ST_Crosses(ST_Polygon(rast), ST_Polygon(rst))
|
C | rast__1__crosses=(rst, 2)
|
ST_Crosses(ST_Polygon(rast, 1), ST_Polygon(rst, 2))
|
C | rast__crosses=geom
|
ST_Crosses(ST_Polygon(rast), geom)
|
C | poly__crosses=rst
|
ST_Crosses(poly, ST_Polygon(rst))
|
仅 PostGIS 后端(在本节中称为 PGRaster)支持使用栅格进行空间查找。
bbcontains
可用性:PostGIS、MariaDB、MySQL、SpatiaLite、PGRaster(原生)
测试几何或栅格字段的边界框是否完全包含查找几何的边界框。
例子:
Zipcode.objects.filter(poly__bbcontains=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | poly ~ geom
|
玛丽亚数据库 | MBRContains(poly, geom)
|
MySQL | MBRContains(poly, geom)
|
SpatiaLite | MbrContains(poly, geom)
|
bboverlaps
可用性:PostGIS、MariaDB、MySQL、SpatiaLite、PGRaster(原生)
测试几何字段的边界框是否与查找几何的边界框重叠。
例子:
Zipcode.objects.filter(poly__bboverlaps=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | poly && geom
|
玛丽亚数据库 | MBROverlaps(poly, geom)
|
MySQL | MBROverlaps(poly, geom)
|
SpatiaLite | MbrOverlaps(poly, geom)
|
contained
可用性:PostGIS、MariaDB、MySQL、SpatiaLite、PGRaster(原生)
测试几何字段的边界框是否完全包含在查找几何的边界框内。
例子:
Zipcode.objects.filter(poly__contained=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | poly @ geom
|
玛丽亚数据库 | MBRWithin(poly, geom)
|
MySQL | MBRWithin(poly, geom)
|
SpatiaLite | MbrWithin(poly, geom)
|
contains
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段在空间上是否包含查找几何。
例子:
Zipcode.objects.filter(poly__contains=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Contains(poly, geom)
|
甲骨文 | SDO_CONTAINS(poly, geom)
|
玛丽亚数据库 | ST_Contains(poly, geom)
|
MySQL | ST_Contains(poly, geom)
|
SpatiaLite | Contains(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBRContains
并且仅对边界框进行操作。
contains_properly
可用性:PostGIS,PGRaster(双边)
如果查找几何与几何字段的内部相交,但不与边界(或外部)相交,则返回 true。
例子:
Zipcode.objects.filter(poly__contains_properly=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_ContainsProperly(poly, geom)
|
coveredby
可用性:PostGIS、Oracle、PGRaster(双边)、SpatiaLite
测试几何字段中是否没有点位于查找几何之外。 3
例子:
Zipcode.objects.filter(poly__coveredby=geom)
2.2 版更改:添加了 SpatiaLite 支持。
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_CoveredBy(poly, geom)
|
甲骨文 | SDO_COVEREDBY(poly, geom)
|
SpatiaLite | CoveredBy(poly, geom)
|
covers
可用性:PostGIS、Oracle、PGRaster(双边)、SpatiaLite
测试查找几何中是否没有点位于几何字段之外。 3
例子:
Zipcode.objects.filter(poly__covers=geom)
2.2 版更改:添加了 SpatiaLite 支持。
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Covers(poly, geom)
|
甲骨文 | SDO_COVERS(poly, geom)
|
SpatiaLite | Covers(poly, geom)
|
crosses
可用性:PostGIS、MariaDB、MySQL、SpatiaLite、PGRaster(转换)
测试几何字段是否与查找几何在空间上交叉。
例子:
Zipcode.objects.filter(poly__crosses=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Crosses(poly, geom)
|
玛丽亚数据库 | ST_Crosses(poly, geom)
|
MySQL | ST_Crosses(poly, geom)
|
SpatiaLite | Crosses(poly, geom)
|
3.0 版更改: 增加了 MySQL 支持。
disjoint
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否与查找几何在空间上不相交。
例子:
Zipcode.objects.filter(poly__disjoint=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Disjoint(poly, geom)
|
甲骨文 | SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)
|
玛丽亚数据库 | ST_Disjoint(poly, geom)
|
MySQL | ST_Disjoint(poly, geom)
|
SpatiaLite | Disjoint(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBRDisjoint
并且仅对边界框进行操作。
equals
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(转换)
测试几何字段在空间上是否等于查找几何。
例子:
Zipcode.objects.filter(poly__equals=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Equals(poly, geom)
|
甲骨文 | SDO_EQUAL(poly, geom)
|
玛丽亚数据库 | ST_Equals(poly, geom)
|
MySQL | ST_Equals(poly, geom)
|
SpatiaLite | Equals(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBREquals
并且仅对边界框进行操作。
exact、same_as
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否“等于”查找几何。 在 Oracle、MySQL 和 SpatiaLite 上,它测试空间相等性,而在 PostGIS 上,它测试边界框的相等性。
例子:
Zipcode.objects.filter(poly=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | poly ~= geom
|
甲骨文 | SDO_EQUAL(poly, geom)
|
玛丽亚数据库 | ST_Equals(poly, geom)
|
MySQL | ST_Equals(poly, geom)
|
SpatiaLite | Equals(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBREquals
并且仅对边界框进行操作。
intersects
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否与查找几何在空间上相交。
例子:
Zipcode.objects.filter(poly__intersects=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Intersects(poly, geom)
|
甲骨文 | SDO_OVERLAPBDYINTERSECT(poly, geom)
|
玛丽亚数据库 | ST_Intersects(poly, geom)
|
MySQL | ST_Intersects(poly, geom)
|
SpatiaLite | Intersects(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBRIntersects
并且仅对边界框进行操作。
isvalid
可用性:MySQL (≥ 5.7.5)、PostGIS、Oracle、SpatiaLite
测试几何图形是否有效。
例子:
Zipcode.objects.filter(poly__isvalid=True)
后端 | SQL 等价物 |
---|---|
MySQL、PostGIS、SpatiaLite | ST_IsValid(poly)
|
甲骨文 | SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE'
|
overlaps
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否与查找几何在空间上重叠。
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Overlaps(poly, geom)
|
甲骨文 | SDO_OVERLAPS(poly, geom)
|
玛丽亚数据库 | ST_Overlaps(poly, geom)
|
MySQL | ST_Overlaps(poly, geom)
|
SpatiaLite | Overlaps(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBROverlaps
并且仅对边界框进行操作。
relate
可用性:PostGIS、Oracle、SpatiaLite、PGRaster(转换)
通过给定模式中给定的值测试几何字段是否与查找几何在空间上相关。 这个查找需要一个元组参数,(geom, pattern)
; pattern
的形式将取决于空间后端:
PostGIS 和 SpatiaLite
在这些空间后端上,相交模式是一个由九个字符组成的字符串,用于定义几何字段与查找几何的内部、边界和外部之间的交集。 交集模式矩阵只能使用以下字符:1
、2
、T
、F
或*
。 这种查找类型允许用户“微调”与 DE-9IM 模型一致的特定几何关系。 1
几何示例:
# A tuple lookup parameter is used to specify the geometry and
# the intersection pattern (the pattern here is for 'contains').
Zipcode.objects.filter(poly__relate=(geom, 'T*T***FF*'))
PostGIS SQL 等效项:
SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')
SpatiaLite SQL 等效项:
SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')
光栅示例:
Zipcode.objects.filter(poly__relate=(rast, 1, 'T*T***FF*'))
Zipcode.objects.filter(rast__2__relate=(rast, 1, 'T*T***FF*'))
PostGIS SQL 等效项:
SELECT ... WHERE ST_Relate(poly, ST_Polygon(rast, 1), 'T*T***FF*')
SELECT ... WHERE ST_Relate(ST_Polygon(rast, 2), ST_Polygon(rast, 1), 'T*T***FF*')
甲骨文
这里的关系模式由以下九个关系串中的至少一个组成:TOUCH
、OVERLAPBDYDISJOINT
、OVERLAPBDYINTERSECT
、EQUAL
、INSIDE
、COVEREDBY
、CONTAINS
、COVERS
、ON
和 ANYINTERACT
。 多个字符串可以用逻辑布尔运算符 OR 组合,例如,'inside+touch'
。 2 关系字符串不区分大小写。
例子:
Zipcode.objects.filter(poly__relate=(geom, 'anyinteract'))
Oracle SQL 等效项:
SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')
touches
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite
测试几何字段是否在空间上接触查找几何。
例子:
Zipcode.objects.filter(poly__touches=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Touches(poly, geom)
|
玛丽亚数据库 | ST_Touches(poly, geom)
|
MySQL | ST_Touches(poly, geom)
|
甲骨文 | SDO_TOUCH(poly, geom)
|
SpatiaLite | Touches(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBRTouches
并且仅对边界框进行操作。
within
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否在空间上查找几何。
例子:
Zipcode.objects.filter(poly__within=geom)
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Within(poly, geom)
|
玛丽亚数据库 | ST_Within(poly, geom)
|
MySQL | ST_Within(poly, geom)
|
甲骨文 | SDO_INSIDE(poly, geom)
|
SpatiaLite | Within(poly, geom)
|
3.0 版本变化: 在旧版本中,MySQL 使用 MBRWithin
并且仅对边界框进行操作。
left
可用性:PostGIS,PGRaster(转换)
测试几何字段的边界框是否严格位于查找几何的边界框的左侧。
例子:
Zipcode.objects.filter(poly__left=geom)
PostGIS 等效项:
SELECT ... WHERE poly << geom
right
可用性:PostGIS,PGRaster(转换)
测试几何字段的边界框是否严格位于查找几何的边界框的右侧。
例子:
Zipcode.objects.filter(poly__right=geom)
PostGIS 等效项:
SELECT ... WHERE poly >> geom
overlaps_left
可用性:PostGIS,PGRaster(双边)
测试几何字段的边界框是否与查找几何的边界框重叠或位于其左侧。
例子:
Zipcode.objects.filter(poly__overlaps_left=geom)
PostGIS 等效项:
SELECT ... WHERE poly &< geom
overlaps_right
可用性:PostGIS,PGRaster(双边)
测试几何字段的边界框是否与查找几何的边界框重叠或位于其右侧。
例子:
Zipcode.objects.filter(poly__overlaps_right=geom)
PostGIS 等效项:
SELECT ... WHERE poly &> geom
overlaps_above
可用性:PostGIS,PGRaster(转换)
测试几何字段的边界框是否与查找几何的边界框重叠或位于其上方。
例子:
Zipcode.objects.filter(poly__overlaps_above=geom)
PostGIS 等效项:
SELECT ... WHERE poly |&> geom
overlaps_below
可用性:PostGIS,PGRaster(转换)
测试几何字段的边界框是否重叠或低于查找几何的边界框。
例子:
Zipcode.objects.filter(poly__overlaps_below=geom)
PostGIS 等效项:
SELECT ... WHERE poly &<| geom
strictly_above
可用性:PostGIS,PGRaster(转换)
测试几何字段的边界框是否严格高于查找几何的边界框。
例子:
Zipcode.objects.filter(poly__strictly_above=geom)
PostGIS 等效项:
SELECT ... WHERE poly |>> geom
strictly_below
可用性:PostGIS,PGRaster(转换)
测试几何字段的边界框是否严格低于查找几何的边界框。
例子:
Zipcode.objects.filter(poly__strictly_below=geom)
PostGIS 等效项:
SELECT ... WHERE poly <<| geom
距离查找
可用性:PostGIS、Oracle、MariaDB、MySQL、SpatiaLite、PGRaster(原生)
有关执行距离查询的概述,请参阅 距离查询介绍 。
距离查找采用以下形式:
<field>__<distance lookup>=(<geometry/raster>, <distance value>[, 'spheroid'])
<field>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])
<field>__<band_index>__<distance lookup>=(<raster>, <band_index>, <distance value>[, 'spheroid'])
传递给距离查找的值是一个元组; 前两个值是强制性的,并且是计算到的距离的几何图形和一个距离值(以字段为单位的数字、Distance 对象或 查询表达式 )。 要将波段索引传递给查找,请使用 3 元组,其中第二个条目是波段索引。
在除 :lookup:`dwithin` 之外的每个距离查找中,可以包含一个可选元素 'spheroid'
,以便在具有大地坐标系的字段上使用更准确的球体距离计算函数。
在 PostgreSQL 上,'spheroid'
选项使用 ST_DistanceSpheroid 而不是 ST_DistanceSphere。 更简单的 ST_Distance 函数用于投影坐标系。 栅格被转换为几何图形以进行基于球体的查找。
distance_gt
返回从查找几何到几何字段的距离大于给定距离值的模型。
例子:
Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Distance/ST_Distance_Sphere(poly, geom) > 5
|
玛丽亚数据库 | ST_Distance(poly, geom) > 5
|
MySQL | ST_Distance(poly, geom) > 5
|
甲骨文 | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5
|
SpatiaLite | Distance(poly, geom) > 5
|
distance_gte
返回从查找几何到几何字段的距离大于或等于给定距离值的模型。
例子:
Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Distance/ST_Distance_Sphere(poly, geom) >= 5
|
玛丽亚数据库 | ST_Distance(poly, geom) >= 5
|
MySQL | ST_Distance(poly, geom) >= 5
|
甲骨文 | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5
|
SpatiaLite | Distance(poly, geom) >= 5
|
distance_lt
返回从查找几何到几何字段的距离小于给定距离值的模型。
例子:
Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Distance/ST_Distance_Sphere(poly, geom) < 5
|
玛丽亚数据库 | ST_Distance(poly, geom) < 5
|
MySQL | ST_Distance(poly, geom) < 5
|
甲骨文 | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5
|
SpatiaLite | Distance(poly, geom) < 5
|
distance_lte
返回从查找几何到几何字段的距离小于或等于给定距离值的模型。
例子:
Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_Distance/ST_Distance_Sphere(poly, geom) <= 5
|
玛丽亚数据库 | ST_Distance(poly, geom) <= 5
|
MySQL | ST_Distance(poly, geom) <= 5
|
甲骨文 | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5
|
SpatiaLite | Distance(poly, geom) <= 5
|
dwithin
返回从查找几何到几何字段的距离彼此在给定距离内的模型。 请注意,如果目标几何图形在投影系统中,您只能提供 Distance 对象。 对于地理几何,您应该使用几何字段的单位(例如 WGS84
) 的度数。
例子:
Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))
后端 | SQL 等价物 |
---|---|
地理信息系统 | ST_DWithin(poly, geom, 5)
|
甲骨文 | SDO_WITHIN_DISTANCE(poly, geom, 5)
|
SpatiaLite | PtDistWithin(poly, geom, 5)
|
聚合函数
Django 提供了一些特定于 GIS 的聚合函数。 有关如何使用这些聚合函数的详细信息,请参阅 聚合主题指南 。
关键字参数 | 说明 |
---|---|
tolerance
|
此关键字仅适用于 Oracle。 用于SDOAGGRTYPE 程序使用的公差值; Oracle 文档 有更多细节。
|
例子:
>>> from django.contrib.gis.db.models import Extent, Union
>>> WorldBorder.objects.aggregate(Extent('mpoly'), Union('mpoly'))
Collect
- class Collect(geo_field)
可用性:PostGIS,SpatiaLite
从几何列返回 GEOMETRYCOLLECTION
或 MULTI
几何对象。 这类似于 Union 聚合的简化版本,但它可以比执行联合快几个数量级,因为它将几何图形汇总到一个集合或多对象中,而不关心分解边界。
Extent
- class Extent(geo_field)
可用性:PostGIS、Oracle、SpatiaLite
以四元组形式返回 QuerySet
中所有 geo_field
的范围,包括左下坐标和右上坐标。
例子:
>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent('poly'))
>>> print(qs['poly__extent'])
(-96.8016128540039, 29.7633724212646, -95.3631439208984, 32.782058715820)
Extent3D
- class Extent3D(geo_field)
可用性:PostGIS
以六元组形式返回 QuerySet
中所有 geo_field
的 3D 范围,包括左下坐标和右上坐标(每个坐标都有 x、y 和 z 坐标)。
例子:
>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(Extent3D('poly'))
>>> print(qs['poly__extent3d'])
(-96.8016128540039, 29.7633724212646, 0, -95.3631439208984, 32.782058715820, 0)
MakeLine
- class MakeLine(geo_field)
可用性:PostGIS,SpatiaLite
返回从 QuerySet
中的点场几何构造的 LineString
。 目前,对查询集进行排序无效。
例子:
>>> qs = City.objects.filter(name__in=('Houston', 'Dallas')).aggregate(MakeLine('poly'))
>>> print(qs['poly__makeline'])
LINESTRING (-95.3631510000000020 29.7633739999999989, -96.8016109999999941 32.7820570000000018)
Union
- class Union(geo_field)
可用性:PostGIS、Oracle、SpatiaLite
此方法返回一个 GEOSGeometry 对象,该对象包含查询集中每个几何体的并集。 请注意,使用 Union
是处理器密集型的,并且可能会在大型查询集上花费大量时间。
例子:
>>> u = Zipcode.objects.aggregate(Union(poly)) # This may take a long time.
>>> u = Zipcode.objects.filter(poly__within=bbox).aggregate(Union(poly)) # A more sensible approach.
脚注
- 1
- 看 用于 SQL 的 OpenGIS 简单特征规范, at Ch. 2.1.13.2, p. 2-13 (The Dimensionally Extended Nine-Intersection Model).
- 2
- 看 SDO_RELATE 文档, from the Oracle Spatial and Graph Developer’s Guide.
- 3(1,2)
- 有关此例程的解释,请阅读 Martin Davis(PostGIS 开发人员)撰写的 Quirks of the “Contains” Spatial Predicate。