Pengajaran Tambahan GeoDjangoLink to this heading

Kata PengantarLink to this heading

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:

  • Bidang model Django untuk geometri OGC dan data raster.

  • Tambahan pada ORM Django untuk meminta dan merubah data spasial.

  • Hubungan-renggang, antarmuka tingkat-tinggi Python untuk geometri GIS dan tindakan-tindakan raster dan pengubahan data dalam bentuk-bentuk berbeda.

  • Mengubah bidang geometri dari admin.

Pengajaran tambahan ini menganggap keakraban dengan Django; dengan demikian, jika anda adalah baru pada Django, harap baca melalui the regular tutorial untuk mengakrabkan anda sendiri dengan Django terlebih dahulu.

Pengajaran tambahan ini akan memandu anda melalui pembuatan dari aplikasi jaringan geografis untuk menampilkan world borders. [1] Beberapa dari kode digunakan dalam pengajaran tambahan ini diambil dari dan/atau diilhami oleh proyek GeoDjango basic apps. [2]

PengaturanLink to this heading

Buat Basisdata SpasialLink to this heading

Secara khusus tidak ada pengaturan khusus diperlukan, jadi anda dapat membuat sebuah basisdata seperti anda aakan untuk proyek lain apapun. Kami menyediakan saran untuk basisdata terpilih:

Buat Proyek BaruLink to this heading

Gunakan tulisan django-admin standar untuk membuat sebuah proyek dipanggil geodjango:

Linux / macOS

Shell
$ django-admin startproject geodjango

Windows

Windows
...\> django-admin startproject geodjango

Ini akan memulai sebuah proyek baru. Sekarang, buat sebuah aplikasi Django world dalam proyek geodjango:

Linux / macOS

Shell
$ cd geodjango
$ python manage.py startapp world

Windows

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

Konfigurasi settings.pyLink to this heading

Pengaturan proyek geodjango disimpan dalam berkas geodjango/settings.py. Sunting pengaturan hubungan basisdata untuk mencocokkan pengaturan anda:

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

Sebagai tambahan, rubah pengaturan INSTALLED_APPS untuk menyertakan django.contrib.admin, django.contrib.gis, dan world (aplikasi dibuat terbaru anda):

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",
]

Data GeografisLink to this heading

Batasan DuniaLink 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: Menahan data vektor untuk geometri batasan dunia.

  • .shx: Berkas indeks spatial untuk geometri disimpan dalam .shp.

  • .dbf: Berkas basisdata untuk menampung data atribut bukan-geometri (misalnya, bidang integer dan karakter).

  • .prj: Mengandung informasi acuan spasial untuk data geografis disimpan dalam shapefile.

Gunakan ogrinfo untuk menjalankan data spasialLink to this heading

Kegunaan ogrinfo GDAL mengizinkan mennguji metadata dari shapefile atau sumber data vektor:

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.

Model GeografisLink to this heading

Menentukan Model GeografisLink to this heading

Sekarang anda telah mengujikan dataset anda menggunakan originfo, membuat sebuah model GeoDjango untuk mewakili data ini:

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

Catat bahwa modul models diimpor dari 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.

Jalankan migrateLink to this heading

Setelah menentukan model anda, anda butuh mensinkronisasikan itu dengan basisdata. Pertama, buat sebuah perpindahan basisdata:

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

Mari kita lihat pada SQL yang akan membangkitkan tabel untuk model WorldBorder:

Linux / macOS

Shell
$ python manage.py sqlmigrate world 0001

Windows

Windows
...\> py manage.py sqlmigrate world 0001

Perintah ini harus menghasilkan keluaran berikut:

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;

Jika ini terlihat benar, jalankan migrate untuk membuat tabel ini dalam basisdata:

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

Mengimpor Data SpasialLink to this heading

This section will show you how to import the world borders shapefile into the database via GeoDjango models using the Kegunaan impor data LayerMapping.

Ada banyak cara berbeda utuk meng impor data kedalam basisdata spasial -- disamping alat-alat disertakan dalam GeoDjango, anda mungkin juga menggunakan berikut:

  • ogr2ogr: Sebuah kegunaan baris-perintah disertakan dengan GDAL yang dapat mengimpor banyak bentuk data vektor kedalam basisdata PostGIS, MySQL, dan Oracle.

  • shp2pgsql: Kegunaan ini disertakan dengan PostGIS impor ESRI shapefile ke dalam PostGIS.

