GeoDjango is an included contrib module for Django that turns it into a world-class geographic web framework. GeoDjango strives to make it as simple as possible to create geographic web applications, like location-based services. Its features include:
本教程假设你对 Django 很熟悉,因此,如果你是一个全新的 Django 新手,请先阅读 常规教程 来熟悉 Django。
备注
GeoDjango 有一些额外的要求,超出了 Django 的要求——请查阅 安装文档 了解更多细节。
本教程将指导你创建一个用于查看 世界边界 的地理网络应用。[1] 本教程中使用的一些代码来自于“GeoDjango 基本应用”项目,或者说是受其启发。[2]
备注
依次进行教程部分的步骤说明。
使用标准的 django-admin
脚本创建一个名为 geodjango
的项目:
$ django-admin startproject geodjango
...\> django-admin startproject geodjango
这将初始化一个新项目。现在,在 geodjango
项目中创建一个 world
Django 应用程序:
$ cd geodjango
$ python manage.py startapp world
...\> 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
设置,将 django.contrib.admin
、 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",
]
世界边界数据就在这个 `zip file`__ 中。 在 world
应用程序中创建一个 data
目录,下载世界边界数据,然后解压。在 GNU/Linux 平台上,使用以下命令:
$ mkdir world/data
$ cd world/data
$ wget https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
$ unzip TM_WORLD_BORDERS-0.3.zip
$ cd ../..
...\> mkdir world\data
...\> cd world\data
...\> wget https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
...\> unzip TM_WORLD_BORDERS-0.3.zip
...\> cd ..\..
世界边界 ZIP 文件包含一组数据文件,统称为“ESRI Shapefile”,是最流行的地理空间数据格式之一。 解压后,世界边界数据集包括以下扩展名的文件:
.shp
:保存世界边界几何图形的向量数据。.shx
:.shp
中存储的几何体空间索引文件。.dbf
:用于存放非几何属性数据(如整数和字符字段)的数据库文件。.prj
:包含形状文件中存储的地理数据的空间参考信息。ogrinfo
检查空间数据¶GDAL ogrinfo
实用程序允许检查形状文件或其他矢量数据源的元数据:
$ 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 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
告诉我们形状文件有一个图层,这个图层包含多边形数据。 为了了解更多信息,我们将指定图层名称,并使用 so
选项只获取重要的摘要信息:
$ 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
Geometry: Polygon
Feature Count: 246
Extent: (-180.000000, -90.000000) - (180.000000, 83.623596)
Layer SRS WKT:
GEOGCS["GCS_WGS_1984",
DATUM["WGS_1984",
SPHEROID["WGS_1984",6378137.0,298.257223563]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]]
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: Integer (10.0)
REGION: Integer (3.0)
SUBREGION: Integer (3.0)
LON: Real (8.3)
LAT: Real (7.3)
...\> 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
Geometry: Polygon
Feature Count: 246
Extent: (-180.000000, -90.000000) - (180.000000, 83.623596)
Layer SRS WKT:
GEOGCS["GCS_WGS_1984",
DATUM["WGS_1984",
SPHEROID["WGS_1984",6378137.0,298.257223563]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]]
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: Integer (10.0)
REGION: Integer (3.0)
SUBREGION: Integer (3.0)
LON: Real (8.3)
LAT: Real (7.3)
这种详细的摘要信息告诉我们图层中特征的数量(246)、数据的地理界限、空间参考系统(“SRS WKT”)以及每个属性字段的类型信息。例如,FIPS: String (2.0)
表示 FIPS
字符域的最大长度为 2。同样,LON: Real (8.3)
是一个浮点字段,最多可容纳 8 位数字,最多可容纳 3 位小数。
现在你已经用 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
导入的。
几何场的默认空间参考系统是 WGS84(意味着 `SRID`__ 是 4326)——换句话说,场的坐标是以经度和纬度对为单位的。 要使用不同的坐标系,可以用 srid
参数设置几何字段的 SRID。使用一个整数代表坐标系的 EPSG 代码。
migrate
¶定义完模型后,你需要将其与数据库同步。首先,创建一个数据库迁移:
$ python manage.py makemigrations
Migrations for 'world':
world/migrations/0001_initial.py:
- Create model WorldBorder
...\> py manage.py makemigrations
Migrations for 'world':
world/migrations/0001_initial.py:
- Create model WorldBorder
让我们看看将为 WorldBorder
模型生成表的 SQL:
$ python manage.py sqlmigrate world 0001
...\> py manage.py sqlmigrate world 0001
该命令应产生以下输出:
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
在数据库中创建这个表:
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, world
Running migrations:
...
Applying world.0001_initial... OK
...\> py manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, world
Running migrations:
...
Applying world.0001_initial... OK
本节将展示如何通过 GeoDjango 模型使用 LayerMapping data import utility 将世界边界形状文件导入数据库。
有许多不同的方法可以将数据导入空间数据库——除了 GeoDjango 中包含的工具外,你还可以使用以下方法:
之前,你使用 ogrinfo
来检查世界边界形状文件的内容。 GeoDjango 还包含了一个 Pythonic 接口,用于 GDAL 强大的 OGR 库,它可以与 OGR 支持的所有矢量数据源一起工作。
首先,调用 Django 命令行:
$ python manage.py shell
...\> py manage.py shell
If you downloaded the 世界边界 data earlier in the tutorial, then
you can determine its path using Python's pathlib.Path
:
>>> from pathlib import Path
>>> import world
>>> world_shp = Path(world.__file__).resolve().parent / "data" / "TM_WORLD_BORDERS-0.3.shp"
Now, open the world borders shapefile using GeoDjango's
DataSource
interface:
>>> from django.contrib.gis.gdal import DataSource
>>> ds = DataSource(world_shp)
>>> print(ds)
/ ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)
Data source objects can have different layers of geospatial features; however, shapefiles are only allowed to have one layer:
>>> print(len(ds))
1
>>> lyr = ds[0]
>>> print(lyr)
TM_WORLD_BORDERS-0.3
You can see the layer's geometry type and how many features it contains:
>>> print(lyr.geom_type)
Polygon
>>> print(len(lyr))
246
备注
遗憾的是,形状文件数据格式不允许在几何体类型方面有更大的特殊性。 这个形状文件,和其他的形状一样,实际上包含了 MultiPolygon
的几何体,而不是多边形。在模型中使用更通用的字段类型是很重要的:GeoDjango 的 MultiPolygonField
将接受 Polygon
几何体,但 PolygonField
将不接受 MultiPolygon
类型的几何体。 这就是为什么上面定义的 WorldBorder
模型使用 MultiPolygonField
。
The Layer
may also have a spatial reference
system associated with it. If it does, the srs
attribute will return a
SpatialReference
object:
>>> 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 空间参考系统——换句话说,数据使用经度、纬度对,单位是度。
此外,空间文件还支持可能包含附加数据的属性字段。 以下是世界边界图层上的字段:
>>> print(lyr.fields)
['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']
下面的代码将让你检查与每个字段相关联的 OGR 类型(如整数或字符串):
>>> [fld.__name__ for fld in lyr.field_types]
['OFTString', 'OFTString', 'OFTString', 'OFTInteger', 'OFTString', 'OFTInteger', 'OFTInteger64', 'OFTInteger', 'OFTInteger', 'OFTReal', 'OFTReal']
You can iterate over each feature in the layer and extract information from both
the feature's geometry (accessed via the geom
attribute) as well as the
feature's attribute fields (whose values are accessed via get()
method):
>>> 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
objects may be sliced:
>>> lyr[0:2]
[<django.contrib.gis.gdal.feature.Feature object at 0x2f47690>, <django.contrib.gis.gdal.feature.Feature object at 0x2f47650>]
And individual features may be retrieved by their feature ID:
>>> feat = lyr[234]
>>> print(feat.get("NAME"))
San Marino
Boundary geometries may be exported as WKT and GeoJSON:
>>> 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
¶To import the data, use a LayerMapping
in a Python script.
Create a file called load.py
inside the world
application,
with the following 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)
说说现在的情况:
world_mapping
字典中的每个键对应 WorldBorder
模型中的一个字段。 其值是数据将被载入的形状文件字段的名称。mpoly
是 MULTIPOLYGON
,即 GeoDjango 将导入字段的几何类型。 即使是形状文件中的简单多边形,也会在插入数据库之前自动转换为集合。world
应用程序(带 data
子目录)移到不同的位置,脚本仍然会工作。transform
关键字设置为 False
,因为形状文件中的数据不需要转换——它已经是 WGS84 的数据(SRID=4326)。Afterward, invoke the Django shell from the geodjango
project directory:
$ python manage.py shell
...\> py manage.py shell
Next, import the load
module, call the run
routine, and watch
LayerMapping
do the work:
>>> from world import load
>>> load.run()
ogrinspect
¶现在你已经看到了如何使用 LayerMapping data import utility 定义地理模型和导入数据,可以使用 ogrinspect
管理命令进一步自动化这一过程。 ogrinspect
命令对 GDAL 支持的矢量数据源(如形状文件)进行内省,并自动生成模型定义和 LayerMapping
字典。
该命令的一般用法如下:
$ python manage.py ogrinspect [options] <data_source> <model_name> [options]
...\> py manage.py ogrinspect [options] <data_source> <model_name> [options]
data_source
是通往 GDAL 支持的数据源的路径,model_name
是用于模型的名称。 命令行选项可用于进一步定义模型的生成方式。
例如,下面的命令几乎自动复制了上面创建的 WorldBorder
模型和映射字典:
$ python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorder \
--srid=4326 --mapping --multi
...\> 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
中:
# 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 在 Django ORM 中增加了空间查询功能。 例如,你可以在 WorldBorder
表中找到包含某个点的国家。 首先,启动管理 shell:
$ python manage.py shell
...\> py manage.py shell
Now, define a point of interest [3]:
>>> 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:
>>> 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.
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:
>>> from django.contrib.gis.geos import Point
>>> pnt = Point(12.4604, 43.9420)
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
<WorldBorder: San Marino>
contains
和 intersects
查询只是可用查询的一个子集 —— GeoDjango 数据库 API 文档中有更多的内容。
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:
>>> from django.contrib.gis.geos import GEOSGeometry, Point
>>> pnt = Point(954158.1, 4215137.1, srid=32140)
Note that pnt
may also be constructed with EWKT, an "extended" form of
WKT that includes the SRID:
>>> pnt = GEOSGeometry("SRID=32140;POINT(954158.1 4215137.1)")
GeoDjango's ORM will automatically wrap geometry values in transformation SQL, allowing the developer to work at a higher level of abstraction:
>>> 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>]>
原始查询
When using raw queries, you must wrap your geometry fields so that the field value can be recognized by GEOS:
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
object, exposing powerful
functionality, such as serialization properties for popular geospatial
formats:
>>> 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 ], ...
This includes access to all of the advanced geometric operations provided by the GEOS library:
>>> pnt = Point(12.4604, 43.9420)
>>> sm.mpoly.contains(pnt)
True
>>> pnt.contains(sm.mpoly)
False
Django's admin application supports editing geometry fields.
The Django admin allows users to create and modify geometries on a JavaScript slippy map (powered by OpenLayers).
Let's dive right in. Create a file called admin.py
inside the world
application with the following code:
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),
]
创建一个管理用户:
$ python manage.py createsuperuser
...\> py manage.py createsuperuser
接下来,启动 Django 开发服务器:
$ python manage.py runserver
...\> py manage.py runserver
最后,浏览 http://localhost:8000/admin/
,用你刚刚创建的用户登录。浏览到任何一个 WorldBorder
条目——通过点击多边形并将顶点拖到所需位置,可以编辑边界。
GISModelAdmin
¶With the GISModelAdmin
, GeoDjango uses an
OpenStreetMap layer in the admin.
This provides more context (including street and thoroughfare details) than
available with the ModelAdmin
(which uses the
Vector Map Level 0 WMS dataset hosted at OSGeo).
The PROJ datum shifting files must be installed (see the PROJ installation instructions for more details).
If you meet this requirement, then use the GISModelAdmin
option class
in your admin.py
file:
admin.site.register(WorldBorder, admin.GISModelAdmin)
脚注
[1] | 特别感谢 thematicmapping.org 的Bjørn Sandvik 提供和维护这一数据集。 |
[2] | GeoDjango 基本应用由 Dane Springmeyer、Josh Livni 和 Christopher Schmidt 编写。 |
[3] | 这一点是 休斯顿大学法律中心 。 |
[4] | Open Geospatial Consortium, Inc., OpenGIS Simple Feature Specification For SQL. |
12月 05, 2023