1. ==================
    
  2. GeoDjango Tutorial
    
  3. ==================
    
  4. 
    
  5. Introduction
    
  6. ============
    
  7. 
    
  8. GeoDjango is an included contrib module for Django that turns it into a
    
  9. world-class geographic web framework. GeoDjango strives to make it as simple
    
  10. as possible to create geographic web applications, like location-based services.
    
  11. Its features include:
    
  12. 
    
  13. * Django model fields for `OGC`_ geometries and raster data.
    
  14. * Extensions to Django's ORM for querying and manipulating spatial data.
    
  15. * Loosely-coupled, high-level Python interfaces for GIS geometry and raster
    
  16.   operations and data manipulation in different formats.
    
  17. * Editing geometry fields from the admin.
    
  18. 
    
  19. This tutorial assumes familiarity with Django; thus, if you're brand new to
    
  20. Django, please read through the :doc:`regular tutorial </intro/tutorial01>` to
    
  21. familiarize yourself with Django first.
    
  22. 
    
  23. .. note::
    
  24. 
    
  25.     GeoDjango has additional requirements beyond what Django requires --
    
  26.     please consult the :doc:`installation documentation <install/index>`
    
  27.     for more details.
    
  28. 
    
  29. This tutorial will guide you through the creation of a geographic web
    
  30. application for viewing the `world borders`_. [#]_ Some of the code
    
  31. used in this tutorial is taken from and/or inspired by the `GeoDjango
    
  32. basic apps`_ project. [#]_
    
  33. 
    
  34. .. note::
    
  35. 
    
  36.     Proceed through the tutorial sections sequentially for step-by-step
    
  37.     instructions.
    
  38. 
    
  39. .. _OGC: https://www.ogc.org/
    
  40. .. _world borders: https://thematicmapping.org/downloads/world_borders.php
    
  41. .. _GeoDjango basic apps: https://code.google.com/archive/p/geodjango-basic-apps
    
  42. 
    
  43. Setting Up
    
  44. ==========
    
  45. 
    
  46. Create a Spatial Database
    
  47. -------------------------
    
  48. 
    
  49. Typically no special setup is required, so you can create a database as you
    
  50. would for any other project. We provide some tips for selected databases:
    
  51. 
    
  52. * :doc:`install/postgis`
    
  53. * :doc:`install/spatialite`
    
  54. 
    
  55. Create a New Project
    
  56. --------------------
    
  57. 
    
  58. Use the standard ``django-admin`` script to create a project called
    
  59. ``geodjango``:
    
  60. 
    
  61. .. console::
    
  62. 
    
  63.     $ django-admin startproject geodjango
    
  64. 
    
  65. This will initialize a new project. Now, create a ``world`` Django application
    
  66. within the ``geodjango`` project:
    
  67. 
    
  68. .. console::
    
  69. 
    
  70.     $ cd geodjango
    
  71.     $ python manage.py startapp world
    
  72. 
    
  73. Configure ``settings.py``
    
  74. -------------------------
    
  75. 
    
  76. The ``geodjango`` project settings are stored in the ``geodjango/settings.py``
    
  77. file. Edit the database connection settings to match your setup::
    
  78. 
    
  79.     DATABASES = {
    
  80.         'default': {
    
  81.             'ENGINE': 'django.contrib.gis.db.backends.postgis',
    
  82.             'NAME': 'geodjango',
    
  83.             'USER': 'geo',
    
  84.         },
    
  85.     }
    
  86. 
    
  87. In addition, modify the :setting:`INSTALLED_APPS` setting to include
    
  88. :mod:`django.contrib.admin`, :mod:`django.contrib.gis`,
    
  89. and ``world`` (your newly created application)::
    
  90. 
    
  91.     INSTALLED_APPS = [
    
  92.         'django.contrib.admin',
    
  93.         'django.contrib.auth',
    
  94.         'django.contrib.contenttypes',
    
  95.         'django.contrib.sessions',
    
  96.         'django.contrib.messages',
    
  97.         'django.contrib.staticfiles',
    
  98.         'django.contrib.gis',
    
  99.         'world',
    
  100.     ]
    
  101. 
    
  102. Geographic Data
    
  103. ===============
    
  104. 
    
  105. .. _worldborders:
    
  106. 
    
  107. World Borders
    
  108. -------------
    
  109. 
    
  110. The world borders data is available in this `zip file`__.  Create a ``data``
    
  111. directory in the ``world`` application, download the world borders data, and
    
  112. unzip. On GNU/Linux platforms, use the following commands:
    
  113. 
    
  114. .. console::
    
  115. 
    
  116.     $ mkdir world/data
    
  117.     $ cd world/data
    
  118.     $ wget https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
    
  119.     $ unzip TM_WORLD_BORDERS-0.3.zip
    
  120.     $ cd ../..
    
  121. 
    
  122. The world borders ZIP file contains a set of data files collectively known as
    
  123. an `ESRI Shapefile`__, one of the most popular geospatial data formats.  When
    
  124. unzipped, the world borders dataset includes files with the following
    
  125. extensions:
    
  126. 
    
  127. * ``.shp``: Holds the vector data for the world borders geometries.
    
  128. * ``.shx``: Spatial index file for geometries stored in the ``.shp``.
    
  129. * ``.dbf``: Database file for holding non-geometric attribute data
    
  130.   (e.g., integer and character fields).
    
  131. * ``.prj``: Contains the spatial reference information for the geographic
    
  132.   data stored in the shapefile.
    
  133. 
    
  134. __ https://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip
    
  135. __ https://en.wikipedia.org/wiki/Shapefile
    
  136. 
    
  137. Use ``ogrinfo`` to examine spatial data
    
  138. ---------------------------------------
    
  139. 
    
  140. The GDAL ``ogrinfo`` utility allows examining the metadata of shapefiles or
    
  141. other vector data sources:
    
  142. 
    
  143. .. console::
    
  144. 
    
  145.     $ ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp
    
  146.     INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'
    
  147.           using driver `ESRI Shapefile' successful.
    
  148.     1: TM_WORLD_BORDERS-0.3 (Polygon)
    
  149. 
    
  150. ``ogrinfo`` tells us that the shapefile has one layer, and that this
    
  151. layer contains polygon data.  To find out more, we'll specify the layer name
    
  152. and use the ``-so`` option to get only the important summary information:
    
  153. 
    
  154. .. console::
    
  155. 
    
  156.     $ ogrinfo -so world/data/TM_WORLD_BORDERS-0.3.shp TM_WORLD_BORDERS-0.3
    
  157.     INFO: Open of `world/data/TM_WORLD_BORDERS-0.3.shp'
    
  158.           using driver `ESRI Shapefile' successful.
    
  159. 
    
  160.     Layer name: TM_WORLD_BORDERS-0.3
    
  161.     Geometry: Polygon
    
  162.     Feature Count: 246
    
  163.     Extent: (-180.000000, -90.000000) - (180.000000, 83.623596)
    
  164.     Layer SRS WKT:
    
  165.     GEOGCS["GCS_WGS_1984",
    
  166.         DATUM["WGS_1984",
    
  167.             SPHEROID["WGS_1984",6378137.0,298.257223563]],
    
  168.         PRIMEM["Greenwich",0.0],
    
  169.         UNIT["Degree",0.0174532925199433]]
    
  170.     FIPS: String (2.0)
    
  171.     ISO2: String (2.0)
    
  172.     ISO3: String (3.0)
    
  173.     UN: Integer (3.0)
    
  174.     NAME: String (50.0)
    
  175.     AREA: Integer (7.0)
    
  176.     POP2005: Integer (10.0)
    
  177.     REGION: Integer (3.0)
    
  178.     SUBREGION: Integer (3.0)
    
  179.     LON: Real (8.3)
    
  180.     LAT: Real (7.3)
    
  181. 
    
  182. This detailed summary information tells us the number of features in the layer
    
  183. (246), the geographic bounds of the data, the spatial reference system
    
  184. ("SRS WKT"), as well as type information for each attribute field. For example,
    
  185. ``FIPS: String (2.0)`` indicates that the ``FIPS`` character field has
    
  186. a maximum length of 2.  Similarly, ``LON: Real (8.3)`` is a floating-point
    
  187. field that holds a maximum of 8 digits up to three decimal places.
    
  188. 
    
  189. Geographic Models
    
  190. =================
    
  191. 
    
  192. Defining a Geographic Model
    
  193. ---------------------------
    
  194. 
    
  195. Now that you've examined your dataset using ``ogrinfo``, create a GeoDjango
    
  196. model to represent this data::
    
  197. 
    
  198.     from django.contrib.gis.db import models
    
  199. 
    
  200.     class WorldBorder(models.Model):
    
  201.         # Regular Django fields corresponding to the attributes in the
    
  202.         # world borders shapefile.
    
  203.         name = models.CharField(max_length=50)
    
  204.         area = models.IntegerField()
    
  205.         pop2005 = models.IntegerField('Population 2005')
    
  206.         fips = models.CharField('FIPS Code', max_length=2, null=True)
    
  207.         iso2 = models.CharField('2 Digit ISO', max_length=2)
    
  208.         iso3 = models.CharField('3 Digit ISO', max_length=3)
    
  209.         un = models.IntegerField('United Nations Code')
    
  210.         region = models.IntegerField('Region Code')
    
  211.         subregion = models.IntegerField('Sub-Region Code')
    
  212.         lon = models.FloatField()
    
  213.         lat = models.FloatField()
    
  214. 
    
  215.         # GeoDjango-specific: a geometry field (MultiPolygonField)
    
  216.         mpoly = models.MultiPolygonField()
    
  217. 
    
  218.         # Returns the string representation of the model.
    
  219.         def __str__(self):
    
  220.             return self.name
    
  221. 
    
  222. Note that the ``models`` module is imported from ``django.contrib.gis.db``.
    
  223. 
    
  224. The default spatial reference system for geometry fields is WGS84 (meaning
    
  225. the `SRID`__ is 4326) -- in other words, the field coordinates are in
    
  226. longitude, latitude pairs in units of degrees.  To use a different
    
  227. coordinate system, set the SRID of the geometry field with the ``srid``
    
  228. argument. Use an integer representing the coordinate system's EPSG code.
    
  229. 
    
  230. __ https://en.wikipedia.org/wiki/SRID
    
  231. 
    
  232. Run ``migrate``
    
  233. ---------------
    
  234. 
    
  235. After defining your model, you need to sync it with the database. First,
    
  236. create a database migration:
    
  237. 
    
  238. .. console::
    
  239. 
    
  240.     $ python manage.py makemigrations
    
  241.     Migrations for 'world':
    
  242.       world/migrations/0001_initial.py:
    
  243.         - Create model WorldBorder
    
  244. 
    
  245. Let's look at the SQL that will generate the table for the ``WorldBorder``
    
  246. model:
    
  247. 
    
  248. .. console::
    
  249. 
    
  250.     $ python manage.py sqlmigrate world 0001
    
  251. 
    
  252. This command should produce the following output:
    
  253. 
    
  254. .. console::
    
  255. 
    
  256.     BEGIN;
    
  257.     --
    
  258.     -- Create model WorldBorder
    
  259.     --
    
  260.     CREATE TABLE "world_worldborder" (
    
  261.         "id" bigint NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
    
  262.         "name" varchar(50) NOT NULL,
    
  263.         "area" integer NOT NULL,
    
  264.         "pop2005" integer NOT NULL,
    
  265.         "fips" varchar(2) NOT NULL,
    
  266.         "iso2" varchar(2) NOT NULL,
    
  267.         "iso3" varchar(3) NOT NULL,
    
  268.         "un" integer NOT NULL,
    
  269.         "region" integer NOT NULL,
    
  270.         "subregion" integer NOT NULL,
    
  271.         "lon" double precision NOT NULL,
    
  272.         "lat" double precision NOT NULL
    
  273.         "mpoly" geometry(MULTIPOLYGON,4326) NOT NULL
    
  274.     )
    
  275.     ;
    
  276.     CREATE INDEX "world_worldborder_mpoly_id" ON "world_worldborder" USING GIST ("mpoly");
    
  277.     COMMIT;
    
  278. 
    
  279. If this looks correct, run :djadmin:`migrate` to create this table in the
    
  280. database:
    
  281. 
    
  282. .. console::
    
  283. 
    
  284.     $ python manage.py migrate
    
  285.     Operations to perform:
    
  286.       Apply all migrations: admin, auth, contenttypes, sessions, world
    
  287.     Running migrations:
    
  288.       ...
    
  289.       Applying world.0001_initial... OK
    
  290. 
    
  291. Importing Spatial Data
    
  292. ======================
    
  293. 
    
  294. This section will show you how to import the world borders shapefile into the
    
  295. database via GeoDjango models using the :doc:`layermapping`.
    
  296. 
    
  297. There are many different ways to import data into a spatial database --
    
  298. besides the tools included within GeoDjango, you may also use the following:
    
  299. 
    
  300. * `ogr2ogr`_: A command-line utility included with GDAL that
    
  301.   can import many vector data formats into PostGIS, MySQL, and Oracle databases.
    
  302. * `shp2pgsql`_: This utility included with PostGIS imports ESRI shapefiles into
    
  303.   PostGIS.
    
  304. 
    
  305. .. _ogr2ogr: https://gdal.org/programs/ogr2ogr.html
    
  306. .. _shp2pgsql: https://postgis.net/docs/using_postgis_dbmanagement.html#shp2pgsql_usage
    
  307. 
    
  308. .. _gdalinterface:
    
  309. 
    
  310. GDAL Interface
    
  311. --------------
    
  312. 
    
  313. Earlier, you used ``ogrinfo`` to examine the contents of the world borders
    
  314. shapefile.  GeoDjango also includes a Pythonic interface to GDAL's powerful OGR
    
  315. library that can work with all the vector data sources that OGR supports.
    
  316. 
    
  317. First, invoke the Django shell:
    
  318. 
    
  319. .. console::
    
  320. 
    
  321.     $ python manage.py shell
    
  322. 
    
  323. If you downloaded the :ref:`worldborders` data earlier in the tutorial, then
    
  324. you can determine its path using Python's :class:`pathlib.Path`::
    
  325. 
    
  326.     >>> from pathlib import Path
    
  327.     >>> import world
    
  328.     >>> world_shp = Path(world.__file__).resolve().parent / 'data' / 'TM_WORLD_BORDERS-0.3.shp'
    
  329. 
    
  330. Now, open the world borders shapefile using GeoDjango's
    
  331. :class:`~django.contrib.gis.gdal.DataSource` interface::
    
  332. 
    
  333.     >>> from django.contrib.gis.gdal import DataSource
    
  334.     >>> ds = DataSource(world_shp)
    
  335.     >>> print(ds)
    
  336.     / ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)
    
  337. 
    
  338. Data source objects can have different layers of geospatial features; however,
    
  339. shapefiles are only allowed to have one layer::
    
  340. 
    
  341.     >>> print(len(ds))
    
  342.     1
    
  343.     >>> lyr = ds[0]
    
  344.     >>> print(lyr)
    
  345.     TM_WORLD_BORDERS-0.3
    
  346. 
    
  347. You can see the layer's geometry type and how many features it contains::
    
  348. 
    
  349.     >>> print(lyr.geom_type)
    
  350.     Polygon
    
  351.     >>> print(len(lyr))
    
  352.     246
    
  353. 
    
  354. .. note::
    
  355. 
    
  356.     Unfortunately, the shapefile data format does not allow for greater
    
  357.     specificity with regards to geometry types.  This shapefile, like
    
  358.     many others, actually includes ``MultiPolygon`` geometries, not Polygons.
    
  359.     It's important to use a more general field type in models: a
    
  360.     GeoDjango ``MultiPolygonField`` will accept a ``Polygon`` geometry, but a
    
  361.     ``PolygonField`` will not accept a ``MultiPolygon`` type geometry.  This
    
  362.     is why the ``WorldBorder`` model defined above uses a ``MultiPolygonField``.
    
  363. 
    
  364. The :class:`~django.contrib.gis.gdal.Layer` may also have a spatial reference
    
  365. system associated with it.  If it does, the ``srs`` attribute will return a
    
  366. :class:`~django.contrib.gis.gdal.SpatialReference` object::
    
  367. 
    
  368.     >>> srs = lyr.srs
    
  369.     >>> print(srs)
    
  370.     GEOGCS["WGS 84",
    
  371.     DATUM["WGS_1984",
    
  372.         SPHEROID["WGS 84",6378137,298.257223563,
    
  373.             AUTHORITY["EPSG","7030"]],
    
  374.         AUTHORITY["EPSG","6326"]],
    
  375.     PRIMEM["Greenwich",0,
    
  376.         AUTHORITY["EPSG","8901"]],
    
  377.     UNIT["degree",0.0174532925199433,
    
  378.         AUTHORITY["EPSG","9122"]],
    
  379.     AXIS["Latitude",NORTH],
    
  380.     AXIS["Longitude",EAST],
    
  381.     AUTHORITY["EPSG","4326"]]
    
  382.     >>> srs.proj # PROJ representation
    
  383.     '+proj=longlat +datum=WGS84 +no_defs'
    
  384. 
    
  385. This shapefile is in the popular WGS84 spatial reference
    
  386. system -- in other words, the data uses longitude, latitude pairs in
    
  387. units of degrees.
    
  388. 
    
  389. In addition, shapefiles also support attribute fields that may contain
    
  390. additional data.  Here are the fields on the World Borders layer:
    
  391. 
    
  392.     >>> print(lyr.fields)
    
  393.     ['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']
    
  394. 
    
  395. The following code will let you examine the OGR types (e.g. integer or
    
  396. string) associated with each of the fields:
    
  397. 
    
  398.     >>> [fld.__name__ for fld in lyr.field_types]
    
  399.     ['OFTString', 'OFTString', 'OFTString', 'OFTInteger', 'OFTString', 'OFTInteger', 'OFTInteger64', 'OFTInteger', 'OFTInteger', 'OFTReal', 'OFTReal']
    
  400. 
    
  401. You can iterate over each feature in the layer and extract information from both
    
  402. the feature's geometry (accessed via the ``geom`` attribute) as well as the
    
  403. feature's attribute fields (whose **values** are accessed via ``get()``
    
  404. method)::
    
  405. 
    
  406.     >>> for feat in lyr:
    
  407.     ...    print(feat.get('NAME'), feat.geom.num_points)
    
  408.     ...
    
  409.     Guernsey 18
    
  410.     Jersey 26
    
  411.     South Georgia South Sandwich Islands 338
    
  412.     Taiwan 363
    
  413. 
    
  414. :class:`~django.contrib.gis.gdal.Layer` objects may be sliced::
    
  415. 
    
  416.     >>> lyr[0:2]
    
  417.     [<django.contrib.gis.gdal.feature.Feature object at 0x2f47690>, <django.contrib.gis.gdal.feature.Feature object at 0x2f47650>]
    
  418. 
    
  419. And individual features may be retrieved by their feature ID::
    
  420. 
    
  421.     >>> feat = lyr[234]
    
  422.     >>> print(feat.get('NAME'))
    
  423.     San Marino
    
  424. 
    
  425. Boundary geometries may be exported as WKT and GeoJSON::
    
  426. 
    
  427.     >>> geom = feat.geom
    
  428.     >>> print(geom.wkt)
    
  429.     POLYGON ((12.415798 43.957954,12.450554 ...
    
  430.     >>> print(geom.json)
    
  431.     { "type": "Polygon", "coordinates": [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...
    
  432. 
    
  433. ``LayerMapping``
    
  434. ----------------
    
  435. 
    
  436. To import the data, use a ``LayerMapping`` in a Python script.
    
  437. Create a file called ``load.py`` inside the ``world`` application,
    
  438. with the following code::
    
  439. 
    
  440.     from pathlib import Path
    
  441.     from django.contrib.gis.utils import LayerMapping
    
  442.     from .models import WorldBorder
    
  443. 
    
  444.     world_mapping = {
    
  445.         'fips' : 'FIPS',
    
  446.         'iso2' : 'ISO2',
    
  447.         'iso3' : 'ISO3',
    
  448.         'un' : 'UN',
    
  449.         'name' : 'NAME',
    
  450.         'area' : 'AREA',
    
  451.         'pop2005' : 'POP2005',
    
  452.         'region' : 'REGION',
    
  453.         'subregion' : 'SUBREGION',
    
  454.         'lon' : 'LON',
    
  455.         'lat' : 'LAT',
    
  456.         'mpoly' : 'MULTIPOLYGON',
    
  457.     }
    
  458. 
    
  459.     world_shp = Path(__file__).resolve().parent / 'data' / 'TM_WORLD_BORDERS-0.3.shp'
    
  460. 
    
  461.     def run(verbose=True):
    
  462.         lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False)
    
  463.         lm.save(strict=True, verbose=verbose)
    
  464. 
    
  465. A few notes about what's going on:
    
  466. 
    
  467. * Each key in the ``world_mapping`` dictionary corresponds to a field in the
    
  468.   ``WorldBorder`` model.  The value is the name of the shapefile field
    
  469.   that data will be loaded from.
    
  470. * The key ``mpoly`` for the geometry field is ``MULTIPOLYGON``, the
    
  471.   geometry type GeoDjango will import the field as.  Even simple polygons in
    
  472.   the shapefile will automatically be converted into collections prior to
    
  473.   insertion into the database.
    
  474. * The path to the shapefile is not absolute -- in other words, if you move the
    
  475.   ``world`` application (with ``data`` subdirectory) to a different location,
    
  476.   the script will still work.
    
  477. * The ``transform`` keyword is set to ``False`` because the data in the
    
  478.   shapefile does not need to be converted -- it's already in WGS84 (SRID=4326).
    
  479. 
    
  480. Afterward, invoke the Django shell from the ``geodjango`` project directory:
    
  481. 
    
  482. .. console::
    
  483. 
    
  484.     $ python manage.py shell
    
  485. 
    
  486. Next, import the ``load`` module, call the ``run`` routine, and watch
    
  487. ``LayerMapping`` do the work::
    
  488. 
    
  489.     >>> from world import load
    
  490.     >>> load.run()
    
  491. 
    
  492. .. _ogrinspect-intro:
    
  493. 
    
  494. Try ``ogrinspect``
    
  495. ------------------
    
  496. Now that you've seen how to define geographic models and import data with the
    
  497. :doc:`layermapping`, it's possible to further automate this process with
    
  498. use of the :djadmin:`ogrinspect` management command.  The :djadmin:`ogrinspect`
    
  499. command  introspects a GDAL-supported vector data source (e.g., a shapefile)
    
  500. and generates a model definition and ``LayerMapping`` dictionary automatically.
    
  501. 
    
  502. The general usage of the command goes as follows:
    
  503. 
    
  504. .. console::
    
  505. 
    
  506.     $ python manage.py ogrinspect [options] <data_source> <model_name> [options]
    
  507. 
    
  508. ``data_source`` is the path to the GDAL-supported data source and
    
  509. ``model_name`` is the name to use for the model.  Command-line options may
    
  510. be used to further define how the model is generated.
    
  511. 
    
  512. For example, the following command nearly reproduces the ``WorldBorder`` model
    
  513. and mapping dictionary created above, automatically:
    
  514. 
    
  515. .. console::
    
  516. 
    
  517.     $ python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorder \
    
  518.         --srid=4326 --mapping --multi
    
  519. 
    
  520. A few notes about the command-line options given above:
    
  521. 
    
  522. * The ``--srid=4326`` option sets the SRID for the geographic field.
    
  523. * The ``--mapping`` option tells ``ogrinspect`` to also generate a
    
  524.   mapping dictionary for use with
    
  525.   :class:`~django.contrib.gis.utils.LayerMapping`.
    
  526. * The ``--multi`` option is specified so that the geographic field is a
    
  527.   :class:`~django.contrib.gis.db.models.MultiPolygonField` instead of just a
    
  528.   :class:`~django.contrib.gis.db.models.PolygonField`.
    
  529. 
    
  530. The command produces the following output, which may be copied
    
  531. directly into the ``models.py`` of a GeoDjango application::
    
  532. 
    
  533.     # This is an auto-generated Django model module created by ogrinspect.
    
  534.     from django.contrib.gis.db import models
    
  535. 
    
  536.     class WorldBorder(models.Model):
    
  537.         fips = models.CharField(max_length=2)
    
  538.         iso2 = models.CharField(max_length=2)
    
  539.         iso3 = models.CharField(max_length=3)
    
  540.         un = models.IntegerField()
    
  541.         name = models.CharField(max_length=50)
    
  542.         area = models.IntegerField()
    
  543.         pop2005 = models.IntegerField()
    
  544.         region = models.IntegerField()
    
  545.         subregion = models.IntegerField()
    
  546.         lon = models.FloatField()
    
  547.         lat = models.FloatField()
    
  548.         geom = models.MultiPolygonField(srid=4326)
    
  549. 
    
  550.     # Auto-generated `LayerMapping` dictionary for WorldBorder model
    
  551.     worldborders_mapping = {
    
  552.         'fips' : 'FIPS',
    
  553.         'iso2' : 'ISO2',
    
  554.         'iso3' : 'ISO3',
    
  555.         'un' : 'UN',
    
  556.         'name' : 'NAME',
    
  557.         'area' : 'AREA',
    
  558.         'pop2005' : 'POP2005',
    
  559.         'region' : 'REGION',
    
  560.         'subregion' : 'SUBREGION',
    
  561.         'lon' : 'LON',
    
  562.         'lat' : 'LAT',
    
  563.         'geom' : 'MULTIPOLYGON',
    
  564.     }
    
  565. 
    
  566. Spatial Queries
    
  567. ===============
    
  568. 
    
  569. Spatial Lookups
    
  570. ---------------
    
  571. GeoDjango adds spatial lookups to the Django ORM.  For example, you
    
  572. can find the country in the ``WorldBorder`` table that contains
    
  573. a particular point.  First, fire up the management shell:
    
  574. 
    
  575. .. console::
    
  576. 
    
  577.     $ python manage.py shell
    
  578. 
    
  579. Now, define a point of interest [#]_::
    
  580. 
    
  581.     >>> pnt_wkt = 'POINT(-95.3385 29.7245)'
    
  582. 
    
  583. The ``pnt_wkt`` string represents the point at -95.3385 degrees longitude,
    
  584. 29.7245 degrees latitude.  The geometry is in a format known as
    
  585. Well Known Text (WKT), a standard issued by the Open Geospatial
    
  586. Consortium (OGC). [#]_  Import the ``WorldBorder`` model, and perform
    
  587. a ``contains`` lookup using the ``pnt_wkt`` as the parameter::
    
  588. 
    
  589.     >>> from world.models import WorldBorder
    
  590.     >>> WorldBorder.objects.filter(mpoly__contains=pnt_wkt)
    
  591.     <QuerySet [<WorldBorder: United States>]>
    
  592. 
    
  593. Here, you retrieved a ``QuerySet`` with only one model: the border of the
    
  594. United States (exactly what you would expect).
    
  595. 
    
  596. Similarly, you may also use a :doc:`GEOS geometry object <geos>`.
    
  597. Here, you can combine the ``intersects`` spatial lookup with the ``get``
    
  598. method to retrieve only the ``WorldBorder`` instance for San Marino instead
    
  599. of a queryset::
    
  600. 
    
  601.     >>> from django.contrib.gis.geos import Point
    
  602.     >>> pnt = Point(12.4604, 43.9420)
    
  603.     >>> WorldBorder.objects.get(mpoly__intersects=pnt)
    
  604.     <WorldBorder: San Marino>
    
  605. 
    
  606. The ``contains`` and ``intersects`` lookups are just a subset of the
    
  607. available queries -- the :doc:`db-api` documentation has more.
    
  608. 
    
  609. .. _automatic-spatial-transformations:
    
  610. 
    
  611. Automatic Spatial Transformations
    
  612. ---------------------------------
    
  613. When doing spatial queries, GeoDjango automatically transforms
    
  614. geometries if they're in a different coordinate system.  In the following
    
  615. example, coordinates will be expressed in `EPSG SRID 32140`__,
    
  616. a coordinate system specific to south Texas **only** and in units of
    
  617. **meters**, not degrees::
    
  618. 
    
  619.     >>> from django.contrib.gis.geos import GEOSGeometry, Point
    
  620.     >>> pnt = Point(954158.1, 4215137.1, srid=32140)
    
  621. 
    
  622. Note that ``pnt`` may also be constructed with EWKT, an "extended" form of
    
  623. WKT that includes the SRID::
    
  624. 
    
  625.     >>> pnt = GEOSGeometry('SRID=32140;POINT(954158.1 4215137.1)')
    
  626. 
    
  627. GeoDjango's ORM will automatically wrap geometry values
    
  628. in transformation SQL, allowing the developer to work at a higher level
    
  629. of abstraction::
    
  630. 
    
  631.     >>> qs = WorldBorder.objects.filter(mpoly__intersects=pnt)
    
  632.     >>> print(qs.query) # Generating the SQL
    
  633.     SELECT "world_worldborder"."id", "world_worldborder"."name", "world_worldborder"."area",
    
  634.     "world_worldborder"."pop2005", "world_worldborder"."fips", "world_worldborder"."iso2",
    
  635.     "world_worldborder"."iso3", "world_worldborder"."un", "world_worldborder"."region",
    
  636.     "world_worldborder"."subregion", "world_worldborder"."lon", "world_worldborder"."lat",
    
  637.     "world_worldborder"."mpoly" FROM "world_worldborder"
    
  638.     WHERE ST_Intersects("world_worldborder"."mpoly", ST_Transform(%s, 4326))
    
  639.     >>> qs # printing evaluates the queryset
    
  640.     <QuerySet [<WorldBorder: United States>]>
    
  641. 
    
  642. __ https://spatialreference.org/ref/epsg/32140/
    
  643. 
    
  644. .. _gis-raw-sql:
    
  645. 
    
  646. .. admonition:: Raw queries
    
  647. 
    
  648.     When using :doc:`raw queries </topics/db/sql>`, you must wrap your geometry
    
  649.     fields so that the field value can be recognized by GEOS::
    
  650. 
    
  651.         from django.db import connection
    
  652.         # or if you're querying a non-default database:
    
  653.         from django.db import connections
    
  654.         connection = connections['your_gis_db_alias']
    
  655. 
    
  656.         City.objects.raw('SELECT id, name, %s as point from myapp_city' % (connection.ops.select % 'point'))
    
  657. 
    
  658.     You should only use raw queries when you know exactly what you're doing.
    
  659. 
    
  660. Lazy Geometries
    
  661. ---------------
    
  662. GeoDjango loads geometries in a standardized textual representation.  When the
    
  663. geometry field is first accessed, GeoDjango creates a
    
  664. :class:`~django.contrib.gis.geos.GEOSGeometry` object, exposing powerful
    
  665. functionality, such as serialization properties for popular geospatial
    
  666. formats::
    
  667. 
    
  668.     >>> sm = WorldBorder.objects.get(name='San Marino')
    
  669.     >>> sm.mpoly
    
  670.     <MultiPolygon object at 0x24c6798>
    
  671.     >>> sm.mpoly.wkt # WKT
    
  672.     MULTIPOLYGON (((12.4157980000000006 43.9579540000000009, 12.4505540000000003 43.9797209999999978, ...
    
  673.     >>> sm.mpoly.wkb # WKB (as Python binary buffer)
    
  674.     <read-only buffer for 0x1fe2c70, size -1, offset 0 at 0x2564c40>
    
  675.     >>> sm.mpoly.geojson # GeoJSON
    
  676.     '{ "type": "MultiPolygon", "coordinates": [ [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...
    
  677. 
    
  678. This includes access to all of the advanced geometric operations provided by
    
  679. the GEOS library::
    
  680. 
    
  681.     >>> pnt = Point(12.4604, 43.9420)
    
  682.     >>> sm.mpoly.contains(pnt)
    
  683.     True
    
  684.     >>> pnt.contains(sm.mpoly)
    
  685.     False
    
  686. 
    
  687. Geographic annotations
    
  688. ----------------------
    
  689. 
    
  690. GeoDjango also offers a set of geographic annotations to compute distances and
    
  691. several other operations (intersection, difference, etc.). See the
    
  692. :doc:`functions` documentation.
    
  693. 
    
  694. Putting your data on the map
    
  695. ============================
    
  696. 
    
  697. Geographic Admin
    
  698. ----------------
    
  699. 
    
  700. :doc:`Django's admin application </ref/contrib/admin/index>` supports editing
    
  701. geometry fields.
    
  702. 
    
  703. Basics
    
  704. ~~~~~~
    
  705. 
    
  706. The Django admin allows users to create and modify geometries on a JavaScript
    
  707. slippy map (powered by `OpenLayers`_).
    
  708. 
    
  709. Let's dive right in. Create a file called ``admin.py`` inside the ``world``
    
  710. application with the following code::
    
  711. 
    
  712.     from django.contrib.gis import admin
    
  713.     from .models import WorldBorder
    
  714. 
    
  715.     admin.site.register(WorldBorder, admin.ModelAdmin)
    
  716. 
    
  717. Next, edit your ``urls.py`` in the ``geodjango`` application folder as follows::
    
  718. 
    
  719.     from django.contrib import admin
    
  720.     from django.urls import include, path
    
  721. 
    
  722.     urlpatterns = [
    
  723.         path('admin/', admin.site.urls),
    
  724.     ]
    
  725. 
    
  726. Create an admin user:
    
  727. 
    
  728. .. console::
    
  729. 
    
  730.     $ python manage.py createsuperuser
    
  731. 
    
  732. Next, start up the Django development server:
    
  733. 
    
  734. .. console::
    
  735. 
    
  736.     $ python manage.py runserver
    
  737. 
    
  738. Finally, browse to ``http://localhost:8000/admin/``, and log in with the user
    
  739. you just created. Browse to any of the ``WorldBorder`` entries -- the borders
    
  740. may be edited by clicking on a polygon and dragging the vertices to the desired
    
  741. position.
    
  742. 
    
  743. .. _OpenLayers: https://openlayers.org/
    
  744. .. _OpenStreetMap: https://www.openstreetmap.org/
    
  745. .. _Vector Map Level 0: http://web.archive.org/web/20201024202709/https://earth-info.nga.mil/publications/vmap0.html
    
  746. .. _OSGeo: https://www.osgeo.org/
    
  747. 
    
  748. ``GISModelAdmin``
    
  749. ~~~~~~~~~~~~~~~~~
    
  750. 
    
  751. With the :class:`~django.contrib.gis.admin.GISModelAdmin`, GeoDjango uses an
    
  752. `OpenStreetMap`_ layer in the admin.
    
  753. This provides more context (including street and thoroughfare details) than
    
  754. available with the :class:`~django.contrib.admin.ModelAdmin` (which uses the
    
  755. `Vector Map Level 0`_ WMS dataset hosted at `OSGeo`_).
    
  756. 
    
  757. The PROJ datum shifting files must be installed (see the :ref:`PROJ
    
  758. installation instructions <proj4>` for more details).
    
  759. 
    
  760. If you meet this requirement, then use the ``GISModelAdmin`` option class
    
  761. in your ``admin.py`` file::
    
  762. 
    
  763.     admin.site.register(WorldBorder, admin.GISModelAdmin)
    
  764. 
    
  765. .. rubric:: Footnotes
    
  766. 
    
  767. .. [#] Special thanks to Bjørn Sandvik of `thematicmapping.org
    
  768.        <https://thematicmapping.org/>`_ for providing and maintaining this
    
  769.        dataset.
    
  770. .. [#] GeoDjango basic apps was written by Dane Springmeyer, Josh Livni, and
    
  771.        Christopher Schmidt.
    
  772. .. [#] This point is the `University of Houston Law Center
    
  773.        <https://www.law.uh.edu/>`_.
    
  774. .. [#] Open Geospatial Consortium, Inc., `OpenGIS Simple Feature Specification
    
  775.        For SQL <https://www.ogc.org/standards/sfs>`_.