2010-03-26 20:14:53 +00:00
|
|
|
==================
|
|
|
|
GeoDjango Tutorial
|
|
|
|
==================
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
============
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
GeoDjango is an included contrib module for Django that turns it into a
|
2021-07-23 06:48:16 +00:00
|
|
|
world-class geographic web framework. GeoDjango strives to make it as simple
|
|
|
|
as possible to create geographic web applications, like location-based services.
|
2012-11-18 06:20:49 +00:00
|
|
|
Its features include:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2015-06-19 15:46:03 +00:00
|
|
|
* Django model fields for `OGC`_ geometries and raster data.
|
2012-11-18 06:20:49 +00:00
|
|
|
* Extensions to Django's ORM for querying and manipulating spatial data.
|
2015-06-19 15:46:03 +00:00
|
|
|
* Loosely-coupled, high-level Python interfaces for GIS geometry and raster
|
|
|
|
operations and data manipulation in different formats.
|
2012-11-18 06:20:49 +00:00
|
|
|
* Editing geometry fields from the admin.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
This tutorial assumes familiarity with Django; thus, if you're brand new to
|
|
|
|
Django, please read through the :doc:`regular tutorial </intro/tutorial01>` to
|
|
|
|
familiarize yourself with Django first.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
GeoDjango has additional requirements beyond what Django requires --
|
2014-11-26 17:46:06 +00:00
|
|
|
please consult the :doc:`installation documentation <install/index>`
|
2010-03-26 20:14:53 +00:00
|
|
|
for more details.
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
This tutorial will guide you through the creation of a geographic web
|
2010-10-08 14:13:44 +00:00
|
|
|
application for viewing the `world borders`_. [#]_ Some of the code
|
|
|
|
used in this tutorial is taken from and/or inspired by the `GeoDjango
|
|
|
|
basic apps`_ project. [#]_
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
Proceed through the tutorial sections sequentially for step-by-step
|
2010-03-26 20:14:53 +00:00
|
|
|
instructions.
|
|
|
|
|
2021-04-27 11:09:00 +00:00
|
|
|
.. _OGC: https://www.ogc.org/
|
2018-09-26 06:48:47 +00:00
|
|
|
.. _world borders: https://thematicmapping.org/downloads/world_borders.php
|
2021-04-27 15:38:57 +00:00
|
|
|
.. _GeoDjango basic apps: https://code.google.com/archive/p/geodjango-basic-apps
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
Setting Up
|
|
|
|
==========
|
|
|
|
|
|
|
|
Create a Spatial Database
|
|
|
|
-------------------------
|
|
|
|
|
2015-09-10 22:37:58 +00:00
|
|
|
Typically no special setup is required, so you can create a database as you
|
|
|
|
would for any other project. We provide some tips for selected databases:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2015-09-10 22:37:58 +00:00
|
|
|
* :doc:`install/postgis`
|
|
|
|
* :doc:`install/spatialite`
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
Create a New Project
|
2017-03-20 22:30:32 +00:00
|
|
|
--------------------
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2014-07-26 11:21:52 +00:00
|
|
|
Use the standard ``django-admin`` script to create a project called
|
2012-11-18 06:20:49 +00:00
|
|
|
``geodjango``:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2014-07-26 11:21:52 +00:00
|
|
|
$ django-admin startproject geodjango
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
This will initialize a new project. Now, create a ``world`` Django application
|
|
|
|
within the ``geodjango`` project:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ cd geodjango
|
|
|
|
$ python manage.py startapp world
|
|
|
|
|
|
|
|
Configure ``settings.py``
|
|
|
|
-------------------------
|
|
|
|
|
2012-03-16 21:41:19 +00:00
|
|
|
The ``geodjango`` project settings are stored in the ``geodjango/settings.py``
|
2012-11-18 06:20:49 +00:00
|
|
|
file. Edit the database connection settings to match your setup::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
DATABASES = {
|
2010-03-26 20:14:53 +00:00
|
|
|
"default": {
|
2021-09-15 07:15:39 +00:00
|
|
|
"ENGINE": "django.contrib.gis.db.backends.postgis",
|
|
|
|
"NAME": "geodjango",
|
|
|
|
"USER": "geo",
|
2016-02-19 09:11:01 +00:00
|
|
|
},
|
2010-03-26 20:14:53 +00:00
|
|
|
}
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
In addition, modify the :setting:`INSTALLED_APPS` setting to include
|
2010-03-26 20:14:53 +00:00
|
|
|
:mod:`django.contrib.admin`, :mod:`django.contrib.gis`,
|
2012-11-18 06:20:49 +00:00
|
|
|
and ``world`` (your newly created application)::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2015-01-21 16:55:57 +00:00
|
|
|
INSTALLED_APPS = [
|
2014-01-24 21:43:00 +00:00
|
|
|
"django.contrib.admin",
|
2010-03-26 20:14:53 +00:00
|
|
|
"django.contrib.auth",
|
|
|
|
"django.contrib.contenttypes",
|
|
|
|
"django.contrib.sessions",
|
2012-03-16 21:41:19 +00:00
|
|
|
"django.contrib.messages",
|
|
|
|
"django.contrib.staticfiles",
|
2010-03-26 20:14:53 +00:00
|
|
|
"django.contrib.gis",
|
2016-02-19 09:11:01 +00:00
|
|
|
"world",
|
2015-01-21 16:55:57 +00:00
|
|
|
]
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
Geographic Data
|
|
|
|
===============
|
|
|
|
|
|
|
|
.. _worldborders:
|
|
|
|
|
|
|
|
World Borders
|
|
|
|
-------------
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
The world borders data is available in this `zip file`__. Create a ``data``
|
2011-09-28 14:00:43 +00:00
|
|
|
directory in the ``world`` application, download the world borders data, and
|
2012-11-18 06:20:49 +00:00
|
|
|
unzip. On GNU/Linux platforms, use the following commands:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ mkdir world/data
|
|
|
|
$ cd world/data
|
2018-09-26 06:48:47 +00:00
|
|
|
$ wget https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
|
2010-03-26 20:14:53 +00:00
|
|
|
$ 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
|
2012-11-18 06:20:49 +00:00
|
|
|
unzipped, the world borders dataset includes files with the following
|
2011-09-28 14:00:43 +00:00
|
|
|
extensions:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
* ``.shp``: Holds the vector data for the world borders geometries.
|
|
|
|
* ``.shx``: Spatial index file for geometries stored in the ``.shp``.
|
2010-08-19 19:27:44 +00:00
|
|
|
* ``.dbf``: Database file for holding non-geometric attribute data
|
|
|
|
(e.g., integer and character fields).
|
2010-03-26 20:14:53 +00:00
|
|
|
* ``.prj``: Contains the spatial reference information for the geographic
|
|
|
|
data stored in the shapefile.
|
|
|
|
|
2018-09-26 06:48:47 +00:00
|
|
|
__ https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
|
2015-08-08 10:02:32 +00:00
|
|
|
__ https://en.wikipedia.org/wiki/Shapefile
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
Use ``ogrinfo`` to examine spatial data
|
|
|
|
---------------------------------------
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
The GDAL ``ogrinfo`` utility allows examining the metadata of shapefiles or
|
|
|
|
other vector data sources:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ 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)
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
``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:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ 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)
|
|
|
|
|
|
|
|
This detailed summary information tells us the number of features in the layer
|
2012-11-18 06:20:49 +00:00
|
|
|
(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.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
Geographic Models
|
|
|
|
=================
|
|
|
|
|
|
|
|
Defining a Geographic Model
|
|
|
|
---------------------------
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
Now that you've examined your dataset using ``ogrinfo``, create a GeoDjango
|
|
|
|
model to represent this data::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
from django.contrib.gis.db import models
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2011-09-11 00:15:43 +00:00
|
|
|
class WorldBorder(models.Model):
|
2010-08-19 19:27:44 +00:00
|
|
|
# Regular Django fields corresponding to the attributes in the
|
2013-01-01 13:12:42 +00:00
|
|
|
# world borders shapefile.
|
2010-03-26 20:14:53 +00:00
|
|
|
name = models.CharField(max_length=50)
|
|
|
|
area = models.IntegerField()
|
|
|
|
pop2005 = models.IntegerField("Population 2005")
|
2020-06-10 19:56:18 +00:00
|
|
|
fips = models.CharField("FIPS Code", max_length=2, null=True)
|
2010-03-26 20:14:53 +00:00
|
|
|
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")
|
2013-01-01 13:12:42 +00:00
|
|
|
lon = models.FloatField()
|
|
|
|
lat = models.FloatField()
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2015-05-08 17:52:15 +00:00
|
|
|
# GeoDjango-specific: a geometry field (MultiPolygonField)
|
2013-01-01 13:12:42 +00:00
|
|
|
mpoly = models.MultiPolygonField()
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
# Returns the string representation of the model.
|
2017-01-18 16:51:29 +00:00
|
|
|
def __str__(self):
|
2010-03-26 20:14:53 +00:00
|
|
|
return self.name
|
|
|
|
|
2015-05-08 17:52:15 +00:00
|
|
|
Note that the ``models`` module is imported from ``django.contrib.gis.db``.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
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.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2015-08-08 10:02:32 +00:00
|
|
|
__ https://en.wikipedia.org/wiki/SRID
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2013-11-21 14:04:31 +00:00
|
|
|
Run ``migrate``
|
|
|
|
---------------
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
After defining your model, you need to sync it with the database. First,
|
2014-07-29 13:08:49 +00:00
|
|
|
create a database migration:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2014-07-29 13:08:49 +00:00
|
|
|
|
|
|
|
$ python manage.py makemigrations
|
|
|
|
Migrations for 'world':
|
2015-11-07 13:43:06 +00:00
|
|
|
world/migrations/0001_initial.py:
|
2023-09-16 02:11:22 +00:00
|
|
|
+ Create model WorldBorder
|
2014-07-29 13:08:49 +00:00
|
|
|
|
|
|
|
Let's look at the SQL that will generate the table for the ``WorldBorder``
|
|
|
|
model:
|
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2014-07-29 13:08:49 +00:00
|
|
|
|
|
|
|
$ python manage.py sqlmigrate world 0001
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
This command should produce the following output:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2023-12-28 18:52:15 +00:00
|
|
|
.. code-block:: sql
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2014-08-16 15:21:14 +00:00
|
|
|
BEGIN;
|
2015-03-29 14:59:35 +00:00
|
|
|
--
|
|
|
|
-- Create model WorldBorder
|
|
|
|
--
|
2012-02-11 01:48:45 +00:00
|
|
|
CREATE TABLE "world_worldborder" (
|
2022-08-26 19:42:44 +00:00
|
|
|
"id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
|
2010-03-26 20:14:53 +00:00
|
|
|
"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
|
2014-01-11 16:58:29 +00:00
|
|
|
"mpoly" geometry(MULTIPOLYGON,4326) NOT NULL
|
2010-03-26 20:14:53 +00:00
|
|
|
)
|
|
|
|
;
|
2021-09-15 07:15:39 +00:00
|
|
|
CREATE INDEX "world_worldborder_mpoly_id" ON "world_worldborder" USING GIST ("mpoly");
|
2014-08-16 15:21:14 +00:00
|
|
|
COMMIT;
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2014-07-29 13:08:49 +00:00
|
|
|
If this looks correct, run :djadmin:`migrate` to create this table in the
|
|
|
|
database:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2014-07-29 13:08:49 +00:00
|
|
|
$ python manage.py migrate
|
|
|
|
Operations to perform:
|
2016-01-28 06:52:00 +00:00
|
|
|
Apply all migrations: admin, auth, contenttypes, sessions, world
|
2014-07-29 13:08:49 +00:00
|
|
|
Running migrations:
|
|
|
|
...
|
|
|
|
Applying world.0001_initial... OK
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
Importing Spatial Data
|
|
|
|
======================
|
|
|
|
|
2014-11-26 17:46:06 +00:00
|
|
|
This section will show you how to import the world borders shapefile into the
|
|
|
|
database via GeoDjango models using the :doc:`layermapping`.
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
There are many different ways to import data into a spatial database --
|
|
|
|
besides the tools included within GeoDjango, you may also use the following:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
* `ogr2ogr`_: A command-line utility included with GDAL that
|
|
|
|
can import many vector data formats into PostGIS, MySQL, and Oracle databases.
|
|
|
|
* `shp2pgsql`_: This utility included with PostGIS imports ESRI shapefiles into
|
|
|
|
PostGIS.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2019-08-19 09:43:06 +00:00
|
|
|
.. _ogr2ogr: https://gdal.org/programs/ogr2ogr.html
|
2017-03-16 18:01:45 +00:00
|
|
|
.. _shp2pgsql: https://postgis.net/docs/using_postgis_dbmanagement.html#shp2pgsql_usage
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
.. _gdalinterface:
|
|
|
|
|
|
|
|
GDAL Interface
|
|
|
|
--------------
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
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.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2011-09-28 14:00:43 +00:00
|
|
|
First, invoke the Django shell:
|
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ python manage.py shell
|
|
|
|
|
2019-11-07 10:11:27 +00:00
|
|
|
If you downloaded the :ref:`worldborders` data earlier in the tutorial, then
|
|
|
|
you can determine its path using Python's :class:`pathlib.Path`:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2019-11-07 10:11:27 +00:00
|
|
|
.. code-block:: pycon
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2019-11-07 10:11:27 +00:00
|
|
|
>>> from pathlib import Path
|
2012-02-10 01:40:43 +00:00
|
|
|
>>> import world
|
2019-11-07 10:11:27 +00:00
|
|
|
>>> world_shp = Path(world.__file__).resolve().parent / "data" / "TM_WORLD_BORDERS-0.3.shp"
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
Now, open the world borders shapefile using GeoDjango's
|
2010-03-26 20:14:53 +00:00
|
|
|
:class:`~django.contrib.gis.gdal.DataSource` interface:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
2012-03-16 21:41:19 +00:00
|
|
|
>>> from django.contrib.gis.gdal import DataSource
|
2020-11-27 17:58:28 +00:00
|
|
|
>>> ds = DataSource(world_shp)
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(ds)
|
2010-03-26 20:14:53 +00:00
|
|
|
/ ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
Data source objects can have different layers of geospatial features; however,
|
2010-03-26 20:14:53 +00:00
|
|
|
shapefiles are only allowed to have one layer:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(len(ds))
|
2010-03-26 20:14:53 +00:00
|
|
|
1
|
|
|
|
>>> lyr = ds[0]
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(lyr)
|
2010-03-26 20:14:53 +00:00
|
|
|
TM_WORLD_BORDERS-0.3
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
You can see the layer's geometry type and how many features it contains:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
.. code-block:: pycon
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(lyr.geom_type)
|
2010-03-26 20:14:53 +00:00
|
|
|
Polygon
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(len(lyr))
|
2010-03-26 20:14:53 +00:00
|
|
|
246
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
Unfortunately, the shapefile data format does not allow for greater
|
2010-08-19 19:27:44 +00:00
|
|
|
specificity with regards to geometry types. This shapefile, like
|
2012-11-18 06:20:49 +00:00
|
|
|
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``.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
The :class:`~django.contrib.gis.gdal.Layer` may also have a spatial reference
|
2012-11-18 06:20:49 +00:00
|
|
|
system associated with it. If it does, the ``srs`` attribute will return a
|
2010-03-26 20:14:53 +00:00
|
|
|
:class:`~django.contrib.gis.gdal.SpatialReference` object:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> srs = lyr.srs
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(srs)
|
2021-09-15 07:15:39 +00:00
|
|
|
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"]]
|
2020-05-10 20:30:03 +00:00
|
|
|
>>> srs.proj # PROJ representation
|
2021-09-15 07:15:39 +00:00
|
|
|
'+proj=longlat +datum=WGS84 +no_defs'
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
This shapefile is in the popular WGS84 spatial reference
|
|
|
|
system -- in other words, the data uses longitude, latitude pairs in
|
|
|
|
units of degrees.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
In addition, shapefiles also support attribute fields that may contain
|
2010-03-26 20:14:53 +00:00
|
|
|
additional data. Here are the fields on the World Borders layer:
|
|
|
|
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(lyr.fields)
|
2010-03-26 20:14:53 +00:00
|
|
|
['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
The following code will let you examine the OGR types (e.g. integer or
|
|
|
|
string) associated with each of the fields:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
>>> [fld.__name__ for fld in lyr.field_types]
|
2021-09-15 07:15:39 +00:00
|
|
|
['OFTString', 'OFTString', 'OFTString', 'OFTInteger', 'OFTString', 'OFTInteger', 'OFTInteger64', 'OFTInteger', 'OFTInteger', 'OFTReal', 'OFTReal']
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
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
|
2010-03-26 20:14:53 +00:00
|
|
|
feature's attribute fields (whose **values** are accessed via ``get()``
|
|
|
|
method):
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> for feat in lyr:
|
2012-04-28 16:02:01 +00:00
|
|
|
... print(feat.get("NAME"), feat.geom.num_points)
|
2010-03-26 20:14:53 +00:00
|
|
|
...
|
|
|
|
Guernsey 18
|
|
|
|
Jersey 26
|
|
|
|
South Georgia South Sandwich Islands 338
|
|
|
|
Taiwan 363
|
|
|
|
|
|
|
|
:class:`~django.contrib.gis.gdal.Layer` objects may be sliced:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> 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:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> feat = lyr[234]
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(feat.get("NAME"))
|
2010-03-26 20:14:53 +00:00
|
|
|
San Marino
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
Boundary geometries may be exported as WKT and GeoJSON:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
.. code-block:: pycon
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
>>> geom = feat.geom
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(geom.wkt)
|
2010-03-26 20:14:53 +00:00
|
|
|
POLYGON ((12.415798 43.957954,12.450554 ...
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(geom.json)
|
2010-03-26 20:14:53 +00:00
|
|
|
{ "type": "Polygon", "coordinates": [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...
|
|
|
|
|
|
|
|
``LayerMapping``
|
|
|
|
----------------
|
|
|
|
|
2021-09-15 07:15:39 +00:00
|
|
|
To import the data, use a ``LayerMapping`` in a Python script.
|
2012-11-18 06:20:49 +00:00
|
|
|
Create a file called ``load.py`` inside the ``world`` application,
|
|
|
|
with the following code::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2019-11-07 10:11:27 +00:00
|
|
|
from pathlib import Path
|
2010-03-26 20:14:53 +00:00
|
|
|
from django.contrib.gis.utils import LayerMapping
|
2016-02-16 23:16:22 +00:00
|
|
|
from .models import WorldBorder
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2019-11-07 10:11:27 +00:00
|
|
|
world_shp = Path(__file__).resolve().parent / "data" / "TM_WORLD_BORDERS-0.3.shp"
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
def run(verbose=True):
|
2018-08-27 10:26:29 +00:00
|
|
|
lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False)
|
2010-03-26 20:14:53 +00:00
|
|
|
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
|
2012-11-18 06:20:49 +00:00
|
|
|
``WorldBorder`` model. The value is the name of the shapefile field
|
2010-08-19 19:27:44 +00:00
|
|
|
that data will be loaded from.
|
2010-03-26 20:14:53 +00:00
|
|
|
* The key ``mpoly`` for the geometry field is ``MULTIPOLYGON``, the
|
2012-11-18 06:20:49 +00:00
|
|
|
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.
|
2010-03-26 20:14:53 +00:00
|
|
|
* The path to the shapefile is not absolute -- in other words, if you move the
|
|
|
|
``world`` application (with ``data`` subdirectory) to a different location,
|
2012-11-18 06:20:49 +00:00
|
|
|
the script will still work.
|
2010-03-26 20:14:53 +00:00
|
|
|
* The ``transform`` keyword is set to ``False`` because the data in the
|
|
|
|
shapefile does not need to be converted -- it's already in WGS84 (SRID=4326).
|
|
|
|
|
2021-07-27 08:41:51 +00:00
|
|
|
Afterward, invoke the Django shell from the ``geodjango`` project directory:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2013-09-18 14:35:41 +00:00
|
|
|
$ python manage.py shell
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
Next, import the ``load`` module, call the ``run`` routine, and watch
|
|
|
|
``LayerMapping`` do the work:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
.. code-block:: pycon
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2013-09-18 14:35:41 +00:00
|
|
|
>>> from world import load
|
|
|
|
>>> load.run()
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
.. _ogrinspect-intro:
|
|
|
|
|
|
|
|
Try ``ogrinspect``
|
|
|
|
------------------
|
2010-08-19 19:27:44 +00:00
|
|
|
Now that you've seen how to define geographic models and import data with the
|
2014-11-26 17:46:06 +00:00
|
|
|
:doc:`layermapping`, it's possible to further automate this process with
|
2010-03-26 20:14:53 +00:00
|
|
|
use of the :djadmin:`ogrinspect` management command. The :djadmin:`ogrinspect`
|
2011-09-28 14:00:43 +00:00
|
|
|
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:
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ python manage.py ogrinspect [options] <data_source> <model_name> [options]
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
``data_source`` is the path to the GDAL-supported data source and
|
2010-03-26 20:14:53 +00:00
|
|
|
``model_name`` is the name to use for the model. Command-line options may
|
|
|
|
be used to further define how the model is generated.
|
|
|
|
|
2011-09-11 00:15:43 +00:00
|
|
|
For example, the following command nearly reproduces the ``WorldBorder`` model
|
2011-09-28 14:00:43 +00:00
|
|
|
and mapping dictionary created above, automatically:
|
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-02-11 01:48:45 +00:00
|
|
|
$ python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorder \
|
|
|
|
--srid=4326 --mapping --multi
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
A few notes about the command-line options given above:
|
|
|
|
|
|
|
|
* The ``--srid=4326`` option sets the SRID for the geographic field.
|
2010-08-19 19:27:44 +00:00
|
|
|
* The ``--mapping`` option tells ``ogrinspect`` to also generate a
|
2011-09-28 14:00:43 +00:00
|
|
|
mapping dictionary for use with
|
|
|
|
:class:`~django.contrib.gis.utils.LayerMapping`.
|
2010-03-26 20:14:53 +00:00
|
|
|
* 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`.
|
|
|
|
|
2010-08-19 19:27:44 +00:00
|
|
|
The command produces the following output, which may be copied
|
2010-03-26 20:14:53 +00:00
|
|
|
directly 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 models
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2011-09-11 00:15:43 +00:00
|
|
|
class WorldBorder(models.Model):
|
2010-03-26 20:14:53 +00:00
|
|
|
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)
|
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2011-09-11 00:15:43 +00:00
|
|
|
# Auto-generated `LayerMapping` dictionary for WorldBorder model
|
2010-03-26 20:14:53 +00:00
|
|
|
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 Queries
|
|
|
|
===============
|
|
|
|
|
|
|
|
Spatial Lookups
|
|
|
|
---------------
|
2012-11-18 06:20:49 +00:00
|
|
|
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:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ python manage.py shell
|
|
|
|
|
|
|
|
Now, define a point of interest [#]_:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> pnt_wkt = "POINT(-95.3385 29.7245)"
|
|
|
|
|
|
|
|
The ``pnt_wkt`` string represents the point at -95.3385 degrees longitude,
|
2012-11-18 06:20:49 +00:00
|
|
|
29.7245 degrees latitude. The geometry is in a format known as
|
|
|
|
Well Known Text (WKT), a standard issued by the Open Geospatial
|
2011-09-11 00:15:43 +00:00
|
|
|
Consortium (OGC). [#]_ Import the ``WorldBorder`` model, and perform
|
2010-03-26 20:14:53 +00:00
|
|
|
a ``contains`` lookup using the ``pnt_wkt`` as the parameter:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
2011-09-11 00:15:43 +00:00
|
|
|
>>> from world.models import WorldBorder
|
2015-10-05 23:07:34 +00:00
|
|
|
>>> WorldBorder.objects.filter(mpoly__contains=pnt_wkt)
|
|
|
|
<QuerySet [<WorldBorder: United States>]>
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2015-01-30 19:23:23 +00:00
|
|
|
Here, you retrieved a ``QuerySet`` with only one model: the border of the
|
|
|
|
United States (exactly what you would expect).
|
2012-11-18 06:20:49 +00:00
|
|
|
|
2014-11-26 17:46:06 +00:00
|
|
|
Similarly, you may also use a :doc:`GEOS geometry object <geos>`.
|
2012-11-18 06:20:49 +00:00
|
|
|
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:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
.. code-block:: pycon
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
>>> from django.contrib.gis.geos import Point
|
|
|
|
>>> pnt = Point(12.4604, 43.9420)
|
2015-10-05 23:07:34 +00:00
|
|
|
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
|
2011-09-11 00:15:43 +00:00
|
|
|
<WorldBorder: San Marino>
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
The ``contains`` and ``intersects`` lookups are just a subset of the
|
2014-11-26 17:46:06 +00:00
|
|
|
available queries -- the :doc:`db-api` documentation has more.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2017-06-26 11:42:31 +00:00
|
|
|
.. _automatic-spatial-transformations:
|
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
Automatic Spatial Transformations
|
|
|
|
---------------------------------
|
2012-11-18 06:20:49 +00:00
|
|
|
When doing spatial queries, GeoDjango automatically transforms
|
2010-03-26 20:14:53 +00:00
|
|
|
geometries if they're in a different coordinate system. In the following
|
2012-11-18 06:20:49 +00:00
|
|
|
example, coordinates will be expressed in `EPSG SRID 32140`__,
|
2010-03-26 20:14:53 +00:00
|
|
|
a coordinate system specific to south Texas **only** and in units of
|
2012-11-18 06:20:49 +00:00
|
|
|
**meters**, not degrees:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
.. code-block:: pycon
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2018-05-12 17:37:42 +00:00
|
|
|
>>> from django.contrib.gis.geos import GEOSGeometry, Point
|
2010-03-26 20:14:53 +00:00
|
|
|
>>> pnt = Point(954158.1, 4215137.1, srid=32140)
|
|
|
|
|
2011-12-17 02:00:20 +00:00
|
|
|
Note that ``pnt`` may also be constructed with EWKT, an "extended" form of
|
2010-03-26 20:14:53 +00:00
|
|
|
WKT that includes the SRID:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> pnt = GEOSGeometry("SRID=32140;POINT(954158.1 4215137.1)")
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
GeoDjango's ORM will automatically wrap geometry values
|
2010-03-26 20:14:53 +00:00
|
|
|
in transformation SQL, allowing the developer to work at a higher level
|
|
|
|
of abstraction:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
2011-09-11 00:15:43 +00:00
|
|
|
>>> qs = WorldBorder.objects.filter(mpoly__intersects=pnt)
|
2012-04-28 16:02:01 +00:00
|
|
|
>>> print(qs.query) # Generating the SQL
|
2012-02-11 01:48:45 +00:00
|
|
|
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))
|
2010-03-26 20:14:53 +00:00
|
|
|
>>> qs # printing evaluates the queryset
|
2015-10-05 23:07:34 +00:00
|
|
|
<QuerySet [<WorldBorder: United States>]>
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-01-29 08:34:37 +00:00
|
|
|
__ https://spatialreference.org/ref/epsg/32140/
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2017-11-01 14:12:01 +00:00
|
|
|
.. _gis-raw-sql:
|
|
|
|
|
2012-08-25 12:37:00 +00:00
|
|
|
.. admonition:: Raw queries
|
|
|
|
|
2017-11-01 14:12:01 +00:00
|
|
|
When using :doc:`raw queries </topics/db/sql>`, you must wrap your geometry
|
|
|
|
fields so that the field value can be recognized by GEOS:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2017-11-01 14:12:01 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
2023-12-28 18:52:15 +00:00
|
|
|
>>> 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")
|
|
|
|
... )
|
2012-08-25 12:37:00 +00:00
|
|
|
|
2017-11-01 14:12:01 +00:00
|
|
|
You should only use raw queries when you know exactly what you're doing.
|
2012-08-25 12:37:00 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
Lazy Geometries
|
|
|
|
---------------
|
2012-11-18 06:20:49 +00:00
|
|
|
GeoDjango loads geometries in a standardized textual representation. When the
|
2017-06-26 19:10:07 +00:00
|
|
|
geometry field is first accessed, GeoDjango creates a
|
|
|
|
:class:`~django.contrib.gis.geos.GEOSGeometry` object, exposing powerful
|
|
|
|
functionality, such as serialization properties for popular geospatial
|
|
|
|
formats:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2017-06-26 19:10:07 +00:00
|
|
|
.. code-block:: pycon
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2011-09-11 00:15:43 +00:00
|
|
|
>>> sm = WorldBorder.objects.get(name="San Marino")
|
2010-03-26 20:14:53 +00:00
|
|
|
>>> sm.mpoly
|
|
|
|
<MultiPolygon object at 0x24c6798>
|
2010-08-19 19:27:44 +00:00
|
|
|
>>> sm.mpoly.wkt # WKT
|
2010-03-26 20:14:53 +00:00
|
|
|
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>
|
2016-06-14 14:18:33 +00:00
|
|
|
>>> sm.mpoly.geojson # GeoJSON
|
2010-03-26 20:14:53 +00:00
|
|
|
'{ "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:
|
2023-02-09 15:48:46 +00:00
|
|
|
|
2010-03-26 20:14:53 +00:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> pnt = Point(12.4604, 43.9420)
|
|
|
|
>>> sm.mpoly.contains(pnt)
|
|
|
|
True
|
|
|
|
>>> pnt.contains(sm.mpoly)
|
|
|
|
False
|
|
|
|
|
2015-01-30 19:23:23 +00:00
|
|
|
Geographic annotations
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
GeoDjango also offers a set of geographic annotations to compute distances and
|
|
|
|
several other operations (intersection, difference, etc.). See the
|
|
|
|
:doc:`functions` documentation.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
Putting your data on the map
|
|
|
|
============================
|
|
|
|
|
|
|
|
Geographic Admin
|
|
|
|
----------------
|
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
:doc:`Django's admin application </ref/contrib/admin/index>` supports editing
|
|
|
|
geometry fields.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
Basics
|
2016-01-03 10:56:22 +00:00
|
|
|
~~~~~~
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
The Django admin allows users to create and modify geometries on a JavaScript
|
|
|
|
slippy map (powered by `OpenLayers`_).
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
Let's dive right in. Create a file called ``admin.py`` inside the ``world``
|
|
|
|
application with the following code::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
from django.contrib.gis import admin
|
2016-02-16 23:16:22 +00:00
|
|
|
from .models import WorldBorder
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
admin.site.register(WorldBorder, admin.ModelAdmin)
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
Next, edit your ``urls.py`` in the ``geodjango`` application folder as follows::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
from django.contrib import admin
|
2018-05-12 17:37:42 +00:00
|
|
|
from django.urls import include, path
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2014-04-02 00:46:34 +00:00
|
|
|
urlpatterns = [
|
2016-10-20 17:29:04 +00:00
|
|
|
path("admin/", admin.site.urls),
|
2014-04-02 00:46:34 +00:00
|
|
|
]
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2014-07-29 13:08:49 +00:00
|
|
|
Create an admin user:
|
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2014-07-29 13:08:49 +00:00
|
|
|
|
|
|
|
$ python manage.py createsuperuser
|
|
|
|
|
|
|
|
Next, start up the Django development server:
|
2011-09-28 14:00:43 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
$ python manage.py runserver
|
|
|
|
|
2014-07-29 13:08:49 +00:00
|
|
|
Finally, browse to ``http://localhost:8000/admin/``, and log in with the user
|
|
|
|
you just created. Browse to any of the ``WorldBorder`` entries -- the borders
|
2018-08-01 16:55:53 +00:00
|
|
|
may be edited by clicking on a polygon and dragging the vertices to the desired
|
2014-07-29 13:08:49 +00:00
|
|
|
position.
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2018-01-07 13:28:41 +00:00
|
|
|
.. _OpenLayers: https://openlayers.org/
|
2021-07-09 18:17:16 +00:00
|
|
|
.. _OpenStreetMap: https://www.openstreetmap.org/
|
2021-04-27 15:38:57 +00:00
|
|
|
.. _Vector Map Level 0: http://web.archive.org/web/20201024202709/https://earth-info.nga.mil/publications/vmap0.html
|
2018-09-26 06:48:47 +00:00
|
|
|
.. _OSGeo: https://www.osgeo.org/
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
``GISModelAdmin``
|
|
|
|
~~~~~~~~~~~~~~~~~
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
With the :class:`~django.contrib.gis.admin.GISModelAdmin`, GeoDjango uses an
|
|
|
|
`OpenStreetMap`_ layer in the admin.
|
2010-03-26 20:14:53 +00:00
|
|
|
This provides more context (including street and thoroughfare details) than
|
2020-12-09 17:12:56 +00:00
|
|
|
available with the :class:`~django.contrib.admin.ModelAdmin` (which uses the
|
|
|
|
`Vector Map Level 0`_ WMS dataset hosted at `OSGeo`_).
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-05-10 20:30:03 +00:00
|
|
|
The PROJ datum shifting files must be installed (see the :ref:`PROJ
|
2016-06-14 14:18:33 +00:00
|
|
|
installation instructions <proj4>` for more details).
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
If you meet this requirement, then use the ``GISModelAdmin`` option class
|
2019-06-17 14:54:55 +00:00
|
|
|
in your ``admin.py`` file::
|
2010-03-26 20:14:53 +00:00
|
|
|
|
2020-12-09 17:12:56 +00:00
|
|
|
admin.site.register(WorldBorder, admin.GISModelAdmin)
|
2010-03-26 20:14:53 +00:00
|
|
|
|
|
|
|
.. rubric:: Footnotes
|
|
|
|
|
2012-11-18 06:20:49 +00:00
|
|
|
.. [#] Special thanks to Bjørn Sandvik of `thematicmapping.org
|
2018-09-26 06:48:47 +00:00
|
|
|
<https://thematicmapping.org/>`_ for providing and maintaining this
|
2012-11-18 06:20:49 +00:00
|
|
|
dataset.
|
|
|
|
.. [#] GeoDjango basic apps was written by Dane Springmeyer, Josh Livni, and
|
|
|
|
Christopher Schmidt.
|
|
|
|
.. [#] This point is the `University of Houston Law Center
|
2015-11-29 16:29:46 +00:00
|
|
|
<https://www.law.uh.edu/>`_.
|
2012-11-18 06:20:49 +00:00
|
|
|
.. [#] Open Geospatial Consortium, Inc., `OpenGIS Simple Feature Specification
|
2021-04-27 11:09:00 +00:00
|
|
|
For SQL <https://www.ogc.org/standards/sfs>`_.
|