“Django/docs/2.2.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 等价于: |
|- | |- | ||
| N, B | | N, B | ||
第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]、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 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第125行: | 第114行: | ||
<div id="bboverlaps" class="section"> | <div id="bboverlaps" class="section"> | ||
− | + | === bboverlaps === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/geometry_overlaps.html PostGIS]、MySQL、SpatiaLite、PGRaster(原生) |
− | |||
− | + | 测试几何字段的边界框是否与查找几何的边界框重叠。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第140行: | 第126行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__bboverlaps=geom)</syntaxhighlight> |
</div> | </div> | ||
第146行: | 第132行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="27%"| | + | !width="27%"| 后端 |
− | !width="73%"| SQL | + | !width="73%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第163行: | 第149行: | ||
<div id="contained" class="section"> | <div id="contained" class="section"> | ||
− | + | === contained === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Geometry_Contained.html PostGIS]、MySQL、SpatiaLite、PGRaster(原生) |
− | |||
− | + | 测试几何字段的边界框是否完全包含在查找几何的边界框内。 | |
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第178行: | 第161行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__contained=geom)</syntaxhighlight> |
</div> | </div> | ||
第184行: | 第167行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第201行: | 第184行: | ||
<div id="contains" class="section"> | <div id="contains" class="section"> | ||
− | + | === contains === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Contains.html PostGIS]、Oracle、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段在空间上是否包含查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第215行: | 第196行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__contains=geom)</syntaxhighlight> |
</div> | </div> | ||
第221行: | 第202行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="26%"| | + | !width="26%"| 后端 |
− | !width="74%"| SQL | + | !width="74%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第241行: | 第222行: | ||
<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"> | ||
第256行: | 第234行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__contains_properly=geom)</syntaxhighlight> |
</div> | </div> | ||
第262行: | 第240行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="22%"| | + | !width="22%"| 后端 |
− | !width="78%"| SQL | + | !width="78%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第273行: | 第251行: | ||
<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"> | ||
第288行: | 第263行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__coveredby=geom)</syntaxhighlight> |
</div> | </div> | ||
第295行: | 第270行: | ||
<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 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第317行: | 第292行: | ||
<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"> | ||
第332行: | 第304行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__covers=geom)</syntaxhighlight> |
</div> | </div> | ||
第339行: | 第311行: | ||
<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 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第361行: | 第333行: | ||
<div id="crosses" class="section"> | <div id="crosses" class="section"> | ||
− | + | === crosses === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Crosses.html PostGIS]、SpatiaLite、PGRaster(转换) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上交叉。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第375行: | 第345行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__crosses=geom)</syntaxhighlight> |
</div> | </div> | ||
第381行: | 第351行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第395行: | 第365行: | ||
<div id="disjoint" class="section"> | <div id="disjoint" class="section"> | ||
− | + | === disjoint === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Disjoint.html PostGIS]、Oracle、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上不相交。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第409行: | 第377行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__disjoint=geom)</syntaxhighlight> |
</div> | </div> | ||
第415行: | 第383行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第435行: | 第403行: | ||
<div id="equals" class="section"> | <div id="equals" class="section"> | ||
− | + | === equals === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Equals.html PostGIS]、Oracle、MySQL、SpatiaLite、PGRaster(转换) |
− | |||
− | + | 测试几何字段在空间上是否等于查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第449行: | 第415行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__equals=geom)</syntaxhighlight> |
</div> | </div> | ||
第455行: | 第421行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第471行: | 第437行: | ||
|} | |} | ||
− | |||
</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、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否“等于”查找几何。 在 Oracle 和 SpatiaLite 上它测试空间相等性,而在 MySQL 和 PostGIS 上它测试边界框的相等性。 | |
− | SpatiaLite | ||
− | |||
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第492行: | 第453行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly=geom)</syntaxhighlight> |
</div> | </div> | ||
第498行: | 第459行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第518行: | 第479行: | ||
<div id="intersects" class="section"> | <div id="intersects" class="section"> | ||
− | + | === intersects === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Intersects.html PostGIS]、Oracle、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上相交。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第532行: | 第491行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__intersects=geom)</syntaxhighlight> |
</div> | </div> | ||
第538行: | 第497行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第558行: | 第517行: | ||
<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"> | ||
第571行: | 第529行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__isvalid=True)</syntaxhighlight> |
</div> | </div> | ||
第577行: | 第535行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="71%"| SQL | + | !width="71%"| SQL 等价于: |
|- | |- | ||
| MySQL, PostGIS, SpatiaLite | | MySQL, PostGIS, SpatiaLite | ||
第591行: | 第549行: | ||
<div id="overlaps" class="section"> | <div id="overlaps" class="section"> | ||
− | + | === overlaps === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Overlaps.html PostGIS]、Oracle、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否与查找几何在空间上重叠。 | |
{| | {| | ||
− | !width="26%"| | + | !width="26%"| 后端 |
− | !width="74%"| SQL | + | !width="74%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第620行: | 第576行: | ||
<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"> | ||
第647行: | 第594行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第660行: | 第607行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE ST_Relate(poly, geom, 'T*T***FF*')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | SpatiaLite SQL | + | SpatiaLite SQL 等效项: |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第671行: | 第618行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE Relate(poly, geom, 'T*T***FF*')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 光栅示例: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第682行: | 第629行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第694行: | 第641行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">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> | ||
第706行: | 第653行: | ||
==== Oracle ==== | ==== Oracle ==== | ||
− | + | 这里的关系模式由以下九个关系串中的至少一个组成:<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"> | ||
第719行: | 第661行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第730行: | 第672行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE SDO_RELATE(poly, geom, 'anyinteract')</syntaxhighlight> |
</div> | </div> | ||
第741行: | 第683行: | ||
<div id="touches" class="section"> | <div id="touches" class="section"> | ||
− | + | === touches === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Touches.html PostGIS]、Oracle、MySQL、SpatiaLite |
− | |||
− | + | 测试几何字段是否在空间上接触查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第755行: | 第695行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__touches=geom)</syntaxhighlight> |
</div> | </div> | ||
第761行: | 第701行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第781行: | 第721行: | ||
<div id="within" class="section"> | <div id="within" class="section"> | ||
− | + | === within === | |
− | === | ||
− | '' | + | ''可用性'':[https://postgis.net/docs/ST_Within.html PostGIS]、Oracle、MySQL、SpatiaLite、PGRaster(双边) |
− | |||
− | + | 测试几何字段是否在空间上查找几何。 | |
− | + | 例子: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第795行: | 第733行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__within=geom)</syntaxhighlight> |
</div> | </div> | ||
第801行: | 第739行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="28%"| | + | !width="28%"| 后端 |
− | !width="72%"| SQL | + | !width="72%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第821行: | 第759行: | ||
<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"> | ||
第836行: | 第771行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第847行: | 第782行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly << geom</syntaxhighlight> |
</div> | </div> | ||
第856行: | 第791行: | ||
<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"> | ||
第871行: | 第803行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第882行: | 第814行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly >> geom</syntaxhighlight> |
</div> | </div> | ||
第891行: | 第823行: | ||
<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"> | ||
第906行: | 第835行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第917行: | 第846行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly &< geom</syntaxhighlight> |
</div> | </div> | ||
第926行: | 第855行: | ||
<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"> | ||
第941行: | 第867行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第952行: | 第878行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly &> geom</syntaxhighlight> |
</div> | </div> | ||
第961行: | 第887行: | ||
<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"> | ||
第976行: | 第899行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第987行: | 第910行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly |&> geom</syntaxhighlight> |
</div> | </div> | ||
第996行: | 第919行: | ||
<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,011行: | 第931行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,022行: | 第942行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly &<| geom</syntaxhighlight> |
</div> | </div> | ||
第1,031行: | 第951行: | ||
<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,046行: | 第963行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,057行: | 第974行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly |>> geom</syntaxhighlight> |
</div> | </div> | ||
第1,066行: | 第983行: | ||
<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,081行: | 第995行: | ||
<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-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,092行: | 第1,006行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">SELECT ... WHERE poly <<| geom</syntaxhighlight> |
</div> | </div> | ||
第1,104行: | 第1,018行: | ||
<span id="id6"></span> | <span id="id6"></span> | ||
− | == | + | == 距离查找 == |
− | '' | + | ''可用性'':PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(原生) |
− | + | 有关执行距离查询的概述,请参阅 [[../db-api#distance-queries|距离查询介绍]] 。 | |
− | |||
− | + | 距离查找采用以下形式: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第1,117行: | 第1,030行: | ||
<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|距离]]对象,或查询表达式 )。 要将波段索引传递给查找,请使用 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,155行: | 第1,055行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_gt=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,161行: | 第1,061行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第1,181行: | 第1,081行: | ||
<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,193行: | 第1,091行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_gte=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,199行: | 第1,097行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="16%"| | + | !width="16%"| 后端 |
− | !width="84%"| SQL | + | !width="84%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第1,219行: | 第1,117行: | ||
<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,231行: | 第1,127行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_lt=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,237行: | 第1,133行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="17%"| | + | !width="17%"| 后端 |
− | !width="83%"| SQL | + | !width="83%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第1,257行: | 第1,153行: | ||
<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,269行: | 第1,163行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__distance_lte=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,275行: | 第1,169行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="16%"| | + | !width="16%"| 后端 |
− | !width="84%"| SQL | + | !width="84%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第1,295行: | 第1,189行: | ||
<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,310行: | 第1,199行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <syntaxhighlight lang="python">Zipcode.objects.filter(poly__dwithin=(geom, D(m=5)))</syntaxhighlight> |
</div> | </div> | ||
第1,316行: | 第1,205行: | ||
</div> | </div> | ||
{| | {| | ||
− | !width="21%"| | + | !width="21%"| 后端 |
− | !width="79%"| SQL | + | !width="79%"| SQL 等价于: |
|- | |- | ||
| PostGIS | | PostGIS | ||
第1,333行: | 第1,222行: | ||
<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,355行: | 第1,240行: | ||
<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,363行: | 第1,248行: | ||
<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,381行: | 第1,261行: | ||
<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,398行: | 第1,276行: | ||
<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,409行: | 第1,287行: | ||
<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,426行: | 第1,302行: | ||
<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,437行: | 第1,313行: | ||
<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,454行: | 第1,328行: | ||
<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,465行: | 第1,339行: | ||
<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,493行: | 第1,362行: | ||
<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,516行: | 第1,385行: | ||
</div> | </div> | ||
+ | <div class="clearer"> | ||
− | [[Category:Django 2.2.x | + | |
+ | |||
+ | </div> | ||
+ | |||
+ | [[Category:Django 2.2.x 文档]] |
2021年10月31日 (日) 04:05的最新版本
GIS QuerySet API 参考
空间查找
本节中的空间查找可用于 GeometryField 和 RasterField。
有关介绍,请参阅 空间查找介绍 。 有关哪些查找与特定空间后端兼容的概述,请参阅 空间查找兼容性表 。
使用栅格查找
下面参考中的所有示例都是针对几何字段和输入给出的,但查找可以以相同的方式用于两侧的栅格。 每当查找不支持栅格输入时,输入会在必要时使用 ST_Polygon 函数自动转换为几何图形。 另请参阅 对栅格查找的介绍 。
查找使用的数据库运算符可以分为三类:
- 原生栅格支持
N
:运算符在查找的两侧原生接受栅格,并且栅格输入可以与几何输入混合。 - 双边栅格支持
B
:仅当查找的两侧都接收栅格输入时,运算符才支持栅格。 栅格数据会自动转换为几何图形以进行混合查找。 - 几何转换支持
C
。 查找没有原生栅格支持,所有栅格数据都会自动转换为几何图形。
下面的示例显示了在不同类型的栅格支持中查找的 SQL 等效项。 相同的模式适用于所有空间查找。
案件 | 查找 | SQL 等价于: |
---|---|---|
N, B | rast__contains=rst
|
ST_Contains(rast, rst)
|
N, B | rast__1__contains=(rst, 2)
|
ST_Contains(rast, 1, rst, 2)
|
B, C | rast__contains=geom
|
ST_Contains(ST_Polygon(rast), geom)
|
B, C | rast__1__contains=geom
|
ST_Contains(ST_Polygon(rast, 1), geom)
|
B, C | poly__contains=rst
|
ST_Contains(poly, ST_Polygon(rst))
|
B, C | poly__contains=(rst, 1)
|
ST_Contains(poly, ST_Polygon(rst, 1))
|
C | rast__crosses=rst
|
ST_Crosses(ST_Polygon(rast), ST_Polygon(rst))
|
C | rast__1__crosses=(rst, 2)
|
ST_Crosses(ST_Polygon(rast, 1), ST_Polygon(rst, 2))
|
C | rast__crosses=geom
|
ST_Crosses(ST_Polygon(rast), geom)
|
C | poly__crosses=rst
|
ST_Crosses(poly, ST_Polygon(rst))
|
仅 PostGIS 后端(在本节中称为 PGRaster)支持使用栅格进行空间查找。
bbcontains
可用性:PostGIS、MySQL、SpatiaLite、PGRaster(原生)
测试几何或栅格字段的边界框是否完全包含查找几何的边界框。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | poly ~ geom
|
MySQL | MBRContains(poly, geom)
|
SpatiaLite | MbrContains(poly, geom)
|
bboverlaps
可用性:PostGIS、MySQL、SpatiaLite、PGRaster(原生)
测试几何字段的边界框是否与查找几何的边界框重叠。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | poly && geom
|
MySQL | MBROverlaps(poly, geom)
|
SpatiaLite | MbrOverlaps(poly, geom)
|
contained
可用性:PostGIS、MySQL、SpatiaLite、PGRaster(原生)
测试几何字段的边界框是否完全包含在查找几何的边界框内。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | poly @ geom
|
MySQL | MBRWithin(poly, geom)
|
SpatiaLite | MbrWithin(poly, geom)
|
contains
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段在空间上是否包含查找几何。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Contains(poly, geom)
|
Oracle | SDO_CONTAINS(poly, geom)
|
MySQL | MBRContains(poly, geom)
|
SpatiaLite | Contains(poly, geom)
|
contains_properly
可用性:PostGIS,PGRaster(双边)
如果查找几何与几何字段的内部相交,但不与边界(或外部)相交,则返回 true。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_ContainsProperly(poly, geom)
|
coveredby
可用性:PostGIS、Oracle、PGRaster(双边)、SpatiaLite
测试几何字段中是否没有点位于查找几何之外。 3
例子:
2.2 版更改:添加了 SpatiaLite 支持。
后端 | SQL 等价于: |
---|---|
PostGIS | ST_CoveredBy(poly, geom)
|
Oracle | SDO_COVEREDBY(poly, geom)
|
SpatiaLite | CoveredBy(poly, geom)
|
covers
可用性:PostGIS、Oracle、PGRaster(双边)、SpatiaLite
测试查找几何中是否没有点位于几何字段之外。 3
例子:
2.2 版更改:添加了 SpatiaLite 支持。
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Covers(poly, geom)
|
Oracle | SDO_COVERS(poly, geom)
|
SpatiaLite | Covers(poly, geom)
|
crosses
可用性:PostGIS、SpatiaLite、PGRaster(转换)
测试几何字段是否与查找几何在空间上交叉。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Crosses(poly, geom)
|
SpatiaLite | Crosses(poly, geom)
|
disjoint
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否与查找几何在空间上不相交。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Disjoint(poly, geom)
|
Oracle | SDO_GEOM.RELATE(poly, 'DISJOINT', geom, 0.05)
|
MySQL | MBRDisjoint(poly, geom)
|
SpatiaLite | Disjoint(poly, geom)
|
equals
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(转换)
测试几何字段在空间上是否等于查找几何。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Equals(poly, geom)
|
Oracle | SDO_EQUAL(poly, geom)
|
MySQL | MBREquals(poly, geom)
|
SpatiaLite | Equals(poly, geom)
|
exact、same_as
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否“等于”查找几何。 在 Oracle 和 SpatiaLite 上它测试空间相等性,而在 MySQL 和 PostGIS 上它测试边界框的相等性。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | poly ~= geom
|
Oracle | SDO_EQUAL(poly, geom)
|
MySQL | MBREquals(poly, geom)
|
SpatiaLite | Equals(poly, geom)
|
intersects
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否与查找几何在空间上相交。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Intersects(poly, geom)
|
Oracle | SDO_OVERLAPBDYINTERSECT(poly, geom)
|
MySQL | MBRIntersects(poly, geom)
|
SpatiaLite | Intersects(poly, geom)
|
isvalid
可用性:MySQL (≥ 5.7.5)、PostGIS、Oracle、SpatiaLite
测试几何图形是否有效。
例子:
后端 | SQL 等价于: |
---|---|
MySQL, PostGIS, SpatiaLite | ST_IsValid(poly)
|
Oracle | SDO_GEOM.VALIDATE_GEOMETRY_WITH_CONTEXT(poly, 0.05) = 'TRUE'
|
overlaps
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否与查找几何在空间上重叠。
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Overlaps(poly, geom)
|
Oracle | SDO_OVERLAPS(poly, geom)
|
MySQL | MBROverlaps(poly, geom)
|
SpatiaLite | Overlaps(poly, geom)
|
relate
可用性:PostGIS、Oracle、SpatiaLite、PGRaster(转换)
通过给定模式中给定的值测试几何字段是否与查找几何在空间上相关。 这个查找需要一个元组参数,(geom, pattern)
; pattern
的形式将取决于空间后端:
PostGIS 和 SpatiaLite
在这些空间后端上,相交模式是一个由九个字符组成的字符串,用于定义几何字段与查找几何的内部、边界和外部之间的交集。 交集模式矩阵只能使用以下字符:1
、2
、T
、F
或*
。 这种查找类型允许用户“微调”与 DE-9IM 模型一致的特定几何关系。 1
几何示例:
PostGIS SQL 等效项:
SpatiaLite SQL 等效项:
光栅示例:
PostGIS SQL 等效项:
Oracle
这里的关系模式由以下九个关系串中的至少一个组成:TOUCH
、OVERLAPBDYDISJOINT
、OVERLAPBDYINTERSECT
、EQUAL
、INSIDE
、COVEREDBY
、CONTAINS
、COVERS
、ON
和 ANYINTERACT
。 多个字符串可以用逻辑布尔运算符 OR 组合,例如,'inside+touch'
。 2 关系字符串不区分大小写。
例子:
Oracle SQL 等效项:
touches
可用性:PostGIS、Oracle、MySQL、SpatiaLite
测试几何字段是否在空间上接触查找几何。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Touches(poly, geom)
|
MySQL | MBRTouches(poly, geom)
|
Oracle | SDO_TOUCH(poly, geom)
|
SpatiaLite | Touches(poly, geom)
|
within
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(双边)
测试几何字段是否在空间上查找几何。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Within(poly, geom)
|
MySQL | MBRWithin(poly, geom)
|
Oracle | SDO_INSIDE(poly, geom)
|
SpatiaLite | Within(poly, geom)
|
距离查找
可用性:PostGIS、Oracle、MySQL、SpatiaLite、PGRaster(原生)
有关执行距离查询的概述,请参阅 距离查询介绍 。
距离查找采用以下形式:
传递给距离查找的值是一个元组; 前两个值是强制性的,是计算距离的几何图形和一个距离值(以字段为单位的数字,一个距离对象,或查询表达式 )。 要将波段索引传递给查找,请使用 3 元组,其中第二个条目是波段索引。
在除 :lookup:`dwithin` 之外的每个距离查找中,可以包含一个可选元素 'spheroid'
,以便在具有大地坐标系的字段上使用更准确的球体距离计算函数。
在 PostgreSQL 上,'spheroid'
选项使用 ST_DistanceSpheroid 而不是 ST_DistanceSphere。 更简单的 ST_Distance 函数用于投影坐标系。 栅格被转换为几何图形以进行基于球体的查找。
distance_gt
返回从查找几何到几何字段的距离大于给定距离值的模型。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Distance/ST_Distance_Sphere(poly, geom) > 5
|
MySQL | ST_Distance(poly, geom) > 5
|
Oracle | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) > 5
|
SpatiaLite | Distance(poly, geom) > 5
|
distance_gte
返回从查找几何到几何字段的距离大于或等于给定距离值的模型。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Distance/ST_Distance_Sphere(poly, geom) >= 5
|
MySQL | ST_Distance(poly, geom) >= 5
|
Oracle | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) >= 5
|
SpatiaLite | Distance(poly, geom) >= 5
|
distance_lt
返回从查找几何到几何字段的距离小于给定距离值的模型。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Distance/ST_Distance_Sphere(poly, geom) < 5
|
MySQL | ST_Distance(poly, geom) < 5
|
Oracle | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) < 5
|
SpatiaLite | Distance(poly, geom) < 5
|
distance_lte
返回从查找几何到几何字段的距离小于或等于给定距离值的模型。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_Distance/ST_Distance_Sphere(poly, geom) <= 5
|
MySQL | ST_Distance(poly, geom) <= 5
|
Oracle | SDO_GEOM.SDO_DISTANCE(poly, geom, 0.05) <= 5
|
SpatiaLite | Distance(poly, geom) <= 5
|
dwithin
返回从查找几何到几何字段的距离彼此在给定距离内的模型。 请注意,如果目标几何图形在投影系统中,您只能提供 Distance 对象。 对于地理几何,您应该使用几何字段的单位(例如 WGS84
) 的度数。
例子:
后端 | SQL 等价于: |
---|---|
PostGIS | ST_DWithin(poly, geom, 5)
|
Oracle | SDO_WITHIN_DISTANCE(poly, geom, 5)
|
SpatiaLite | PtDistWithin(poly, geom, 5)
|
聚合函数
Django 提供了一些特定于 GIS 的聚合函数。 有关如何使用这些聚合函数的详细信息,请参阅 聚合主题指南 。
关键字参数 | 描述 |
---|---|
tolerance
|
此关键字仅适用于 Oracle。 用于SDOAGGRTYPE 程序使用的公差值; Oracle 文档 有更多细节。
|
例子:
Collect
- class Collect(geo_field)
可用性:PostGIS,SpatiaLite
从几何列返回 GEOMETRYCOLLECTION
或 MULTI
几何对象。 这类似于 Union 聚合的简化版本,但它可以比执行联合快几个数量级,因为它只是将几何图形汇总到一个集合或多对象中,而不关心分解边界。
Extent
- class Extent(geo_field)
可用性:PostGIS、Oracle、SpatiaLite
以四元组形式返回 QuerySet
中所有 geo_field
的范围,包括左下坐标和右上坐标。
例子:
Extent3D
- class Extent3D(geo_field)
可用性:PostGIS
以六元组形式返回 QuerySet
中所有 geo_field
的 3D 范围,包括左下坐标和右上坐标(每个坐标都有 x、y 和 z 坐标)。
例子:
MakeLine
- class MakeLine(geo_field)
可用性:PostGIS,SpatiaLite
返回从 QuerySet
中的点场几何构造的 LineString
。 目前,对查询集进行排序无效。
例子:
Union
- class Union(geo_field)
可用性:PostGIS、Oracle、SpatiaLite
此方法返回一个 GEOSGeometry 对象,该对象包含查询集中每个几何体的并集。 请注意,使用 Union
是处理器密集型的,并且可能会在大型查询集上花费大量时间。
例子:
脚注
- 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。