tutorial.txt 27 KB

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