“Django/docs/2.2.x/ref/contrib/gis/model-api”的版本间差异

来自菜鸟教程
Django/docs/2.2.x/ref/contrib/gis/model-api
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:GeoDjango 模型 API — Django 文档}}
 
<div id="module-django.contrib.gis.db.models" class="section">
 
<div id="module-django.contrib.gis.db.models" class="section">
  
 
<span id="geodjango-model-api"></span>
 
<span id="geodjango-model-api"></span>
= GeoDjango Model API =
+
= GeoDjango 模型 API =
  
This document explores the details of the GeoDjango Model API. Throughout this
+
本文档探讨了 GeoDjango 模型 API 的详细信息。 在本节中,我们将使用 [https://en.wikipedia.org/wiki/ZIP_code 邮政编码] [https://en.wikipedia.org/wiki/Digital_elevation_model 数字高程模型] 的以下地理模型作为示例:
section, we'll be using the following geographic model of a [https://en.wikipedia.org/wiki/ZIP_code ZIP code] and
 
of a [https://en.wikipedia.org/wiki/Digital_elevation_model Digital Elevation Model] as our examples:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第12行: 第11行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.contrib.gis.db import models
+
<syntaxhighlight lang="python">from django.contrib.gis.db import models
  
 
class Zipcode(models.Model):
 
class Zipcode(models.Model):
第20行: 第19行:
 
class Elevation(models.Model):
 
class Elevation(models.Model):
 
     name = models.CharField(max_length=100)
 
     name = models.CharField(max_length=100)
     rast = models.RasterField()</pre>
+
     rast = models.RasterField()</syntaxhighlight>
  
 
</div>
 
</div>
第27行: 第26行:
 
<div id="spatial-field-types" class="section">
 
<div id="spatial-field-types" class="section">
  
== Spatial Field Types ==
+
== 空间字段类型 ==
  
Spatial fields consist of a series of geometry field types and one raster field
+
空间字段由一系列几何字段类型和一个栅格字段类型组成。 每个几何字段类型都对应于 OpenGIS 简单要素规范 [[#fnogc|1]]。 栅格数据没有这样的标准。
type. Each of the geometry field types correspond to the OpenGIS Simple
 
Features specification [[#fnogc|1]]. There is no such standard for raster data.
 
  
 
<div id="geometryfield" class="section">
 
<div id="geometryfield" class="section">
  
=== <code>GeometryField</code> ===
+
=== GeometryField ===
  
; ''class'' <code>GeometryField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">GeometryField</span></span>
 
:  
 
:  
  
The base class for geometry fields.
+
几何字段的基类。
  
  
第46行: 第43行:
 
<div id="pointfield" class="section">
 
<div id="pointfield" class="section">
  
=== <code>PointField</code> ===
+
=== PointField ===
  
; ''class'' <code>PointField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">PointField</span></span>
 
:  
 
:  
  
Stores a [[../geos#django.contrib.gis.geos|<code>Point</code>]].
+
存储 [[../geos#django.contrib.gis.geos|]]
  
  
第57行: 第54行:
 
<div id="linestringfield" class="section">
 
<div id="linestringfield" class="section">
  
=== <code>LineStringField</code> ===
+
=== LineStringField ===
  
; ''class'' <code>LineStringField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">LineStringField</span></span>
 
:  
 
:  
  
Stores a [[../geos#django.contrib.gis.geos|<code>LineString</code>]].
+
存储 [[../geos#django.contrib.gis.geos|LineString]]
  
  
第68行: 第65行:
 
<div id="polygonfield" class="section">
 
<div id="polygonfield" class="section">
  
=== <code>PolygonField</code> ===
+
=== PolygonField ===
  
; ''class'' <code>PolygonField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">PolygonField</span></span>
 
:  
 
:  
  
Stores a [[../geos#django.contrib.gis.geos|<code>Polygon</code>]].
+
存储 [[../geos#django.contrib.gis.geos|多边形]]
  
  
第79行: 第76行:
 
<div id="multipointfield" class="section">
 
<div id="multipointfield" class="section">
  
=== <code>MultiPointField</code> ===
+
=== MultiPointField ===
  
; ''class'' <code>MultiPointField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">MultiPointField</span></span>
 
:  
 
:  
  
Stores a [[../geos#django.contrib.gis.geos|<code>MultiPoint</code>]].
+
存储 [[../geos#django.contrib.gis.geos|MultiPoint]]
  
  
第90行: 第87行:
 
<div id="multilinestringfield" class="section">
 
<div id="multilinestringfield" class="section">
  
=== <code>MultiLineStringField</code> ===
+
=== MultiLineStringField ===
  
; ''class'' <code>MultiLineStringField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">MultiLineStringField</span></span>
 
:  
 
:  
  
Stores a [[../geos#django.contrib.gis.geos|<code>MultiLineString</code>]].
+
存储 [[../geos#django.contrib.gis.geos|MultiLineString]]
  
  
第101行: 第98行:
 
<div id="multipolygonfield" class="section">
 
<div id="multipolygonfield" class="section">
  
=== <code>MultiPolygonField</code> ===
+
=== MultiPolygonField ===
  
; ''class'' <code>MultiPolygonField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">MultiPolygonField</span></span>
 
:  
 
:  
  
Stores a [[../geos#django.contrib.gis.geos|<code>MultiPolygon</code>]].
+
存储 [[../geos#django.contrib.gis.geos|MultiPolygon]]
  
  
第112行: 第109行:
 
<div id="geometrycollectionfield" class="section">
 
<div id="geometrycollectionfield" class="section">
  
=== <code>GeometryCollectionField</code> ===
+
=== GeometryCollectionField ===
  
; ''class'' <code>GeometryCollectionField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">GeometryCollectionField</span></span>
 
:  
 
:  
  
Stores a [[../geos#django.contrib.gis.geos|<code>GeometryCollection</code>]].
+
存储 [[../geos#django.contrib.gis.geos|GeometryCollection]]
  
  
第123行: 第120行:
 
<div id="rasterfield" class="section">
 
<div id="rasterfield" class="section">
  
=== <code>RasterField</code> ===
+
=== RasterField ===
  
; ''class'' <code>RasterField</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">RasterField</span></span>
 
:  
 
:  
  
Stores a [[../gdal#django.contrib.gis.gdal|<code>GDALRaster</code>]].
+
存储 [[../gdal#django.contrib.gis.gdal|GDALRaster]]
  
<code>RasterField</code> is currently only implemented for the PostGIS backend.
+
<code>RasterField</code> 目前只为 PostGIS 后端实现。
  
  
第138行: 第135行:
 
<div id="spatial-field-options" class="section">
 
<div id="spatial-field-options" class="section">
  
== Spatial Field Options ==
+
== 空间字段选项 ==
  
In addition to the regular [[../../../models/fields#common-model-field-options|<span class="std std-ref">字段选项</span>]] available for
+
除了可用于 Django 模型字段的常规 [[../../../models/fields#common-model-field-options|字段选项]] 之外,空间字段还有以下附加选项。 都是可选的。
Django model fields, spatial fields have the following additional options.
 
All are optional.
 
  
 
<div id="srid" class="section">
 
<div id="srid" class="section">
  
=== <code>srid</code> ===
+
=== srid ===
  
; <code>BaseSpatialField.</code><code>srid</code>
+
; <span class="sig-prename descclassname"><span class="pre">BaseSpatialField.</span></span><span class="sig-name descname"><span class="pre">srid</span></span>
 
:  
 
:  
  
Sets the SRID [[#fnogcsrid|2]] (Spatial Reference System Identity) of the geometry field to
+
将几何字段的 SRID [[#fnogcsrid|2]](空间参考系统标识)设置为给定值。 默认为 4326(也称为 [https://en.wikipedia.org/wiki/WGS84 WGS84],单位为经度和纬度)。
the given value. Defaults to 4326 (also known as [https://en.wikipedia.org/wiki/WGS84 WGS84], units are in degrees
 
of longitude and latitude).
 
  
 
<div id="selecting-an-srid" class="section">
 
<div id="selecting-an-srid" class="section">
  
 
<span id="id6"></span>
 
<span id="id6"></span>
==== Selecting an SRID ====
+
==== 选择 SRID ====
  
Choosing an appropriate SRID for your model is an important decision that the
+
为您的模型选择合适的 SRID 是开发人员应仔细考虑的重要决定。 SRID 是一个整数说明符,对应于将用于解释空间数据库中数据的投影系统。 [[#fnsrid|3]] 投影系统为指定位置的坐标提供上下文。 尽管 [https://en.wikipedia.org/wiki/Geodesy geodesy] 的细节超出了本文档的范围,但普遍的问题是地球是球形的,而地球的表示(例如,纸质地图、Web 地图)不是。
developer should consider carefully. The SRID is an integer specifier that
 
corresponds to the projection system that will be used to interpret the data
 
in the spatial database. [[#fnsrid|3]] Projection systems give the context to the
 
coordinates that specify a location. Although the details of [https://en.wikipedia.org/wiki/Geodesy geodesy] are
 
beyond the scope of this documentation, the general problem is that the earth
 
is spherical and representations of the earth (e.g., paper maps, Web maps)
 
are not.
 
  
Most people are familiar with using latitude and longitude to reference a
+
大多数人都熟悉使用纬度和经度来引用地球表面上的位置。 但是,纬度和经度是角度,而不是距离。 换句话说,平面上两点之间的最短路径是直线,而曲面(例如地球)上两点之间的最短路径是[https://en.wikipedia.org/wiki/Great_circle 的''弧''大圆][[#fnthematic|4]] 因此,需要额外的计算来获得平面单位(例如,公里和英里)的距离。 使用地理坐标系可能会给开发人员带来麻烦。 例如,SpatiaLite 不具有使用地理坐标系在几何之间执行距离计算的能力,例如 构建查询以查找存储为 WGS84 的县边界 5 英里内的所有点。 [[#fndist|5]]
location on the earth's surface. However, latitude and longitude are angles,
 
not distances. In other words, while the shortest path between two points on
 
a flat surface is a straight line, the shortest path between two points on a curved
 
surface (such as the earth) is an ''arc'' of a [https://en.wikipedia.org/wiki/Great_circle great circle]. [[#fnthematic|4]] Thus,
 
additional computation is required to obtain distances in planar units (e.g.,
 
kilometers and miles). Using a geographic coordinate system may introduce
 
complications for the developer later on. For example, SpatiaLite does not have
 
the capability to perform distance calculations between geometries using
 
geographic coordinate systems, e.g. constructing a query to find all points
 
within 5 miles of a county boundary stored as WGS84.
 
[[#fndist|5]]
 
  
Portions of the earth's surface may projected onto a two-dimensional, or
+
地球表面的一部分可以投影到二维或笛卡尔平面上。 投影坐标系对于特定区域的应用程序特别方便,例如,如果您知道您的数据库将仅涵盖 [http://www.spatialreference.org/ref/epsg/2796/ 北堪萨斯] 中的几何图形,那么您可以考虑使用特定于该区域的投影系统。 此外,投影坐标系以笛卡尔单位(例如米或英尺)定义,便于距离计算。
Cartesian, plane. Projected coordinate systems are especially convenient
 
for region-specific applications, e.g., if you know that your database will
 
only cover geometries in [http://www.spatialreference.org/ref/epsg/2796/ North Kansas], then you may consider using projection
 
system specific to that region. Moreover, projected coordinate systems are
 
defined in Cartesian units (such as meters or feet), easing distance
 
calculations.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
If you wish to perform arbitrary distance queries using non-point
+
如果您希望在 PostGIS 中使用 WGS84 中的非点几何执行任意距离查询,并且希望获得不错的性能,请启用 [[#django.contrib.gis.db.models.GeometryField.geography|GeometryField.geography]] 关键字,以便使用 [[#geography-type|geography 数据库类型]]
geometries in WGS84 in PostGIS and you want decent performance, enable the
 
[[#django.contrib.gis.db.models.GeometryField.geography|<code>GeometryField.geography</code>]] keyword so that [[#geography-type|<span class="std std-ref">geography database
 
type</span>]] is used instead.
 
  
  
 
</div>
 
</div>
Additional Resources:
+
其他资源:
  
* [http://spatialreference.org/ spatialreference.org]: A Django-powered database of spatial reference systems.
+
* [http://spatialreference.org/ spatialreference.org]:Django 驱动的空间参考系统数据库。
* [https://web.archive.org/web/20080302095452/http://welcome.warnercnr.colostate.edu/class_info/nr502/lg3/datums_coordinates/spcs.html The State Plane Coordinate System]: A website covering the various projection systems used in the United States. Much of the U.S. spatial data encountered will be in one of these coordinate systems rather than in a geographic coordinate system such as WGS84.
+
* [https://web.archive.org/web/20080302095452/http://welcome.warnercnr.colostate.edu/class_info/nr502/lg3/datums_coordinates/spcs.html The State Plane Coordinate System]:一个涵盖美国使用的各种投影系统的网站。 美国大部分地区 遇到的空间数据将位于这些坐标系之一中,而不是位于诸如 WGS84 之类的地理坐标系中。
  
  
第212行: 第178行:
 
<div id="spatial-index" class="section">
 
<div id="spatial-index" class="section">
  
=== <code>spatial_index</code> ===
+
=== spatial_index ===
  
; <code>BaseSpatialField.</code><code>spatial_index</code>
+
; <span class="sig-prename descclassname"><span class="pre">BaseSpatialField.</span></span><span class="sig-name descname"><span class="pre">spatial_index</span></span>
 
:  
 
:  
  
Defaults to <code>True</code>. Creates a spatial index for the given geometry
+
默认为 <code>True</code>。 为给定的几何字段创建空间索引。
field.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
This is different from the <code>db_index</code> field option because spatial
+
这与 <code>db_index</code> 字段选项不同,因为空间索引的创建方式与常规数据库索引不同。 具体来说,空间索引通常使用 R 树的变体创建,而常规数据库索引通常使用 B 树。
indexes are created in a different manner than regular database
 
indexes. Specifically, spatial indexes are typically created using
 
a variant of the R-Tree, while regular database indexes typically
 
use B-Trees.
 
  
  
第239行: 第200行:
  
 
<span id="id15"></span>
 
<span id="id15"></span>
== Geometry Field Options ==
+
== 几何字段选项 ==
  
There are additional options available for Geometry fields. All the following
+
几何字段还有其他可用选项。 以下所有选项都是可选的。
options are optional.
 
  
 
<div id="dim" class="section">
 
<div id="dim" class="section">
  
=== <code>dim</code> ===
+
=== dim ===
  
; <code>GeometryField.</code><code>dim</code>
+
; <span class="sig-prename descclassname"><span class="pre">GeometryField.</span></span><span class="sig-name descname"><span class="pre">dim</span></span>
 
:  
 
:  
  
This option may be used for customizing the coordinate dimension of the
+
此选项可用于自定义几何字段的坐标尺寸。 默认情况下,它设置为 2,用于表示二维几何。 对于支持它的空间后端,它可以设置为 3 以进行三维支持。
geometry field. By default, it is set to 2, for representing two-dimensional
 
geometries. For spatial backends that support it, it may be set to 3 for
 
three-dimensional support.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
At this time 3D support is limited to the PostGIS and SpatiaLite backends.
+
目前 3D 支持仅限于 PostGIS SpatiaLite 后端。
  
  
第268行: 第225行:
 
<div id="geography" class="section">
 
<div id="geography" class="section">
  
=== <code>geography</code> ===
+
=== geography ===
  
; <code>GeometryField.</code><code>geography</code>
+
; <span class="sig-prename descclassname"><span class="pre">GeometryField.</span></span><span class="sig-name descname"><span class="pre">geography</span></span>
 
:  
 
:  
  
If set to <code>True</code>, this option will create a database column of
+
如果设置为 <code>True</code>,此选项将创建一个地理类型的数据库列,而不是几何。 有关详细信息,请参阅下面的 [[#geography-type|地理类型]] 部分。
type geography, rather than geometry. Please refer to the
 
[[#geography-type|<span class="std std-ref">geography type</span>]] section below for more
 
details.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
Geography support is limited to PostGIS and will force the SRID to be 4326.
+
地理支持仅限于 PostGIS,并将强制 SRID 为 4326。
  
  
第289行: 第243行:
  
 
<span id="id16"></span>
 
<span id="id16"></span>
==== Geography Type ====
+
==== 地理类型 ====
  
The geography type provides native support for spatial features represented
+
geography 类型为用地理坐标(例如,WGS84 经度/纬度)表示的空间特征提供本机支持。 [[#fngeography|6]] 与几何类型使用的平面不同,地理类型使用其数据的球面表示。 在地理柱上执行的距离和测量操作会自动采用大圆弧计算并返回线性单位。 换句话说,当在两个地理区域上调用 <code>ST_Distance</code> 时,将返回一个以米为单位的值(如果在 WGS84 中的几何列上调用,则与度相反)。
with geographic coordinates (e.g., WGS84 longitude/latitude). [[#fngeography|6]]
 
Unlike the plane used by a geometry type, the geography type uses a spherical
 
representation of its data. Distance and measurement operations
 
performed on a geography column automatically employ great circle arc
 
calculations and return linear units. In other words, when <code>ST_Distance</code>
 
is called on two geographies, a value in meters is returned (as opposed
 
to degrees if called on a geometry column in WGS84).
 
  
Because geography calculations involve more mathematics, only a subset of the
+
由于地理计算涉及更多数学,因此只有一部分 PostGIS 空间查找可用于地理类型。 实际上,这意味着除了 [[../geoquerysets#distance-lookups|距离查找]] 之外,只有以下额外的 [[../geoquerysets#spatial-lookups|空间查找]] 可用于地理列:
PostGIS spatial lookups are available for the geography type. Practically,
 
this means that in addition to the [[../geoquerysets#distance-lookups|<span class="std std-ref">distance lookups</span>]]
 
only the following additional [[../geoquerysets#spatial-lookups|<span class="std std-ref">spatial lookups</span>]] are
 
available for geography columns:
 
  
* [[../geoquerysets#std-fieldlookup-bboverlaps|<code>bboverlaps</code>]]
+
* [[#id18|<span id="id19" class="problematic">:lookup:`bboverlaps`</span>]]
* [[../geoquerysets#std-fieldlookup-coveredby|<code>coveredby</code>]]
+
* [[#id20|<span id="id21" class="problematic">:lookup:`coveredby`</span>]]
* [[../geoquerysets#std-fieldlookup-covers|<code>covers</code>]]
+
* [[#id22|<span id="id23" class="problematic">:lookup:`covers`</span>]]
* [[../geoquerysets#std-fieldlookup-intersects|<code>intersects</code>]]
+
* [[#id24|<span id="id25" class="problematic">:lookup:`intersects`</span>]]
  
If you need to use a spatial lookup or aggregate that doesn't support the
+
如果您需要使用不支持 geography 类型作为输入的空间查找或聚合,您可以使用 [[../../../models/database-functions#django.db.models.functions|Cast]] 数据库函数将 geography 列转换为查询中的几何类型:
geography type as input, you can use the
 
[[../../../models/database-functions#django.db.models.functions|<code>Cast</code>]] database function to convert the
 
geography column to a geometry type in the query:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第320行: 第260行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.contrib.gis.db.models import PointField
+
<syntaxhighlight lang="python">from django.contrib.gis.db.models import PointField
 
from django.db.models.functions import Cast
 
from django.db.models.functions import Cast
  
 
Zipcode.objects.annotate(
 
Zipcode.objects.annotate(
 
     geom=Cast('geography_field', PointField())
 
     geom=Cast('geography_field', PointField())
).filter(geom__within=poly)</pre>
+
).filter(geom__within=poly)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
For more information, the PostGIS documentation contains a helpful section on
+
有关更多信息,PostGIS 文档包含有关确定 [https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_GeographyVSGeometry 何时使用地理数据类型而不是几何数据类型] 的有用部分。
determining [https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_GeographyVSGeometry when to use geography data type over geometry data type].
 
  
Footnotes
+
脚注
  
 
; <span class="brackets">[[#id3|1]]</span>
 
; <span class="brackets">[[#id3|1]]</span>
: OpenGIS Consortium, Inc., [https://www.opengeospatial.org/standards/sfs Simple Feature Specification For SQL].
+
: OpenGIS Consortium, Inc., [https://www.opengeospatial.org/standards/sfs SQL 的简单特征规范]
 
; <span class="brackets">[[#id4|2]]</span>
 
; <span class="brackets">[[#id4|2]]</span>
: ''See id.'' at Ch. 2.3.8, p. 39 (Geometry Values and Spatial Reference Systems).
+
: ''见第 id.'' 章节。 2.3.8,第。 39(几何值和空间参考系统)。
 
; <span class="brackets">[[#id7|3]]</span>
 
; <span class="brackets">[[#id7|3]]</span>
: Typically, SRID integer corresponds to an EPSG ([http://www.epsg.org European Petroleum Survey Group]) identifier. However, it may also be associated with custom projections defined in spatial database's spatial reference systems table.
+
: 通常,SRID 整数对应于 EPSG ( [http://www.epsg.org 欧洲石油调查组] ) 标识符。 但是,它也可能与空间数据库的空间参考系统表中定义的自定义投影相关联。
 
; <span class="brackets">[[#id8|4]]</span>
 
; <span class="brackets">[[#id8|4]]</span>
: Terry A. Slocum, Robert B. McMaster, Fritz C. Kessler, &amp; Hugh H. Howard, ''Thematic Cartography and Geographic Visualization'' (Prentice Hall, 2nd edition), at Ch. 7.1.3.
+
: 特里 A。 斯洛克姆,罗伯特 B. 麦克马斯特,弗里茨 C. 凯斯勒和休 H. 霍华德, ''专题制图和地理可视化'' (Prentice Hall,第 2 版),在 Ch。 7.1.3.
 
; <span class="brackets">[[#id9|5]]</span>
 
; <span class="brackets">[[#id9|5]]</span>
: This limitation does not apply to PostGIS.
+
: 此限制不适用于 PostGIS。
 
; <span class="brackets">[[#id17|6]]</span>
 
; <span class="brackets">[[#id17|6]]</span>
: Please refer to the [https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_Geography PostGIS Geography Type] documentation for more details.
+
: 有关详细信息,请参阅 [https://postgis.net/docs/using_postgis_dbmanagement.html#PostGIS_Geography PostGIS Geography Type] 文档。
  
  
第356行: 第295行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Django 2.2.x 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Django 2.2.x 文档]]

2021年10月31日 (日) 04:05的最新版本

GeoDjango 模型 API

本文档探讨了 GeoDjango 模型 API 的详细信息。 在本节中,我们将使用 邮政编码数字高程模型 的以下地理模型作为示例:

from django.contrib.gis.db import models

class Zipcode(models.Model):
    code = models.CharField(max_length=5)
    poly = models.PolygonField()

class Elevation(models.Model):
    name = models.CharField(max_length=100)
    rast = models.RasterField()

空间字段类型

空间字段由一系列几何字段类型和一个栅格字段类型组成。 每个几何字段类型都对应于 OpenGIS 简单要素规范 1。 栅格数据没有这样的标准。

GeometryField

class GeometryField

几何字段的基类。


PointField

class PointField

存储


LineStringField

class LineStringField

存储 LineString


PolygonField

class PolygonField

存储 多边形


MultiPointField

class MultiPointField

存储 MultiPoint


MultiLineStringField

class MultiLineStringField

存储 MultiLineString


MultiPolygonField

class MultiPolygonField

存储 MultiPolygon


GeometryCollectionField

class GeometryCollectionField

存储 GeometryCollection


RasterField

class RasterField

存储 GDALRaster

RasterField 目前只为 PostGIS 后端实现。


空间字段选项

除了可用于 Django 模型字段的常规 字段选项 之外,空间字段还有以下附加选项。 都是可选的。

srid

BaseSpatialField.srid

将几何字段的 SRID 2(空间参考系统标识)设置为给定值。 默认为 4326(也称为 WGS84,单位为经度和纬度)。

选择 SRID

为您的模型选择合适的 SRID 是开发人员应仔细考虑的重要决定。 SRID 是一个整数说明符,对应于将用于解释空间数据库中数据的投影系统。 3 投影系统为指定位置的坐标提供上下文。 尽管 geodesy 的细节超出了本文档的范围,但普遍的问题是地球是球形的,而地球的表示(例如,纸质地图、Web 地图)不是。

大多数人都熟悉使用纬度和经度来引用地球表面上的位置。 但是,纬度和经度是角度,而不是距离。 换句话说,平面上两点之间的最短路径是直线,而曲面(例如地球)上两点之间的最短路径是大圆4 因此,需要额外的计算来获得平面单位(例如,公里和英里)的距离。 使用地理坐标系可能会给开发人员带来麻烦。 例如,SpatiaLite 不具有使用地理坐标系在几何之间执行距离计算的能力,例如 构建查询以查找存储为 WGS84 的县边界 5 英里内的所有点。 5

地球表面的一部分可以投影到二维或笛卡尔平面上。 投影坐标系对于特定区域的应用程序特别方便,例如,如果您知道您的数据库将仅涵盖 北堪萨斯 中的几何图形,那么您可以考虑使用特定于该区域的投影系统。 此外,投影坐标系以笛卡尔单位(例如米或英尺)定义,便于距离计算。

笔记

如果您希望在 PostGIS 中使用 WGS84 中的非点几何执行任意距离查询,并且希望获得不错的性能,请启用 GeometryField.geography 关键字,以便使用 geography 数据库类型


其他资源:

  • spatialreference.org:Django 驱动的空间参考系统数据库。
  • The State Plane Coordinate System:一个涵盖美国使用的各种投影系统的网站。 美国大部分地区 遇到的空间数据将位于这些坐标系之一中,而不是位于诸如 WGS84 之类的地理坐标系中。


spatial_index

BaseSpatialField.spatial_index

默认为 True。 为给定的几何字段创建空间索引。

笔记

这与 db_index 字段选项不同,因为空间索引的创建方式与常规数据库索引不同。 具体来说,空间索引通常使用 R 树的变体创建,而常规数据库索引通常使用 B 树。


几何字段选项

几何字段还有其他可用选项。 以下所有选项都是可选的。

dim

GeometryField.dim

此选项可用于自定义几何字段的坐标尺寸。 默认情况下,它设置为 2,用于表示二维几何。 对于支持它的空间后端,它可以设置为 3 以进行三维支持。

笔记

目前 3D 支持仅限于 PostGIS 和 SpatiaLite 后端。


geography

GeometryField.geography

如果设置为 True,此选项将创建一个地理类型的数据库列,而不是几何。 有关详细信息,请参阅下面的 地理类型 部分。

笔记

地理支持仅限于 PostGIS,并将强制 SRID 为 4326。


地理类型

geography 类型为用地理坐标(例如,WGS84 经度/纬度)表示的空间特征提供本机支持。 6 与几何类型使用的平面不同,地理类型使用其数据的球面表示。 在地理柱上执行的距离和测量操作会自动采用大圆弧计算并返回线性单位。 换句话说,当在两个地理区域上调用 ST_Distance 时,将返回一个以米为单位的值(如果在 WGS84 中的几何列上调用,则与度相反)。

由于地理计算涉及更多数学,因此只有一部分 PostGIS 空间查找可用于地理类型。 实际上,这意味着除了 距离查找 之外,只有以下额外的 空间查找 可用于地理列:

如果您需要使用不支持 geography 类型作为输入的空间查找或聚合,您可以使用 Cast 数据库函数将 geography 列转换为查询中的几何类型:

from django.contrib.gis.db.models import PointField
from django.db.models.functions import Cast

Zipcode.objects.annotate(
    geom=Cast('geography_field', PointField())
).filter(geom__within=poly)

有关更多信息,PostGIS 文档包含有关确定 何时使用地理数据类型而不是几何数据类型 的有用部分。

脚注

1
OpenGIS Consortium, Inc., SQL 的简单特征规范
2
见第 id. 章节。 2.3.8,第。 39(几何值和空间参考系统)。
3
通常,SRID 整数对应于 EPSG ( 欧洲石油调查组 ) 标识符。 但是,它也可能与空间数据库的空间参考系统表中定义的自定义投影相关联。
4
特里 A。 斯洛克姆,罗伯特 B. 麦克马斯特,弗里茨 C. 凯斯勒和休 H. 霍华德, 专题制图和地理可视化 (Prentice Hall,第 2 版),在 Ch。 7.1.3.
5
此限制不适用于 PostGIS。
6
有关详细信息,请参阅 PostGIS Geography Type 文档。