---
title: "GeoDjango 教程"
version: 6.0
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/6.0/ref/contrib/gis/tutorial/
canonical: https://djangodocs.dev/zh-hans/6.0/ref/contrib/gis/tutorial/
---
# GeoDjango 教程

## 介绍

GeoDjango 是 Django 的一个包含的贡献模块，将其转变为一个世界级的地理 Web 框架。GeoDjango 力求使创建地理 Web 应用程序（如基于位置的服务）尽可能简单化。其功能包括：

- [OGC](https://www.ogc.org/) 几何图形和光栅数据的 Django 模型字段。
- Django ORM 的扩展，用于查询和操作空间数据。
- 松散耦合的高级 Python 接口，用于 GIS 几何和栅格操作以及不同格式的数据处理。
- 从管理界面编辑几何字段。

本教程假设你对 Django 很熟悉，因此，如果你是一个全新的 Django 新手，请先阅读 [常规教程](/zh-hans/6.0/intro/tutorial01/) 来熟悉 Django。

> **Note**
>
> GeoDjango has additional requirements beyond what Django requires -- please
> consult the [installation documentation](/zh-hans/6.0/ref/contrib/gis/install/) for more details.

本教程将指导你创建一个用于查看 [世界边界](#id3) 的地理网络应用。[^1] 本教程中使用的一些代码来自于/或者说是受启发于 [GeoDjango 基本应用](https://web.archive.org/web/20240123190237/https://thematicmapping.org/downloads/world_borders.php) 项目 [^2]

> **Note**
>
> 依次进行教程部分的步骤说明。

## 设置

### 创建一个空间数据库

通常情况下，不需要特殊的设置，因此你可以像创建其他项目一样创建数据库。我们为选定的数据库提供一些提示：

- [安装 PostGIS](/zh-hans/6.0/ref/contrib/gis/install/postgis/)
- [安装 SpatiaLite](/zh-hans/6.0/ref/contrib/gis/install/spatialite/)

### 创建一个新项目

使用标准的 `django-admin` 脚本创建一个名为 `geodjango` 的项目：

```console
$ django-admin startproject geodjango
```

*Windows*

```doscon
...\> django-admin startproject geodjango
```

这将初始化一个新项目。现在，在 `geodjango` 项目中创建一个 `world` Django 应用程序：

```console
$ cd geodjango
$ python manage.py startapp world
```

*Windows*

```doscon
...\> cd geodjango
...\> py manage.py startapp world
```

### 设置 `settings.py`

`geodjango` 项目配置存储在 `geodjango/settings.py` 文件中。编辑数据库连接配置以符合你的设置：

```
DATABASES = {
    "default": {
        "ENGINE": "django.contrib.gis.db.backends.postgis",
        "NAME": "geodjango",
        "USER": "geo",
    },
}
```

此外，修改 [`INSTALLED_APPS`](/zh-hans/6.0/ref/settings/#std-setting-INSTALLED_APPS) 设置，将 [`django.contrib.admin`](/zh-hans/6.0/ref/contrib/admin/#module-django.contrib.admin)、 [`django.contrib.gis`](/zh-hans/6.0/ref/contrib/gis/#module-django.contrib.gis) 和 `world` （你新创建的应用程序）包含进去：

```
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.gis",
    "world",
]
```

## 地理数据

### 世界边界

The world borders data is available in this [zip file](https://web.archive.org/web/20231220150759/https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip). Create a `data`
directory in the `world` application, download the world borders data, and
unzip. On GNU/Linux platforms, use the following commands:

```console
$ mkdir world/data
$ cd world/data
$ wget https://web.archive.org/web/20231220150759/https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
$ unzip TM_WORLD_BORDERS-0.3.zip
$ cd ../..
```

*Windows*

```doscon
...\> mkdir world\data
...\> cd world\data
...\> wget https://web.archive.org/web/20231220150759/https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
...\> unzip TM_WORLD_BORDERS-0.3.zip
...\> cd ..\..
```

The world borders ZIP file contains a set of data files collectively known as
an [ESRI Shapefile](https://en.wikipedia.org/wiki/Shapefile), one of the most popular geospatial data formats. When
unzipped, the world borders dataset includes files with the following
extensions:

- `.shp`：保存世界边界几何图形的向量数据。
- `.shx`：`.shp` 中存储的几何体空间索引文件。
- `.dbf`：用于存放非几何属性数据（如整数和字符字段）的数据库文件。
- `.prj`：包含形状文件中存储的地理数据的空间参考信息。

### 使用 `ogrinfo` 检查空间数据

GDAL `ogrinfo` 实用程序允许检查形状文件或其他矢量数据源的元数据：

```console
$ ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp
INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'
      using driver `ESRI Shapefile' successful.
1: TM_WORLD_BORDERS-0.3 (Polygon)
```

*Windows*

```doscon
...\> ogrinfo world\data\TM_WORLD_BORDERS-0.3.shp
INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'
      using driver `ESRI Shapefile' successful.
1: TM_WORLD_BORDERS-0.3 (Polygon)
```

`ogrinfo` tells us that the shapefile has one layer, and that this
layer contains polygon data. To find out more, we'll specify the layer name
and use the `-so` option to get only the important summary information:

```console
$ ogrinfo -so world/data/TM_WORLD_BORDERS-0.3.shp TM_WORLD_BORDERS-0.3
INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'
      using driver `ESRI Shapefile' successful.

Layer name: TM_WORLD_BORDERS-0.3
Metadata:
  DBF_DATE_LAST_UPDATE=2008-07-30
Geometry: Polygon
Feature Count: 246
Extent: (-180.000000, -90.000000) - (180.000000, 83.623596)
Layer SRS WKT:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
FIPS: String (2.0)
ISO2: String (2.0)
ISO3: String (3.0)
UN: Integer (3.0)
NAME: String (50.0)
AREA: Integer (7.0)
POP2005: Integer64 (10.0)
REGION: Integer (3.0)
SUBREGION: Integer (3.0)
LON: Real (8.3)
LAT: Real (7.3)
```

*Windows*

```doscon
...\> ogrinfo -so world\data\TM_WORLD_BORDERS-0.3.shp TM_WORLD_BORDERS-0.3
INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'
      using driver `ESRI Shapefile' successful.

Layer name: TM_WORLD_BORDERS-0.3
Metadata:
  DBF_DATE_LAST_UPDATE=2008-07-30
Geometry: Polygon
Feature Count: 246
Extent: (-180.000000, -90.000000) - (180.000000, 83.623596)
Layer SRS WKT:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
FIPS: String (2.0)
ISO2: String (2.0)
ISO3: String (3.0)
UN: Integer (3.0)
NAME: String (50.0)
AREA: Integer (7.0)
POP2005: Integer64 (10.0)
REGION: Integer (3.0)
SUBREGION: Integer (3.0)
LON: Real (8.3)
LAT: Real (7.3)
```

This detailed summary information tells us the number of features in the layer
(246), the geographic bounds of the data, the spatial reference system
("SRS WKT"), as well as type information for each attribute field. For example,
`FIPS: String (2.0)` indicates that the `FIPS` character field has
a maximum length of 2. Similarly, `LON: Real (8.3)` is a floating-point
field that holds a maximum of 8 digits up to three decimal places.

## 地理模型

### 定义地理模型

现在你已经用 `ogrinfo` 检查了你的数据集，创建一个 GeoDjango 模型来表示这个数据：

```
from django.contrib.gis.db import models

class WorldBorder(models.Model):
    # Regular Django fields corresponding to the attributes in the
    # world borders shapefile.
    name = models.CharField(max_length=50)
    area = models.IntegerField()
    pop2005 = models.IntegerField("Population 2005")
    fips = models.CharField("FIPS Code", max_length=2, null=True)
    iso2 = models.CharField("2 Digit ISO", max_length=2)
    iso3 = models.CharField("3 Digit ISO", max_length=3)
    un = models.IntegerField("United Nations Code")
    region = models.IntegerField("Region Code")
    subregion = models.IntegerField("Sub-Region Code")
    lon = models.FloatField()
    lat = models.FloatField()

    # GeoDjango-specific: a geometry field (MultiPolygonField)
    mpoly = models.MultiPolygonField()

    # Returns the string representation of the model.
    def __str__(self):
        return self.name
```

请注意，`models` 模块是从 `django.contrib.gis.db` 导入的。

The default spatial reference system for geometry fields is WGS84 (meaning
the [SRID](https://en.wikipedia.org/wiki/SRID) is 4326) -- in other words, the field coordinates are in
longitude, latitude pairs in units of degrees. To use a different
coordinate system, set the SRID of the geometry field with the `srid`
argument. Use an integer representing the coordinate system's EPSG code.

### 运行 `migrate`

定义完模型后，你需要将其与数据库同步。首先，创建一个数据库迁移：

```console
$ python manage.py makemigrations
Migrations for 'world':
  world/migrations/0001_initial.py:
    + Create model WorldBorder
```

*Windows*

```doscon
...\> py manage.py makemigrations
Migrations for 'world':
  world/migrations/0001_initial.py:
    + Create model WorldBorder
```

让我们看看将为 `WorldBorder` 模型生成表的 SQL：

```console
$ python manage.py sqlmigrate world 0001
```

*Windows*

```doscon
...\> py manage.py sqlmigrate world 0001
```

该命令应产生以下输出：

```sql
BEGIN;
--
-- Create model WorldBorder
--
CREATE TABLE "world_worldborder" (
    "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    "name" varchar(50) NOT NULL,
    "area" integer NOT NULL,
    "pop2005" integer NOT NULL,
    "fips" varchar(2) NOT NULL,
    "iso2" varchar(2) NOT NULL,
    "iso3" varchar(3) NOT NULL,
    "un" integer NOT NULL,
    "region" integer NOT NULL,
    "subregion" integer NOT NULL,
    "lon" double precision NOT NULL,
    "lat" double precision NOT NULL
    "mpoly" geometry(MULTIPOLYGON,4326) NOT NULL
)
;
CREATE INDEX "world_worldborder_mpoly_id" ON "world_worldborder" USING GIST ("mpoly");
COMMIT;
```

如果这个看起来是正确的，运行 [`migrate`](/zh-hans/6.0/ref/django-admin/#django-admin-migrate) 在数据库中创建这个表：

```console
$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, world
Running migrations:
  ...
  Applying world.0001_initial... OK
```

*Windows*

```doscon
...\> py manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, world
Running migrations:
  ...
  Applying world.0001_initial... OK
```

## 导入空间数据

This section will show you how to import the world borders shapefile into the
database via GeoDjango models using the [LayerMapping 是一个数据导入实用程序](/zh-hans/6.0/ref/contrib/gis/layermapping/).

有许多不同的方法可以将数据导入空间数据库——除了 GeoDjango 中包含的工具外，你还可以使用以下方法：

- [ogr2ogr](https://gdal.org/programs/ogr2ogr.html) ：GDAL 包含的一个命令行实用程序，可将许多矢量数据格式导入 PostGIS、MySQL 和 Oracle 数据库。
- [shp2pgsql](https://postgis.net/docs/using_postgis_dbmanagement.html#shp2pgsql_usage) ：PostGIS 所包含的这一工具将 ESRI 形状文件导入 PostGIS。

### GDAL 接口

Earlier, you used `ogrinfo` to examine the contents of the world borders
shapefile. GeoDjango also includes a Pythonic interface to GDAL's powerful OGR
library that can work with all the vector data sources that OGR supports.

首先，调用 Django 命令行：

```console
$ python manage.py shell
```

*Windows*

```doscon
...\> py manage.py shell
```

如果您在教程早期下载了 [世界边界](#worldborders) 数据，那么您可以使用 Python 的 [`pathlib.Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) 来确定其路径：

```pycon
>>> from pathlib import Path
>>> import world
>>> world_shp = Path(world.__file__).resolve().parent / "data" / "TM_WORLD_BORDERS-0.3.shp"
```

现在，使用 GeoDjango 的 [`DataSource`](/zh-hans/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.DataSource) 接口打开世界边界 shapefile 文件：

```pycon
>>> from django.contrib.gis.gdal import DataSource
>>> ds = DataSource(world_shp)
>>> print(ds)
/ ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)
```

数据源对象可以具有不同层的地理空间要素；然而，shapefile 只允许有一个图层：

```pycon
>>> print(len(ds))
1
>>> lyr = ds[0]
>>> print(lyr)
TM_WORLD_BORDERS-0.3
```

您可以查看图层的几何类型以及它包含多少要素：

```pycon
>>> print(lyr.geom_type)
Polygon
>>> print(len(lyr))
246
```

> **Note**
>
> Unfortunately, the shapefile data format does not allow for greater
> specificity with regards to geometry types. This shapefile, like many
> others, actually includes `MultiPolygon` geometries, not Polygons. It's
> important to use a more general field type in models: a GeoDjango
> `MultiPolygonField` will accept a `Polygon` geometry, but a
> `PolygonField` will not accept a `MultiPolygon` type geometry. This is
> why the `WorldBorder` model defined above uses a `MultiPolygonField`.

The [`Layer`](/zh-hans/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.Layer) may also have a spatial reference
system associated with it. If it does, the `srs` attribute will return a
[`SpatialReference`](/zh-hans/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.SpatialReference) object:

```pycon
>>> srs = lyr.srs
>>> print(srs)
GEOGCS["WGS 84",
DATUM["WGS_1984",
    SPHEROID["WGS 84",6378137,298.257223563,
        AUTHORITY["EPSG","7030"]],
    AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
    AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
    AUTHORITY["EPSG","9122"]],
AXIS["Latitude",NORTH],
AXIS["Longitude",EAST],
AUTHORITY["EPSG","4326"]]
>>> srs.proj  # PROJ representation
'+proj=longlat +datum=WGS84 +no_defs'
```

这个形状文件采用流行的 WGS84 空间参考系统——换句话说，数据使用经度、纬度对，单位是度。

In addition, shapefiles also support attribute fields that may contain
additional data. Here are the fields on the World Borders layer:

```pycon
>>> print(lyr.fields)
['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']
```

下面的代码将让你检查与每个字段相关联的 OGR 类型（如整数或字符串）：

```pycon
>>> [fld.__name__ for fld in lyr.field_types]
['OFTString', 'OFTString', 'OFTString', 'OFTInteger', 'OFTString', 'OFTInteger', 'OFTInteger64', 'OFTInteger', 'OFTInteger', 'OFTReal', 'OFTReal']
```

您可以迭代图层中的每个要素，并从要素的几何（通过 `geom` 属性访问）以及要素的属性字段（其 **值** 通过 `get()` 方法访问）中提取信息：

```pycon
>>> for feat in lyr:
...     print(feat.get("NAME"), feat.geom.num_points)
...
Guernsey 18
Jersey 26
South Georgia South Sandwich Islands 338
Taiwan 363
```

[`Layer`](/zh-hans/6.0/ref/contrib/gis/gdal/#django.contrib.gis.gdal.Layer) 对象可以进行切片操作：

```pycon
>>> lyr[0:2]
[<django.contrib.gis.gdal.feature.Feature object at 0x2f47690>, <django.contrib.gis.gdal.feature.Feature object at 0x2f47650>]
```

并且可以通过其要素 ID 检索单个要素：

```pycon
>>> feat = lyr[234]
>>> print(feat.get("NAME"))
San Marino
```

边界几何体可以导出为 WKT 和 GeoJSON 格式：

```pycon
>>> geom = feat.geom
>>> print(geom.wkt)
POLYGON ((12.415798 43.957954,12.450554 ...
>>> print(geom.json)
{ "type": "Polygon", "coordinates": [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...
```

### `LayerMapping`

要导入数据，可以在 Python 脚本中使用 `LayerMapping`。在 `world` 应用程序内创建一个名为 `load.py` 的文件，其中包含以下代码：

```
from pathlib import Path
from django.contrib.gis.utils import LayerMapping
from .models import WorldBorder

world_mapping = {
    "fips": "FIPS",
    "iso2": "ISO2",
    "iso3": "ISO3",
    "un": "UN",
    "name": "NAME",
    "area": "AREA",
    "pop2005": "POP2005",
    "region": "REGION",
    "subregion": "SUBREGION",
    "lon": "LON",
    "lat": "LAT",
    "mpoly": "MULTIPOLYGON",
}

world_shp = Path(__file__).resolve().parent / "data" / "TM_WORLD_BORDERS-0.3.shp"

def run(verbose=True):
    lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False)
    lm.save(strict=True, verbose=verbose)
```

说说现在的情况：

- Each key in the `world_mapping` dictionary corresponds to a field in the
  `WorldBorder` model. The value is the name of the shapefile field
  that data will be loaded from.
- The key `mpoly` for the geometry field is `MULTIPOLYGON`, the
  geometry type GeoDjango will import the field as. Even simple polygons in
  the shapefile will automatically be converted into collections prior to
  insertion into the database.
- 形状文件的路径不是绝对的——换句话说，如果你把 `world` 应用程序（带 `data` 子目录）移到不同的位置，脚本仍然会工作。
- `transform` 关键字设置为 `False`，因为形状文件中的数据不需要转换——它已经是 WGS84 的数据（SRID=4326）。

之后，从 `geodjango` 项目目录中调用 Django shell：

```console
$ python manage.py shell
```

*Windows*

```doscon
...\> py manage.py shell
```

接下来，导入 `load` 模块，调用 `run` 函数，观察 `LayerMapping` 完成工作：

```pycon
>>> from world import load
>>> load.run()
```

### 试试 `ogrinspect`

Now that you've seen how to define geographic models and import data with the
[LayerMapping 是一个数据导入实用程序](/zh-hans/6.0/ref/contrib/gis/layermapping/), it's possible to further automate this
process with use of the [`ogrinspect`](/zh-hans/6.0/ref/contrib/gis/commands/#django-admin-ogrinspect) management command. The
[`ogrinspect`](/zh-hans/6.0/ref/contrib/gis/commands/#django-admin-ogrinspect) command  introspects a GDAL-supported vector data source
(e.g., a shapefile) and generates a model definition and `LayerMapping`
dictionary automatically.

该命令的一般用法如下：

```console
$ python manage.py ogrinspect [options] <data_source> <model_name> [options]
```

*Windows*

```doscon
...\> py manage.py ogrinspect [options] <data_source> <model_name> [options]
```

`data_source` is the path to the GDAL-supported data source and
`model_name` is the name to use for the model. Command-line options may
be used to further define how the model is generated.

例如，下面的命令几乎自动复制了上面创建的 `WorldBorder` 模型和映射字典：

```console
$ python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorder \
    --srid=4326 --mapping --multi
```

*Windows*

```doscon
...\> py manage.py ogrinspect world\data\TM_WORLD_BORDERS-0.3.shp WorldBorder \
    --srid=4326 --mapping --multi
```

关于上面给出的命令行选项的一些说明：

- `--srid=4326` 选项设置地理区域的 SRID。
- `--mapping` 选项告诉 `ogrinspect` 也要生成一个映射字典，供 [`LayerMapping`](/zh-hans/6.0/ref/contrib/gis/layermapping/#django.contrib.gis.utils.LayerMapping) 使用。
- 指定了 `--multi` 选项，这样地理区域就是一个 [`MultiPolygonField`](/zh-hans/6.0/ref/contrib/gis/model-api/#django.contrib.gis.db.models.MultiPolygonField) 而不是一个 [`PolygonField`](/zh-hans/6.0/ref/contrib/gis/model-api/#django.contrib.gis.db.models.PolygonField)。

该命令产生以下输出，可以直接复制到 GeoDjango 应用程序的 `models.py` 中：

```
# This is an auto-generated Django model module created by ogrinspect.
from django.contrib.gis.db import models

class WorldBorder(models.Model):
    fips = models.CharField(max_length=2)
    iso2 = models.CharField(max_length=2)
    iso3 = models.CharField(max_length=3)
    un = models.IntegerField()
    name = models.CharField(max_length=50)
    area = models.IntegerField()
    pop2005 = models.IntegerField()
    region = models.IntegerField()
    subregion = models.IntegerField()
    lon = models.FloatField()
    lat = models.FloatField()
    geom = models.MultiPolygonField(srid=4326)

# Auto-generated `LayerMapping` dictionary for WorldBorder model
worldborders_mapping = {
    "fips": "FIPS",
    "iso2": "ISO2",
    "iso3": "ISO3",
    "un": "UN",
    "name": "NAME",
    "area": "AREA",
    "pop2005": "POP2005",
    "region": "REGION",
    "subregion": "SUBREGION",
    "lon": "LON",
    "lat": "LAT",
    "geom": "MULTIPOLYGON",
}
```

## 空间查询

### 空间查找

GeoDjango adds spatial lookups to the Django ORM. For example, you
can find the country in the `WorldBorder` table that contains
a particular point. First, fire up the management shell:

```console
$ python manage.py shell
```

*Windows*

```doscon
...\> py manage.py shell
```

现在，定义一个兴趣点 [^3]：

```pycon
>>> pnt_wkt = "POINT(-95.3385 29.7245)"
```

The `pnt_wkt` string represents the point at -95.3385 degrees longitude,
29.7245 degrees latitude. The geometry is in a format known as
Well Known Text (WKT), a standard issued by the Open Geospatial
Consortium (OGC). [^4]  Import the `WorldBorder` model, and perform
a `contains` lookup using the `pnt_wkt` as the parameter:

```pycon
>>> from world.models import WorldBorder
>>> WorldBorder.objects.filter(mpoly__contains=pnt_wkt)
<QuerySet [<WorldBorder: United States>]>
```

在这里，你检索到的 `QuerySet` 只有一个模型：美国的边界（正是你所期望的）。

Similarly, you may also use a [GEOS geometry object](/zh-hans/6.0/ref/contrib/gis/geos/). Here, you can combine the `intersects` spatial
lookup with the `get` method to retrieve only the `WorldBorder` instance
for San Marino instead of a queryset:

```pycon
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(12.4604, 43.9420)
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
<WorldBorder: San Marino>
```

The `contains` and `intersects` lookups are just a subset of the available
queries -- the [GeoDjango 数据库 API](/zh-hans/6.0/ref/contrib/gis/db-api/) documentation has more.

### 自动空间变换

When doing spatial queries, GeoDjango automatically transforms
geometries if they're in a different coordinate system. In the following
example, coordinates will be expressed in [EPSG SRID 32140](https://spatialreference.org/ref/epsg/32140/),
a coordinate system specific to south Texas **only** and in units of
**meters**, not degrees:

```pycon
>>> from django.contrib.gis.geos import GEOSGeometry, Point
>>> pnt = Point(954158.1, 4215137.1, srid=32140)
```

请注意，`pnt` 也可以使用 EWKT 构建，EWKT 是 WKT 的"扩展"形式，包括 SRID 信息：

```pycon
>>> pnt = GEOSGeometry("SRID=32140;POINT(954158.1 4215137.1)")
```

GeoDjango 的 ORM 将自动将几何值包装在变换 SQL 中，使开发人员能够以更高的抽象级别工作：

```pycon
>>> qs = WorldBorder.objects.filter(mpoly__intersects=pnt)
>>> print(qs.query)  # Generating the SQL
SELECT "world_worldborder"."id", "world_worldborder"."name", "world_worldborder"."area",
"world_worldborder"."pop2005", "world_worldborder"."fips", "world_worldborder"."iso2",
"world_worldborder"."iso3", "world_worldborder"."un", "world_worldborder"."region",
"world_worldborder"."subregion", "world_worldborder"."lon", "world_worldborder"."lat",
"world_worldborder"."mpoly" FROM "world_worldborder"
WHERE ST_Intersects("world_worldborder"."mpoly", ST_Transform(%s, 4326))
>>> qs  # printing evaluates the queryset
<QuerySet [<WorldBorder: United States>]>
```

> **原始查询**
>
> 在使用 [原始查询](/zh-hans/6.0/topics/db/sql/) 时，必须包装几何字段，以便 GEOS 可以识别字段值：
>
> ```pycon
> >>> from django.db import connection
> >>> # or if you're querying a non-default database:
> >>> from django.db import connections
> >>> connection = connections["your_gis_db_alias"]
> >>> City.objects.raw(
> ...     "SELECT id, name, %s as point from myapp_city" % (connection.ops.select % "point")
> ... )
> ```
>
> 只有当你确切知道自己在做什么的时候，你才应该使用原始查询。

### 惰性几何

GeoDjango loads geometries in a standardized textual representation. When the
geometry field is first accessed, GeoDjango creates a
[`GEOSGeometry`](/zh-hans/6.0/ref/contrib/gis/geos/#django.contrib.gis.geos.GEOSGeometry) object, exposing powerful
functionality, such as serialization properties for popular geospatial
formats:

```pycon
>>> sm = WorldBorder.objects.get(name="San Marino")
>>> sm.mpoly
<MultiPolygon object at 0x24c6798>
>>> sm.mpoly.wkt  # WKT
MULTIPOLYGON (((12.4157980000000006 43.9579540000000009, 12.4505540000000003 43.9797209999999978, ...
>>> sm.mpoly.wkb  # WKB (as Python binary buffer)
<read-only buffer for 0x1fe2c70, size -1, offset 0 at 0x2564c40>
>>> sm.mpoly.geojson  # GeoJSON
'{ "type": "MultiPolygon", "coordinates": [ [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...
```

这包括访问 GEOS 库提供的所有高级几何操作：

```pycon
>>> pnt = Point(12.4604, 43.9420)
>>> sm.mpoly.contains(pnt)
True
>>> pnt.contains(sm.mpoly)
False
```

### 地理注解

GeoDjango also offers a set of geographic annotations to compute distances and
several other operations (intersection, difference, etc.). See the
[地理数据库函数](/zh-hans/6.0/ref/contrib/gis/functions/) documentation.

## 将你的数据放在地图上

### 地理管理

[Django 的管理应用程序](/zh-hans/6.0/ref/contrib/admin/) 支持编辑几何字段。

#### 基础

Django 管理界面允许用户在一个由 [OpenLayers](https://openlayers.org/) 驱动的 JavaScript slippy 地图上创建和修改几何图形。

让我们开始吧。在 `world` 应用程序内创建一个名为 `admin.py` 的文件，其中包含以下代码：

```
from django.contrib.gis import admin
from .models import WorldBorder

admin.site.register(WorldBorder, admin.ModelAdmin)
```

接下来，在 `geodjango` 应用程序文件夹中编辑 `urls.py` 如下：

```
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("admin/", admin.site.urls),
]
```

创建一个管理用户：

```console
$ python manage.py createsuperuser
```

*Windows*

```doscon
...\> py manage.py createsuperuser
```

接下来，启动 Django 开发服务器：

```console
$ python manage.py runserver
```

*Windows*

```doscon
...\> py manage.py runserver
```

最后，浏览 `http://localhost:8000/admin/`，用你刚刚创建的用户登录。浏览到任何一个 `WorldBorder` 条目——通过点击多边形并将顶点拖到所需位置，可以编辑边界。

#### `GISModelAdmin`

使用 [`GISModelAdmin`](/zh-hans/6.0/ref/contrib/gis/admin/#django.contrib.gis.admin.GISModelAdmin)，GeoDjango 在管理界面中使用了一个 [OpenStreetMap](https://www.openstreetmap.org/) 图层。这提供了比 [`ModelAdmin`](/zh-hans/6.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin) 更多的上下文信息（包括街道和道路的详细信息），后者使用在 [OSGeo](https://www.osgeo.org/) 托管的 [Vector Map Level 0](http://web.archive.org/web/20201024202709/https://earth-info.nga.mil/publications/vmap0.html) WMS 数据集。

必须安装 PROJ 基准转换文件（请参阅 [PROJ 安装说明](/zh-hans/6.0/ref/contrib/gis/install/geolibs/#proj4) 以获取更多详细信息）。

如果满足这个要求，那么在您的 `admin.py` 文件中使用 `GISModelAdmin` 选项类：

```
admin.site.register(WorldBorder, admin.GISModelAdmin)
```

**脚注**

[^1]: 特别感谢 [mastermaps.net](https://mastermaps.net/) 的Bjørn Sandvik 提供和维护这一数据集。

[^2]: GeoDjango 基本应用由 Dane Springmeyer、Josh Livni 和 Christopher Schmidt 编写。

[^3]: 这一点是 [休斯顿大学法律中心](https://www.law.uh.edu/) 。

[^4]: 开放地理空间联盟（Open Geospatial Consortium, Inc.）的 [OpenGIS Simple Feature Specification For SQL](https://www.ogc.org/standards/sfs)。
