==================GeoDjango Tutorial==================Introduction============GeoDjango is an included contrib module for Django that turns it into aworld-class geographic web framework. GeoDjango strives to make it as simpleas possible to create geographic web applications, like location-based services.Its features include:* Django model fields for `OGC`_ geometries and raster data.* Extensions to Django's ORM for querying and manipulating spatial data.* Loosely-coupled, high-level Python interfaces for GIS geometry and rasteroperations and data manipulation in different formats.* Editing geometry fields from the admin.This tutorial assumes familiarity with Django; thus, if you're brand new toDjango, please read through the :doc:`regular tutorial </intro/tutorial01>` tofamiliarize yourself with Django first... note::GeoDjango has additional requirements beyond what Django requires --please consult the :doc:`installation documentation <install/index>`for more details.This tutorial will guide you through the creation of a geographic webapplication for viewing the `world borders`_. [#]_ Some of the codeused in this tutorial is taken from and/or inspired by the `GeoDjangobasic apps`_ project. [#]_.. note::Proceed through the tutorial sections sequentially for step-by-stepinstructions... _OGC: https://www.ogc.org/.. _world borders: https://thematicmapping.org/downloads/world_borders.php.. _GeoDjango basic apps: https://code.google.com/archive/p/geodjango-basic-appsSetting Up==========Create a Spatial Database-------------------------Typically no special setup is required, so you can create a database as youwould for any other project. We provide some tips for selected databases:* :doc:`install/postgis`* :doc:`install/spatialite`Create a New Project--------------------Use the standard ``django-admin`` script to create a project called``geodjango``:.. console::$ django-admin startproject geodjangoThis will initialize a new project. Now, create a ``world`` Django applicationwithin the ``geodjango`` project:.. console::$ cd geodjango$ python manage.py startapp worldConfigure ``settings.py``-------------------------The ``geodjango`` project settings are stored in the ``geodjango/settings.py``file. Edit the database connection settings to match your setup::DATABASES = {'default': {'ENGINE': 'django.contrib.gis.db.backends.postgis','NAME': 'geodjango','USER': 'geo',},}In addition, modify the :setting:`INSTALLED_APPS` setting to include:mod:`django.contrib.admin`, :mod:`django.contrib.gis`,and ``world`` (your newly created application)::INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','django.contrib.gis','world',]Geographic Data===============.. _worldborders:World Borders-------------The world borders data is available in this `zip file`__. Create a ``data``directory in the ``world`` application, download the world borders data, andunzip. On GNU/Linux platforms, use the following commands:.. console::$ 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 ../..The world borders ZIP file contains a set of data files collectively known asan `ESRI Shapefile`__, one of the most popular geospatial data formats. Whenunzipped, the world borders dataset includes files with the followingextensions:* ``.shp``: Holds the vector data for the world borders geometries.* ``.shx``: Spatial index file for geometries stored in the ``.shp``.* ``.dbf``: Database file for holding non-geometric attribute data(e.g., integer and character fields).* ``.prj``: Contains the spatial reference information for the geographicdata stored in the shapefile.__ https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip__ https://en.wikipedia.org/wiki/ShapefileUse ``ogrinfo`` to examine spatial data---------------------------------------The GDAL ``ogrinfo`` utility allows examining the metadata of shapefiles orother vector data sources:.. console::$ ogrinfo world/data/TM_WORLD_BORDERS-0.3.shpINFO: 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 thislayer contains polygon data. To find out more, we'll specify the layer nameand 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.3INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'using driver `ESRI Shapefile' successful.Layer name: TM_WORLD_BORDERS-0.3Geometry: PolygonFeature Count: 246Extent: (-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)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 hasa maximum length of 2. Similarly, ``LON: Real (8.3)`` is a floating-pointfield that holds a maximum of 8 digits up to three decimal places.Geographic Models=================Defining a Geographic Model---------------------------Now that you've examined your dataset using ``ogrinfo``, create a GeoDjangomodel to represent this data::from django.contrib.gis.db import modelsclass 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.nameNote that the ``models`` module is imported from ``django.contrib.gis.db``.The default spatial reference system for geometry fields is WGS84 (meaningthe `SRID`__ is 4326) -- in other words, the field coordinates are inlongitude, latitude pairs in units of degrees. To use a differentcoordinate system, set the SRID of the geometry field with the ``srid``argument. Use an integer representing the coordinate system's EPSG code.__ https://en.wikipedia.org/wiki/SRIDRun ``migrate``---------------After defining your model, you need to sync it with the database. First,create a database migration:.. console::$ python manage.py makemigrationsMigrations for 'world':world/migrations/0001_initial.py:- Create model WorldBorderLet's look at the SQL that will generate the table for the ``WorldBorder``model:.. console::$ python manage.py sqlmigrate world 0001This command should produce the following output:.. console::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;If this looks correct, run :djadmin:`migrate` to create this table in thedatabase:.. console::$ python manage.py migrateOperations to perform:Apply all migrations: admin, auth, contenttypes, sessions, worldRunning migrations:...Applying world.0001_initial... OKImporting Spatial Data======================This section will show you how to import the world borders shapefile into thedatabase via GeoDjango models using the :doc:`layermapping`.There are many different ways to import data into a spatial database --besides the tools included within GeoDjango, you may also use the following:* `ogr2ogr`_: A command-line utility included with GDAL thatcan import many vector data formats into PostGIS, MySQL, and Oracle databases.* `shp2pgsql`_: This utility included with PostGIS imports ESRI shapefiles intoPostGIS... _ogr2ogr: https://gdal.org/programs/ogr2ogr.html.. _shp2pgsql: https://postgis.net/docs/using_postgis_dbmanagement.html#shp2pgsql_usage.. _gdalinterface:GDAL Interface--------------Earlier, you used ``ogrinfo`` to examine the contents of the world bordersshapefile. GeoDjango also includes a Pythonic interface to GDAL's powerful OGRlibrary that can work with all the vector data sources that OGR supports.First, invoke the Django shell:.. console::$ python manage.py shellIf you downloaded the :ref:`worldborders` data earlier in the tutorial, thenyou can determine its path using Python's :class:`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:class:`~django.contrib.gis.gdal.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.3You can see the layer's geometry type and how many features it contains::>>> print(lyr.geom_type)Polygon>>> print(len(lyr))246.. note::Unfortunately, the shapefile data format does not allow for greaterspecificity with regards to geometry types. This shapefile, likemany others, actually includes ``MultiPolygon`` geometries, not Polygons.It's important to use a more general field type in models: aGeoDjango ``MultiPolygonField`` will accept a ``Polygon`` geometry, but a``PolygonField`` will not accept a ``MultiPolygon`` type geometry. Thisis why the ``WorldBorder`` model defined above uses a ``MultiPolygonField``.The :class:`~django.contrib.gis.gdal.Layer` may also have a spatial referencesystem associated with it. If it does, the ``srs`` attribute will return a:class:`~django.contrib.gis.gdal.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'This shapefile is in the popular WGS84 spatial referencesystem -- in other words, the data uses longitude, latitude pairs inunits of degrees.In addition, shapefiles also support attribute fields that may containadditional data. Here are the fields on the World Borders layer:>>> print(lyr.fields)['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']The following code will let you examine the OGR types (e.g. integer orstring) associated with each of the fields:>>> [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 boththe feature's geometry (accessed via the ``geom`` attribute) as well as thefeature's attribute fields (whose **values** are accessed via ``get()``method)::>>> for feat in lyr:... print(feat.get('NAME'), feat.geom.num_points)...Guernsey 18Jersey 26South Georgia South Sandwich Islands 338Taiwan 363:class:`~django.contrib.gis.gdal.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 MarinoBoundary 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 Pathfrom django.contrib.gis.utils import LayerMappingfrom .models import WorldBorderworld_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)A few notes about what's going on:* Each key in the ``world_mapping`` dictionary corresponds to a field in the``WorldBorder`` model. The value is the name of the shapefile fieldthat data will be loaded from.* The key ``mpoly`` for the geometry field is ``MULTIPOLYGON``, thegeometry type GeoDjango will import the field as. Even simple polygons inthe shapefile will automatically be converted into collections prior toinsertion into the database.* The path to the shapefile is not absolute -- in other words, if you move the``world`` application (with ``data`` subdirectory) to a different location,the script will still work.* The ``transform`` keyword is set to ``False`` because the data in theshapefile does not need to be converted -- it's already in WGS84 (SRID=4326).Afterward, invoke the Django shell from the ``geodjango`` project directory:.. console::$ python manage.py shellNext, import the ``load`` module, call the ``run`` routine, and watch``LayerMapping`` do the work::>>> from world import load>>> load.run().. _ogrinspect-intro:Try ``ogrinspect``------------------Now that you've seen how to define geographic models and import data with the:doc:`layermapping`, it's possible to further automate this process withuse of the :djadmin:`ogrinspect` management command. The :djadmin:`ogrinspect`command introspects a GDAL-supported vector data source (e.g., a shapefile)and generates a model definition and ``LayerMapping`` dictionary automatically.The general usage of the command goes as follows:.. console::$ python 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 maybe used to further define how the model is generated.For example, the following command nearly reproduces the ``WorldBorder`` modeland mapping dictionary created above, automatically:.. console::$ python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorder \--srid=4326 --mapping --multiA few notes about the command-line options given above:* The ``--srid=4326`` option sets the SRID for the geographic field.* The ``--mapping`` option tells ``ogrinspect`` to also generate amapping dictionary for use with:class:`~django.contrib.gis.utils.LayerMapping`.* The ``--multi`` option is specified so that the geographic field is a:class:`~django.contrib.gis.db.models.MultiPolygonField` instead of just a:class:`~django.contrib.gis.db.models.PolygonField`.The command produces the following output, which may be copieddirectly into the ``models.py`` of a GeoDjango application::# This is an auto-generated Django model module created by ogrinspect.from django.contrib.gis.db import modelsclass 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 modelworldborders_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 Queries===============Spatial Lookups---------------GeoDjango adds spatial lookups to the Django ORM. For example, youcan find the country in the ``WorldBorder`` table that containsa particular point. First, fire up the management shell:.. console::$ python manage.py shellNow, define a point of interest [#]_::>>> 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 asWell Known Text (WKT), a standard issued by the Open GeospatialConsortium (OGC). [#]_ Import the ``WorldBorder`` model, and performa ``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>]>Here, you retrieved a ``QuerySet`` with only one model: the border of theUnited States (exactly what you would expect).Similarly, you may also use a :doc:`GEOS geometry object <geos>`.Here, you can combine the ``intersects`` spatial lookup with the ``get``method to retrieve only the ``WorldBorder`` instance for San Marino insteadof 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 theavailable queries -- the :doc:`db-api` documentation has more... _automatic-spatial-transformations:Automatic Spatial Transformations---------------------------------When doing spatial queries, GeoDjango automatically transformsgeometries if they're in a different coordinate system. In the followingexample, 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 ofWKT that includes the SRID::>>> pnt = GEOSGeometry('SRID=32140;POINT(954158.1 4215137.1)')GeoDjango's ORM will automatically wrap geometry valuesin transformation SQL, allowing the developer to work at a higher levelof abstraction::>>> qs = WorldBorder.objects.filter(mpoly__intersects=pnt)>>> print(qs.query) # Generating the SQLSELECT "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>]>__ https://spatialreference.org/ref/epsg/32140/.. _gis-raw-sql:.. admonition:: Raw queriesWhen using :doc:`raw queries </topics/db/sql>`, you must wrap your geometryfields 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 connectionsconnection = connections['your_gis_db_alias']City.objects.raw('SELECT id, name, %s as point from myapp_city' % (connection.ops.select % 'point'))You should only use raw queries when you know exactly what you're doing.Lazy Geometries---------------GeoDjango loads geometries in a standardized textual representation. When thegeometry field is first accessed, GeoDjango creates a:class:`~django.contrib.gis.geos.GEOSGeometry` object, exposing powerfulfunctionality, such as serialization properties for popular geospatialformats::>>> sm = WorldBorder.objects.get(name='San Marino')>>> sm.mpoly<MultiPolygon object at 0x24c6798>>>> sm.mpoly.wkt # WKTMULTIPOLYGON (((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 bythe GEOS library::>>> pnt = Point(12.4604, 43.9420)>>> sm.mpoly.contains(pnt)True>>> pnt.contains(sm.mpoly)FalseGeographic annotations----------------------GeoDjango also offers a set of geographic annotations to compute distances andseveral other operations (intersection, difference, etc.). See the:doc:`functions` documentation.Putting your data on the map============================Geographic Admin----------------:doc:`Django's admin application </ref/contrib/admin/index>` supports editinggeometry fields.Basics~~~~~~The Django admin allows users to create and modify geometries on a JavaScriptslippy 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 adminfrom .models import WorldBorderadmin.site.register(WorldBorder, admin.ModelAdmin)Next, edit your ``urls.py`` in the ``geodjango`` application folder as follows::from django.contrib import adminfrom django.urls import include, pathurlpatterns = [path('admin/', admin.site.urls),]Create an admin user:.. console::$ python manage.py createsuperuserNext, start up the Django development server:.. console::$ python manage.py runserverFinally, browse to ``http://localhost:8000/admin/``, and log in with the useryou just created. Browse to any of the ``WorldBorder`` entries -- the bordersmay be edited by clicking on a polygon and dragging the vertices to the desiredposition... _OpenLayers: https://openlayers.org/.. _OpenStreetMap: https://www.openstreetmap.org/.. _Vector Map Level 0: http://web.archive.org/web/20201024202709/https://earth-info.nga.mil/publications/vmap0.html.. _OSGeo: https://www.osgeo.org/``GISModelAdmin``~~~~~~~~~~~~~~~~~With the :class:`~django.contrib.gis.admin.GISModelAdmin`, GeoDjango uses an`OpenStreetMap`_ layer in the admin.This provides more context (including street and thoroughfare details) thanavailable with the :class:`~django.contrib.admin.ModelAdmin` (which uses the`Vector Map Level 0`_ WMS dataset hosted at `OSGeo`_).The PROJ datum shifting files must be installed (see the :ref:`PROJinstallation instructions <proj4>` for more details).If you meet this requirement, then use the ``GISModelAdmin`` option classin your ``admin.py`` file::admin.site.register(WorldBorder, admin.GISModelAdmin).. rubric:: Footnotes.. [#] Special thanks to Bjørn Sandvik of `thematicmapping.org<https://thematicmapping.org/>`_ for providing and maintaining thisdataset... [#] GeoDjango basic apps was written by Dane Springmeyer, Josh Livni, andChristopher Schmidt... [#] This point is the `University of Houston Law Center<https://www.law.uh.edu/>`_... [#] Open Geospatial Consortium, Inc., `OpenGIS Simple Feature SpecificationFor SQL <https://www.ogc.org/standards/sfs>`_.