Antarmuka GDALLink 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.

Pertama, minta cangkang Django:

Linux / macOS

Shell
$ python manage.py shell

Windows

Windows
...\> py manage.py shell

If you downloaded the Batasan Dunia data earlier in the tutorial, then you can determine its path using Python's pathlib.Path:

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

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)

Data source objects can have different layers of geospatial features; however, shapefiles are only allowed to have one layer:

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

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'

Shapefile ini dalam sistem acuan spasial WGS84 yang terkenal -- dengan kata lain, data menggunakan pasangan garis, bujur dalam satuan derajat.

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']

Kode berikut akan membiarkan anda menguji jenis-jenis OGR (misalnya integer atau string) berkaitan dengan masing-masing bidang:

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

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 objects may be sliced:

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

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

Boundary geometries may be exported as WKT and 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

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:

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)

Sedikit catatan mengenai apa yang terjadi:

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

  • Jalur pada shapefile tidak mutlak -- dengan kata lain, jika anda memindahkan apliaksi world (dengan subdirektori data) ke tempat berbeda, tulisan akan masih bekerja.

  • Kata kunci transform disetel menjadi False karena data dalam shapefile tidak butuh dirubah -- itu sudah dalam WGS84 (SRID=4326).

Afterward, invoke the Django shell from the geodjango project directory:

Linux / macOS

Shell
$ python manage.py shell

Windows

Windows
...\> py manage.py shell

Next, import the load module, call the run routine, and watch LayerMapping do the work:

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

Coba ogrinspectLink to this heading

Now that you've seen how to define geographic models and import data with the Kegunaan impor data 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.

Penggunaan umum dari perintah sebagai berikut:

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.

Sebagai contoh, perintah berikut hampir membuat kembali model WorldBorder dan dictionary pemetaan dibuat diatas, secara otomatis:

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

Sedikit catatan tentang pilihan baris-perintah diberikan diatas:

  • Pilihan --srid=4326 menyetel SRID untuk bidang geografis

  • Pilihan --mapping memberitahu ogrinspect untuk juga membangkitkan sebuah pemetaan dictionary untuk digunakan dengan class:~django.contrib.gis.utils.LayerMapping.

  • Pilihan --multi ditentukan sehingga bidang geografis adalah sebuah MultiPolygonField daripada hanya PolygonField.

Perintah menghasilkan keluaran berikut, yang mungkin disalin langsung kedalam models.py dari aplikasi GeoDjango:

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

Spatial QueriesLink to this heading

Pencarian SpasialLink 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

Now, define a point of interest [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 API Basisdata GeoDjango documentation has more.

Automatic Spatial TransformationsLink 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)

Note that pnt may also be constructed with EWKT, an "extended" form of WKT that includes the SRID:

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

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

Geometri LazyLink 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 ], ...

This includes access to all of the advanced geometric operations provided by the GEOS library:

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

Keterangan geografisLink to this heading

GeoDjango also offers a set of geographic annotations to compute distances and several other operations (intersection, difference, etc.). See the Fungsi Basisdata Geografis documentation.

Menaruh data anda di petaLink to this heading

Admin GeografisLink to this heading

Django's admin application supports editing geometry fields.

DasarLink to this heading

The Django admin allows users to create and modify geometries on a JavaScript slippy map (powered by OpenLayers).

Mari kita gali lebih dalam. Buat sebuah berkas disebut admin.py didalam aplikasi world dengan kode berikut:

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

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

Selanjutnya, sunting urls.py anda dalam pelipat aplikasi geodjango seperti berikut:

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

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

Buat pengguna admin:

Linux / macOS

Shell
$ python manage.py createsuperuser

Windows

Windows
...\> py manage.py createsuperuser

Selanjutnya, mulai peladen pengembangan Django:

Linux / macOS

Shell
$ python manage.py runserver

Windows

Windows
...\> py manage.py runserver

Akhirnya, telusuri http://localhost:8000/admin/, dan masuk dengan pengguna anda baru buat. Telusuri apapun dari masukan WorldBorder -- batasan mungkin disunting dengan mengklik pada poligon dan menarik simpul pada tempat yang diinginkan.

GISModelAdminLink to this heading

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

Jika anda memenuhi persyaratan ini, gunakan kelas pilihan GISModelAdmin di berkas admin.py anda:

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

Catatan kaki