tutorial.txt 27 KB

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