Tutoriel GeoDjango¶
Introduction¶
GeoDjango est un module contribué de Django qui le transforme en un système géographique web de grande envergure. L’objectif de GeoDjango est de faciliter le plus possible la création d’applications web géographiques telles que des services basés sur la géolocalisation. Ses fonctionnalités comprennent :
Des champs de modèle Django pour les objets géométriques OGC rz les données matricielles.
Des extensions de l’ORM de Django pour les requêtes et les manipulations des données spatiales.
Des interfaces Python faiblement liées et de haut niveau pour les opérations géométriques SIG et matricielles et la manipulation de données dans divers formats.
L’édition de champs géométriques dans l’interface d’administration.
Ce tutoriel part du principe que Django vous est familier ; si donc vous débutez avec Django, commencez par lire le tutoriel normal pour vous familiariser avec Django.
Note
GeoDjango has additional requirements beyond what Django requires – please consult the installation documentation for more details.
Ce tutoriel va vous guider dans les création d’une application web géographique pour visualiser les frontières mondiales. [1] Une partie du code de ce tutoriel est emprunté ou s’inspire du projet des applications de base GeoDjango. [2]
Note
Suivez dans l’ordre les sections de ce tutoriel afin d’obtenir des instructions pas à pas.
Mise en route¶
Création d’une base de données spatiale¶
En règle générale, aucune configuration spéciale n’est requise, ce qui fait que vous pouvez créer une base de données comme pour n’importe quel autre projet. Nous fournissons quelques astuces pour des bases de données précises :
Création d’un nouveau projet¶
Utilisez le script standard django-admin pour créer un projet nommé geodjango:
$ django-admin startproject geodjango
...\> django-admin startproject geodjango
Ceci va initialiser un nouveau projet. Créez maintenant une application Django world à l’intérieur du projet geodjango:
$ cd geodjango
$ python manage.py startapp world
...\> cd geodjango
...\> py manage.py startapp world
Configuration de settings.py¶
Les réglages du projet geodjango sont dans le fichier geodjango/settings.py. Modifiez les réglages de la connexion de base de données pour qu’ils correspondent à votre configuration :
DATABASES = {
"default": {
"ENGINE": "django.contrib.gis.db.backends.postgis",
"NAME": "geodjango",
"USER": "geo",
},
}
De plus, modifiez le réglage INSTALLED_APPS pour qu’il contienne django.contrib.admin, django.contrib.gis et world (l’application que vous venez de créer) :
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.gis",
"world",
]
Données géographiques¶
Frontières mondiales¶
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:
$ 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 ../..
...\> 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: contient les données vectorielles des objets géométriques des frontières mondiales..shx: fichier d’index spatial des objets géométriques stockés dans les fichiers.dbf: fichier de base de données contenant les données d’attributs non géométriques (par ex. des champs de nombres entiers ou de texte)..prj: contient les informations de référence spatiale pour les données géographiques stockées dans le shapefile.
Utilisation de ogrinfo pour examiner les données spatiales¶
L’utilitaire GDAL ogrinfo permet d’examiner les métadonnées des shapefiles ou d’autres sources de données vectorielles :
$ 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 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:
$ 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)
...\> 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.
Modèles géographiques¶
Définition d’un modèle géographique¶
Après avoir examiné le jeu de données à l’aide de ogrinfo, créez un modèle GeoDjango pour représenter ces données :
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
Notez que le module models est importé à partir de 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.
Exécutez migrate¶
Après avoir défini le modèle, vous devez le synchroniser avec la base de données. Créez tout d’abord une migration de base de données :
$ 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
Examinons le code SQL qui générera la table du modèle WorldBorder:
$ python manage.py sqlmigrate world 0001
...\> py manage.py sqlmigrate world 0001
Cette commande devrait produire le résultat suivant :
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;
Si cela vous semble correct, exécutez migrate pour créer la table dans la base de données :
$ 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
Importation de données spatiales¶
This section will show you how to import the world borders shapefile into the database via GeoDjango models using the Utilitaire d’importation de données LayerMapping.
Il existe plusieurs façons d’importer des données dans une base de données spatiale ; en plus des outils inclus avec GeoDjango, il est aussi possible d’utiliser :
ogr2ogr: un utilitaire en ligne de commande inclus dans GDAL et qui peut importer de nombreux formats de données vectorielles dans des bases de données PostGIS, MySQL et Oracle.
shp2pgsql: cet utilitaire inclus dans PostGIS importe des shapefiles ESRI dans PostGIS.
Interface 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.
Premièrement, lancez le shell Django :
$ python manage.py shell
...\> py manage.py shell
Si vous avez téléchargé les données Frontières mondiales précédemment dans ce tutoriel, vous pouvez déterminer son chemin en utilisant pathlib.Path de Python :
>>> from pathlib import Path
>>> import world
>>> world_shp = Path(world.__file__).resolve().parent / "data" / "TM_WORLD_BORDERS-0.3.shp"
Ouvrez maintenant le fichier shapefile des frontières mondiales en utilisant l’interface DataSource de GeoDjango :
>>> from django.contrib.gis.gdal import DataSource
>>> ds = DataSource(world_shp)
>>> print(ds)
/ ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)
Les objets des sources de données peuvent posséder plusieurs couches de contenu géospatial ; cependant, les fichiers shapefile ne peuvent contenir qu’une seule couche :
>>> print(len(ds))
1
>>> lyr = ds[0]
>>> print(lyr)
TM_WORLD_BORDERS-0.3
Vous pouvez voir le type géométrique de la couche ainsi que le nombre d’objets qu’elle contient :
>>> 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 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'
Ce fichier shapefile utilise le système de référence spatial bien connu WGS84 ; en d’autres termes, les données sont représentées par des couples longitude/latitude en degrés.
In addition, shapefiles also support attribute fields that may contain additional data. Here are the fields on the World Borders layer:
>>> print(lyr.fields)
['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']
Le code suivant permet d’examiner le type OGR (par ex. nombre entier ou chaîne de caractères) associé à chaque champ :
>>> [fld.__name__ for fld in lyr.field_types]
['OFTString', 'OFTString', 'OFTString', 'OFTInteger', 'OFTString', 'OFTInteger', 'OFTInteger64', 'OFTInteger', 'OFTInteger', 'OFTReal', 'OFTReal']
Il est possible de parcourir chaque objet de la couche et d’extraire des informations à la fois de la géométrie des objets (accessible par l’attribut geom) et des champs d’attribut des objets (dont les valeurs sont accessibles par la méthode get()) :
>>> for feat in lyr:
... print(feat.get("NAME"), feat.geom.num_points)
...
Guernsey 18
Jersey 26
South Georgia South Sandwich Islands 338
Taiwan 363
Les objets Layer peuvent être segmentés :
>>> lyr[0:2]
[<django.contrib.gis.gdal.feature.Feature object at 0x2f47690>, <django.contrib.gis.gdal.feature.Feature object at 0x2f47650>]
Et des objets individuels peuvent être récupérés par leur identifiant d’objet :
>>> feat = lyr[234]
>>> print(feat.get("NAME"))
San Marino
Les objets géométriques des frontières peuvent être exportés en tant que WKT ou 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 ], ...
Cartographie des couches (LayerMapping)¶
Pour importer les données, utilisez un objet LayerMapping dans un script Python. Créez un fichier nommé load.py dans l’application world et insérez-y le code suivant :
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)
Quelques notes au sujet du procédé :
Each key in the
world_mappingdictionary corresponds to a field in theWorldBordermodel. The value is the name of the shapefile field that data will be loaded from.The key
mpolyfor the geometry field isMULTIPOLYGON, 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.Le chemin vers le fichier shapefile n’est pas absolu ; en d’autres termes, si vous déplacez l’application
world(contenant le sous-répertoiredata) vers un autre emplacement, le script fonctionne toujours.Le paramètre
transformest défini àFalsecar les données du fichier shapefile ne doivent pas être converties, elles sont déjà au format WGS84 (SRID=4326).
Après cela, appelez le shell Django à partir du répertoire de projet geodjango:
$ python manage.py shell
...\> py manage.py shell
Puis, importez le module load, appelez la routine run et observez LayerMapping effectuant tout le travail :
>>> from world import load
>>> load.run()
Utilisation de ogrinspect¶
Now that you’ve seen how to define geographic models and import data with the
Utilitaire d’importation de données 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.
L’utilisation générale de la commande correspond à ceci :
$ python manage.py ogrinspect [options] <data_source> <model_name> [options]
...\> 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.
Par exemple, la commande suivante reproduit presque le modèle WorldBorder et le dictionnaire de correspondance créés ci-dessus, de façon automatique :
$ 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
Quelques notes sur les options de ligne de commande indiquées ci-dessus :
L’option
--srid=4326définit le SRID du champ géographique.L’option
--mappingindique àogrinspectde générer aussi un dictionnaire de correspondance à l’usage deLayerMapping.L’option
--multiest indiquée pour que le champ géographique utiliseMultiPolygonFieldau lieu d’un simplePolygonField.
La commande produit le résultat suivant, qui peut être directement copié dans un fichier models.py d’une application GeoDjango :
# 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",
}
Requêtes spatiales¶
Recherches spatiales¶
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:
$ python manage.py shell
...\> py manage.py shell
Définissez maintenant un point d’intérêt [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>]>
Vous venez d’obtenir un QuerySet contenant un seul modèle : la frontière des États-Unis (exactement ce que l’on espérait).
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>
The contains and intersects lookups are just a subset of the available
queries – the API de base de données GeoDjango documentation has more.
Transformations spatiales automatiques¶
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)
Notez que pnt peut aussi être construit avec EWKT, une forme « étendue » de WKT qui contient le SRID :
>>> pnt = GEOSGeometry("SRID=32140;POINT(954158.1 4215137.1)")
L’ORM de GeoDjango adapte automatiquement les valeurs géométriques dans le code de transformation SQL adéquat, permettant au développeur de travailler à un haut niveau d’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>]>
Requêtes brutes
Lors de l’utilisation de requêtes brutes, vous devez entourer vos champs géométriques afin que la valeur du champ soit reconnue par 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")
... )
Les requêtes brutes ne devraient être utilisées que lorsque l’on sait exactement ce que l’on fait.
Objets géométriques différés¶
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 ], ...
Cela comprend aussi l’accès à toutes les opérations géométriques avancées fournies par la bibliothèque GEOS :
>>> pnt = Point(12.4604, 43.9420)
>>> sm.mpoly.contains(pnt)
True
>>> pnt.contains(sm.mpoly)
False
Annotations géographiques¶
GeoDjango also offers a set of geographic annotations to compute distances and several other operations (intersection, difference, etc.). See the Fonctions de base de données géographiques documentation.
Placement des données sur une carte¶
Interface d’administration géographique¶
L’application d’administration de Django prend en charge l’édition des champs géométriques.
Généralités¶
L’administration de Django permet aux utilisateurs de créer et modifier des objets géométriques sur une carte active JavaScript (basée sur OpenLayers).
Mettons-nous au travail. Créez un fichier nommé admin.py dans l’application world et placez-y le code suivant :
from django.contrib.gis import admin
from .models import WorldBorder
admin.site.register(WorldBorder, admin.ModelAdmin)
Puis, modifiez urls.py dans le dossier d’application geodjango comme ceci :
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
]
Créez un utilisateur administrateur :
$ python manage.py createsuperuser
...\> py manage.py createsuperuser
Puis, démarrez le serveur de développement Django :
$ python manage.py runserver
...\> py manage.py runserver
Pour terminer, allez à l’adresse http://localhost:8000/admin/ et connectez-vous avec l’utilisateur que vous venez de créer. Parcourez l’un des objets WorldBorder et constatez que les frontières peuvent être modifiées en cliquant sur un polygone et en faisant glisser ses arêtes à la position souhaitée.
GISModelAdmin¶
Avec GISModelAdmin, GeoDjango utilise une couche OpenStreetMap dans l’interface d’administration. Cela donne plus de contexte (y compris les détails des rues et des voies de communication) que l’interface disponible avec ModelAdmin (qui utilise le jeu de données WMS Vector Map Level 0 hébergé sur OSGeo).
Les fichiers de projections PROJ doivent être installés (voir les instructions d’installation de PROJ pour plus de détails).
Si cette exigence est satisfaite, vous pouvez utiliser la classe GISModelAdmin dans le fichier admin.py:
admin.site.register(WorldBorder, admin.GISModelAdmin)
Notes de bas de page