GeoDjango 教程Link to this heading

介绍Link to this heading

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

  • OGC 几何图形和光栅数据的 Django 模型字段。

  • Django ORM 的扩展,用于查询和操作空间数据。

  • 松散耦合的高级 Python 接口,用于 GIS 几何和栅格操作以及不同格式的数据处理。

  • 从管理界面编辑几何字段。

本教程假设你对 Django 很熟悉,因此,如果你是一个全新的 Django 新手,请先阅读 常规教程 来熟悉 Django。

本教程将指导你创建一个用于查看 世界边界 的地理网络应用。[1] 本教程中使用的一些代码来自于/或者说是受启发于 GeoDjango 基本应用 项目 [2]

设置Link to this heading

创建一个空间数据库Link to this heading

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

创建一个新项目Link to this heading

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

Linux / macOS

Shell
$ django-admin startproject geodjango

Windows

Windows
...\> django-admin startproject geodjango

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

Linux / macOS

Shell
$ cd geodjango
$ python manage.py startapp world

Windows

Windows
...\> cd geodjango
...\> py manage.py startapp world

设置 settings.pyLink to this heading

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

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

此外,修改 INSTALLED_APPS 设置,将 django.contrib.admindjango.contrib.gisworld (你新创建的应用程序)包含进去:

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

地理数据Link to this heading

世界边界Link to this heading

The world borders data is available in this zip file. Create a data directory in the world application, download the world borders data, and unzip. On GNU/Linux platforms, use the following commands:

Linux / macOS

Shell
$ 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

Windows
...\> 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, 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 检查空间数据Link to this heading

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

Linux / macOS

Shell
$ 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

Windows
...\> 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:

Linux / macOS

Shell
$ 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

Windows
...\> 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.

地理模型Link to this heading

定义地理模型Link to this heading

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

Code
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 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.

运行 migrateLink to this heading

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

Linux / macOS

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

Windows

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

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

Linux / macOS

Shell
$ python manage.py sqlmigrate world 0001

Windows

Windows
...\> 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 在数据库中创建这个表:

Linux / macOS

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

Windows

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

导入空间数据Link to this heading

This section will show you how to import the world borders shapefile into the database via GeoDjango models using the LayerMapping 是一个数据导入实用程序.

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

  • ogr2ogr :GDAL 包含的一个命令行实用程序,可将许多矢量数据格式导入 PostGIS、MySQL 和 Oracle 数据库。

  • shp2pgsql :PostGIS 所包含的这一工具将 ESRI 形状文件导入 PostGIS。

GDAL 接口Link to this heading

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 命令行:

Linux / macOS

Shell
$ python manage.py shell

Windows

Windows
...\> py manage.py shell

如果您在教程早期下载了 世界边界 数据,那么您可以使用 Python 的 pathlib.Path 来确定其路径:

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

现在,使用 GeoDjango 的 DataSource 接口打开世界边界 shapefile 文件:

Python console
>>> 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 只允许有一个图层:

Python console
>>> print(len(ds))
1
>>> lyr = ds[0]
>>> print(lyr)
TM_WORLD_BORDERS-0.3

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

Python console
>>> print(lyr.geom_type)
Polygon
>>> print(len(lyr))
246

The Layer may also have a spatial reference system associated with it. If it does, the srs attribute will return a SpatialReference object:

Python console
>>> 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:

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

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

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

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

Python console
>>> 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 对象可以进行切片操作:

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

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

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

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

Python console
>>> 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 ], ...

LayerMappingLink to this heading

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

Code
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:

Linux / macOS

Shell
$ python manage.py shell

Windows

Windows
...\> py manage.py shell

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

Python console
>>> from world import load
>>> load.run()

试试 ogrinspectLink to this heading

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

该命令的一般用法如下:

Linux / macOS

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

Windows

Windows
...\> 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 模型和映射字典:

Linux / macOS

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

Windows

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

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

  • --srid=4326 选项设置地理区域的 SRID。

  • --mapping 选项告诉 ogrinspect 也要生成一个映射字典,供 LayerMapping 使用。

  • 指定了 --multi 选项,这样地理区域就是一个 MultiPolygonField 而不是一个 PolygonField

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

Code
# 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",
}

空间查询Link to this heading

空间查找Link to this heading

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:

Linux / macOS

Shell
$ python manage.py shell

Windows

Windows
...\> py manage.py shell

现在,定义一个兴趣点 [3]

Python console
>>> 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:

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

Here, you retrieved a QuerySet with only one model instance: the border of the United States (exactly what you would expect).

Similarly, you may also use a GEOS geometry object. 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:

Python console
>>> 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 documentation has more.

自动空间变换Link to this heading

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, a coordinate system specific to south Texas only and in units of meters, not degrees:

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

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

Python console
>>> pnt = GEOSGeometry("SRID=32140;POINT(954158.1 4215137.1)")

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

Python console
>>> 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>]>

惰性几何Link to this heading

GeoDjango loads geometries in a standardized textual representation. When the geometry field is first accessed, GeoDjango creates a GEOSGeometry object, exposing powerful functionality, such as serialization properties for popular geospatial formats:

Python console
>>> 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 库提供的所有高级几何操作:

Python console
>>> pnt = Point(12.4604, 43.9420)
>>> sm.mpoly.contains(pnt)
True
>>> pnt.contains(sm.mpoly)
False

地理注解Link to this heading

GeoDjango also offers a set of geographic annotations to compute distances and several other operations (intersection, difference, etc.). See the 地理数据库函数 documentation.

将你的数据放在地图上Link to this heading

地理管理Link to this heading

Django 的管理应用程序 支持编辑几何字段。

基础Link to this heading

Django 管理界面允许用户在一个由 OpenLayers 驱动的 JavaScript slippy 地图上创建和修改几何图形。

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

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

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

接下来,在 geodjango 应用程序文件夹中编辑 urls.py 如下:

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

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

创建一个管理用户:

Linux / macOS

Shell
$ python manage.py createsuperuser

Windows

Windows
...\> py manage.py createsuperuser

接下来,启动 Django 开发服务器:

Linux / macOS

Shell
$ python manage.py runserver

Windows

Windows
...\> py manage.py runserver

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

GISModelAdminLink to this heading

使用 GISModelAdmin,GeoDjango 在管理界面中使用了一个 OpenStreetMap 图层。这提供了比 ModelAdmin 更多的上下文信息(包括街道和道路的详细信息),后者使用在 OSGeo 托管的 Vector Map Level 0 WMS 数据集。

必须安装 PROJ 基准转换文件(请参阅 PROJ 安装说明 以获取更多详细信息)。

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

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

脚注