tutorial.txt 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 at 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 :ref:`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 is going to guide you through guide the user through the creation
  22. of a geographic web application for viewing the `world borders`_. [#]_ Some of
  23. the code used in this tutorial is taken from and/or inspired by the
  24. `GeoDjango 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`_ website, 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 WorldBorders(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. # So the model is pluralized correctly in the admin.
  181. class Meta:
  182. verbose_name_plural = "World Borders"
  183. # Returns the string representation of the model.
  184. def __unicode__(self):
  185. return self.name
  186. Two important things to note:
  187. 1. The ``models`` module is imported from :mod:`django.contrib.gis.db`.
  188. 2. The model overrides its default manager with
  189. :class:`~django.contrib.gis.db.models.GeoManager`; this is *required*
  190. to perform spatial queries.
  191. When declaring a geometry field on your model the default spatial reference system
  192. is WGS84 (meaning the `SRID`__ is 4326) -- in other words, the field coordinates are in
  193. longitude/latitude pairs in units of degrees. If you want the coordinate system to be
  194. different, then SRID of the geometry field may be customized by setting the ``srid``
  195. with an integer corresponding to the coordinate system of your choice.
  196. __ http://en.wikipedia.org/wiki/SRID
  197. Run ``syncdb``
  198. --------------
  199. After you've defined your model, it needs to be synced with the spatial database.
  200. First, let's look at the SQL that will generate the table for the ``WorldBorders``
  201. model::
  202. $ python manage.py sqlall world
  203. This management command should produce the following output::
  204. BEGIN;
  205. CREATE TABLE "world_worldborders" (
  206. "id" serial NOT NULL PRIMARY KEY,
  207. "name" varchar(50) NOT NULL,
  208. "area" integer NOT NULL,
  209. "pop2005" integer NOT NULL,
  210. "fips" varchar(2) NOT NULL,
  211. "iso2" varchar(2) NOT NULL,
  212. "iso3" varchar(3) NOT NULL,
  213. "un" integer NOT NULL,
  214. "region" integer NOT NULL,
  215. "subregion" integer NOT NULL,
  216. "lon" double precision NOT NULL,
  217. "lat" double precision NOT NULL
  218. )
  219. ;
  220. SELECT AddGeometryColumn('world_worldborders', 'mpoly', 4326, 'MULTIPOLYGON', 2);
  221. ALTER TABLE "world_worldborders" ALTER "mpoly" SET NOT NULL;
  222. CREATE INDEX "world_worldborders_mpoly_id" ON "world_worldborders" USING GIST ( "mpoly" GIST_GEOMETRY_OPS );
  223. COMMIT;
  224. If satisfied, you may then create this table in the database by running the
  225. ``syncdb`` management command::
  226. $ python manage.py syncdb
  227. Creating table world_worldborders
  228. Installing custom SQL for world.WorldBorders model
  229. The ``syncdb`` command may also prompt you to create an admin user; go ahead and
  230. do so (not required now, may be done at any point in the future using the
  231. ``createsuperuser`` management command).
  232. Importing Spatial Data
  233. ======================
  234. This section will show you how to take the data from the world borders
  235. shapefile and import it into GeoDjango models using the :ref:`ref-layermapping`.
  236. There are many different different ways to import data in to a
  237. spatial database -- besides the tools included within GeoDjango, you
  238. may also use the following to populate your spatial database:
  239. * `ogr2ogr`_: Command-line utility, included with GDAL, that
  240. supports loading a multitude of vector data formats into
  241. the PostGIS, MySQL, and Oracle spatial databases.
  242. * `shp2pgsql`_: This utility is included with PostGIS and only supports
  243. ESRI shapefiles.
  244. .. _ogr2ogr: http://www.gdal.org/ogr2ogr.html
  245. .. _shp2pgsql: http://postgis.refractions.net/documentation/manual-1.5/ch04.html#shp2pgsql_usage
  246. .. _gdalinterface:
  247. GDAL Interface
  248. --------------
  249. Earlier we used the the ``ogrinfo`` to explore the contents of the world borders
  250. shapefile. Included within GeoDjango is an interface to GDAL's powerful OGR
  251. library -- in other words, you'll be able explore all the vector data sources
  252. that OGR supports via a Pythonic API.
  253. First, invoke the Django shell::
  254. $ python manage.py shell
  255. If the :ref:`worldborders` data was downloaded like earlier in the
  256. tutorial, then we can determine the path using Python's built-in
  257. ``os`` module::
  258. >>> import os
  259. >>> from geodjango import world
  260. >>> world_shp = os.path.abspath(os.path.join(os.path.dirname(world.__file__),
  261. ... 'data/TM_WORLD_BORDERS-0.3.shp'))
  262. Now, the world borders shapefile may be opened using GeoDjango's
  263. :class:`~django.contrib.gis.gdal.DataSource` interface::
  264. >>> from django.contrib.gis.gdal import *
  265. >>> ds = DataSource(world_shp)
  266. >>> print ds
  267. / ... /geodjango/world/data/TM_WORLD_BORDERS-0.3.shp (ESRI Shapefile)
  268. Data source objects can have different layers of geospatial features; however,
  269. shapefiles are only allowed to have one layer::
  270. >>> print len(ds)
  271. 1
  272. >>> lyr = ds[0]
  273. >>> print lyr
  274. TM_WORLD_BORDERS-0.3
  275. You can see what the geometry type of the layer is and how many features it
  276. contains::
  277. >>> print lyr.geom_type
  278. Polygon
  279. >>> print len(lyr)
  280. 246
  281. .. note::
  282. Unfortunately the shapefile data format does not allow for greater
  283. specificity with regards to geometry types. This shapefile, like
  284. many others, actually includes ``MultiPolygon`` geometries in its
  285. features. You need to watch out for this when creating your models
  286. as a GeoDjango ``PolygonField`` will not accept a ``MultiPolygon``
  287. type geometry -- thus a ``MultiPolygonField`` is used in our model's
  288. definition instead.
  289. The :class:`~django.contrib.gis.gdal.Layer` may also have a spatial reference
  290. system associated with it -- if it does, the ``srs`` attribute will return a
  291. :class:`~django.contrib.gis.gdal.SpatialReference` object::
  292. >>> srs = lyr.srs
  293. >>> print srs
  294. GEOGCS["GCS_WGS_1984",
  295. DATUM["WGS_1984",
  296. SPHEROID["WGS_1984",6378137.0,298.257223563]],
  297. PRIMEM["Greenwich",0.0],
  298. UNIT["Degree",0.0174532925199433]]
  299. >>> srs.proj4 # PROJ.4 representation
  300. '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs '
  301. Here we've noticed that the shapefile is in the popular WGS84 spatial reference
  302. system -- in other words, the data uses units of degrees longitude and latitude.
  303. In addition, shapefiles also support attribute fields that may contain
  304. additional data. Here are the fields on the World Borders layer:
  305. >>> print lyr.fields
  306. ['FIPS', 'ISO2', 'ISO3', 'UN', 'NAME', 'AREA', 'POP2005', 'REGION', 'SUBREGION', 'LON', 'LAT']
  307. Here we are examining the OGR types (e.g., whether a field is an integer or
  308. a string) associated with each of the fields:
  309. >>> [fld.__name__ for fld in lyr.field_types]
  310. ['OFTString', 'OFTString', 'OFTString', 'OFTInteger', 'OFTString', 'OFTInteger', 'OFTInteger', 'OFTInteger', 'OFTInteger', 'OFTReal', 'OFTReal']
  311. You can iterate over each feature in the layer and extract information from both
  312. the feature's geometry (accessed via the ``geom`` attribute) as well as the
  313. feature's attribute fields (whose **values** are accessed via ``get()``
  314. method)::
  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. >>> lyr[0:2]
  324. [<django.contrib.gis.gdal.feature.Feature object at 0x2f47690>, <django.contrib.gis.gdal.feature.Feature object at 0x2f47650>]
  325. And individual features may be retrieved by their feature ID::
  326. >>> feat = lyr[234]
  327. >>> print feat.get('NAME')
  328. San Marino
  329. Here the boundary geometry for San Marino is extracted and looking
  330. exported to WKT and GeoJSON::
  331. >>> geom = feat.geom
  332. >>> print geom.wkt
  333. POLYGON ((12.415798 43.957954,12.450554 ...
  334. >>> print geom.json
  335. { "type": "Polygon", "coordinates": [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...
  336. ``LayerMapping``
  337. ----------------
  338. We're going to dive right in -- create a file called ``load.py`` inside the
  339. ``world`` application, and insert the following::
  340. import os
  341. from django.contrib.gis.utils import LayerMapping
  342. from models import WorldBorders
  343. world_mapping = {
  344. 'fips' : 'FIPS',
  345. 'iso2' : 'ISO2',
  346. 'iso3' : 'ISO3',
  347. 'un' : 'UN',
  348. 'name' : 'NAME',
  349. 'area' : 'AREA',
  350. 'pop2005' : 'POP2005',
  351. 'region' : 'REGION',
  352. 'subregion' : 'SUBREGION',
  353. 'lon' : 'LON',
  354. 'lat' : 'LAT',
  355. 'mpoly' : 'MULTIPOLYGON',
  356. }
  357. world_shp = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/TM_WORLD_BORDERS-0.3.shp'))
  358. def run(verbose=True):
  359. lm = LayerMapping(WorldBorders, world_shp, world_mapping,
  360. transform=False, encoding='iso-8859-1')
  361. lm.save(strict=True, verbose=verbose)
  362. A few notes about what's going on:
  363. * Each key in the ``world_mapping`` dictionary corresponds to a field in the
  364. ``WorldBorders`` model, and the value is the name of the shapefile field
  365. that data will be loaded from.
  366. * The key ``mpoly`` for the geometry field is ``MULTIPOLYGON``, the
  367. geometry type we wish to import as. Even if simple polygons are encountered
  368. in the shapefile they will automatically be converted into collections prior
  369. to insertion into the database.
  370. * The path to the shapefile is not absolute -- in other words, if you move the
  371. ``world`` application (with ``data`` subdirectory) to a different location,
  372. then the script will still work.
  373. * The ``transform`` keyword is set to ``False`` because the data in the
  374. shapefile does not need to be converted -- it's already in WGS84 (SRID=4326).
  375. * The ``encoding`` keyword is set to the character encoding of string values in
  376. the shapefile. This ensures that string values are read and saved correctly
  377. from their original encoding system.
  378. Afterwards, invoke the Django shell from the ``geodjango`` project directory::
  379. $ python manage.py shell
  380. Next, import the ``load`` module, call the ``run`` routine, and watch ``LayerMapping``
  381. do the work::
  382. >>> from world import load
  383. >>> load.run()
  384. .. _ogrinspect-intro:
  385. Try ``ogrinspect``
  386. ------------------
  387. Now that you've seen how to define geographic models and import data with the
  388. :ref:`ref-layermapping`, it's possible to further automate this process with
  389. use of the :djadmin:`ogrinspect` management command. The :djadmin:`ogrinspect`
  390. command introspects a GDAL-supported vector data source (e.g., a shapefile) and
  391. generates a model definition and ``LayerMapping`` dictionary automatically.
  392. The general usage of the command goes as follows::
  393. $ python manage.py ogrinspect [options] <data_source> <model_name> [options]
  394. Where ``data_source`` is the path to the GDAL-supported data source and
  395. ``model_name`` is the name to use for the model. Command-line options may
  396. be used to further define how the model is generated.
  397. For example, the following command nearly reproduces the ``WorldBorders`` model
  398. and mapping dictionary created above, automatically::
  399. $ python manage.py ogrinspect world/data/TM_WORLD_BORDERS-0.3.shp WorldBorders --srid=4326 --mapping --multi
  400. A few notes about the command-line options given above:
  401. * The ``--srid=4326`` option sets the SRID for the geographic field.
  402. * The ``--mapping`` option tells ``ogrinspect`` to also generate a
  403. mapping dictionary for use with :class:`~django.contrib.gis.utils.LayerMapping`.
  404. * The ``--multi`` option is specified so that the geographic field is a
  405. :class:`~django.contrib.gis.db.models.MultiPolygonField` instead of just a
  406. :class:`~django.contrib.gis.db.models.PolygonField`.
  407. The command produces the following output, which may be copied
  408. directly into the ``models.py`` of a GeoDjango application::
  409. # This is an auto-generated Django model module created by ogrinspect.
  410. from django.contrib.gis.db import models
  411. class WorldBorders(models.Model):
  412. fips = models.CharField(max_length=2)
  413. iso2 = models.CharField(max_length=2)
  414. iso3 = models.CharField(max_length=3)
  415. un = models.IntegerField()
  416. name = models.CharField(max_length=50)
  417. area = models.IntegerField()
  418. pop2005 = models.IntegerField()
  419. region = models.IntegerField()
  420. subregion = models.IntegerField()
  421. lon = models.FloatField()
  422. lat = models.FloatField()
  423. geom = models.MultiPolygonField(srid=4326)
  424. objects = models.GeoManager()
  425. # Auto-generated `LayerMapping` dictionary for WorldBorders model
  426. worldborders_mapping = {
  427. 'fips' : 'FIPS',
  428. 'iso2' : 'ISO2',
  429. 'iso3' : 'ISO3',
  430. 'un' : 'UN',
  431. 'name' : 'NAME',
  432. 'area' : 'AREA',
  433. 'pop2005' : 'POP2005',
  434. 'region' : 'REGION',
  435. 'subregion' : 'SUBREGION',
  436. 'lon' : 'LON',
  437. 'lat' : 'LAT',
  438. 'geom' : 'MULTIPOLYGON',
  439. }
  440. Spatial Queries
  441. ===============
  442. Spatial Lookups
  443. ---------------
  444. GeoDjango extends the Django ORM and allows the use of spatial lookups.
  445. Let's do an example where we find the ``WorldBorder`` model that contains
  446. a point. First, fire up the management shell::
  447. $ python manage.py shell
  448. Now, define a point of interest [#]_::
  449. >>> pnt_wkt = 'POINT(-95.3385 29.7245)'
  450. The ``pnt_wkt`` string represents the point at -95.3385 degrees longitude,
  451. and 29.7245 degrees latitude. The geometry is in a format known as
  452. Well Known Text (WKT), an open standard issued by the Open Geospatial
  453. Consortium (OGC). [#]_ Import the ``WorldBorders`` model, and perform
  454. a ``contains`` lookup using the ``pnt_wkt`` as the parameter::
  455. >>> from world.models import WorldBorders
  456. >>> qs = WorldBorders.objects.filter(mpoly__contains=pnt_wkt)
  457. >>> qs
  458. [<WorldBorders: United States>]
  459. Here we retrieved a ``GeoQuerySet`` that has only one model: the one
  460. for the United States (which is what we would expect). Similarly,
  461. a :ref:`GEOS geometry object <ref-geos>` may also be used -- here the ``intersects``
  462. spatial lookup is combined with the ``get`` method to retrieve
  463. only the ``WorldBorders`` instance for San Marino instead of a queryset::
  464. >>> from django.contrib.gis.geos import Point
  465. >>> pnt = Point(12.4604, 43.9420)
  466. >>> sm = WorldBorders.objects.get(mpoly__intersects=pnt)
  467. >>> sm
  468. <WorldBorders: San Marino>
  469. The ``contains`` and ``intersects`` lookups are just a subset of what's
  470. available -- the :ref:`ref-gis-db-api` documentation has more.
  471. Automatic Spatial Transformations
  472. ---------------------------------
  473. When querying the spatial database GeoDjango automatically transforms
  474. geometries if they're in a different coordinate system. In the following
  475. example, the coordinate will be expressed in terms of `EPSG SRID 32140`__,
  476. a coordinate system specific to south Texas **only** and in units of
  477. **meters** and not degrees::
  478. >>> from django.contrib.gis.geos import *
  479. >>> pnt = Point(954158.1, 4215137.1, srid=32140)
  480. Note that ``pnt`` may also constructed with EWKT, an "extended" form of
  481. WKT that includes the SRID::
  482. >>> pnt = GEOSGeometry('SRID=32140;POINT(954158.1 4215137.1)')
  483. When using GeoDjango's ORM, it will automatically wrap geometry values
  484. in transformation SQL, allowing the developer to work at a higher level
  485. of abstraction::
  486. >>> qs = WorldBorders.objects.filter(mpoly__intersects=pnt)
  487. >>> qs.query.as_sql() # Generating the SQL
  488. ('SELECT "world_worldborders"."id", "world_worldborders"."name", "world_worldborders"."area",
  489. "world_worldborders"."pop2005", "world_worldborders"."fips", "world_worldborders"."iso2",
  490. "world_worldborders"."iso3", "world_worldborders"."un", "world_worldborders"."region",
  491. "world_worldborders"."subregion", "world_worldborders"."lon", "world_worldborders"."lat",
  492. "world_worldborders"."mpoly" FROM "world_worldborders"
  493. WHERE ST_Intersects("world_worldborders"."mpoly", ST_Transform(%s, 4326))',
  494. (<django.contrib.gis.db.backend.postgis.adaptor.PostGISAdaptor object at 0x25641b0>,))
  495. >>> qs # printing evaluates the queryset
  496. [<WorldBorders: United States>]
  497. __ http://spatialreference.org/ref/epsg/32140/
  498. Lazy Geometries
  499. ---------------
  500. Geometries come to GeoDjango in a standardized textual representation. Upon
  501. access of the geometry field, GeoDjango creates a `GEOS geometry object <ref-geos>`,
  502. exposing powerful functionality, such as serialization properties for
  503. popular geospatial formats::
  504. >>> sm = WorldBorders.objects.get(name='San Marino')
  505. >>> sm.mpoly
  506. <MultiPolygon object at 0x24c6798>
  507. >>> sm.mpoly.wkt # WKT
  508. MULTIPOLYGON (((12.4157980000000006 43.9579540000000009, 12.4505540000000003 43.9797209999999978, ...
  509. >>> sm.mpoly.wkb # WKB (as Python binary buffer)
  510. <read-only buffer for 0x1fe2c70, size -1, offset 0 at 0x2564c40>
  511. >>> sm.mpoly.geojson # GeoJSON (requires GDAL)
  512. '{ "type": "MultiPolygon", "coordinates": [ [ [ [ 12.415798, 43.957954 ], [ 12.450554, 43.979721 ], ...
  513. This includes access to all of the advanced geometric operations provided by
  514. the GEOS library::
  515. >>> pnt = Point(12.4604, 43.9420)
  516. >>> sm.mpoly.contains(pnt)
  517. True
  518. >>> pnt.contains(sm.mpoly)
  519. False
  520. ``GeoQuerySet`` Methods
  521. -----------------------
  522. Putting your data on the map
  523. ============================
  524. Google
  525. ------
  526. Geographic Admin
  527. ----------------
  528. GeoDjango extends :ref:`Django's admin application <ref-contrib-admin>` to
  529. enable support for editing geometry fields.
  530. Basics
  531. ^^^^^^
  532. GeoDjango also supplements the Django admin by allowing users to create
  533. and modify geometries on a JavaScript slippy map (powered by `OpenLayers`_).
  534. Let's dive in again -- create a file called ``admin.py`` inside the
  535. ``world`` application, and insert the following::
  536. from django.contrib.gis import admin
  537. from models import WorldBorders
  538. admin.site.register(WorldBorders, admin.GeoModelAdmin)
  539. Next, edit your ``urls.py`` in the ``geodjango`` project folder to look
  540. as follows::
  541. from django.conf.urls.defaults import *
  542. from django.contrib.gis import admin
  543. admin.autodiscover()
  544. urlpatterns = patterns('',
  545. (r'^admin/(.*)', include(admin.site.urls)),
  546. )
  547. Start up the Django development server::
  548. $ python manage.py runserver
  549. Finally, browse to ``http://localhost:8000/admin/``, and log in with the admin
  550. user created after running ``syncdb``. Browse to any of the ``WorldBorders``
  551. entries -- the borders may be edited by clicking on a polygon and dragging
  552. the vertexes to the desired position.
  553. .. _OpenLayers: http://openlayers.org/
  554. .. _Open Street Map: http://openstreetmap.org/
  555. .. _Vector Map Level 0: http://earth-info.nga.mil/publications/vmap0.html
  556. .. _Metacarta: http://metacarta.com
  557. .. _osmgeoadmin-intro:
  558. ``OSMGeoAdmin``
  559. ^^^^^^^^^^^^^^^
  560. With the :class:`~django.contrib.gis.admin.OSMGeoAdmin`, GeoDjango uses
  561. a `Open Street Map`_ layer in the admin.
  562. This provides more context (including street and thoroughfare details) than
  563. available with the :class:`~django.contrib.gis.admin.GeoModelAdmin`
  564. (which uses the `Vector Map Level 0`_ WMS data set hosted at `Metacarta`_).
  565. First, there are some important requirements and limitations:
  566. * :class:`~django.contrib.gis.admin.OSMGeoAdmin` requires that the
  567. :ref:`spherical mercator projection be added <addgoogleprojection>`
  568. to the to be added to the ``spatial_ref_sys`` table (PostGIS 1.3 and
  569. below, only).
  570. * The PROJ.4 datum shifting files must be installed (see the
  571. :ref:`PROJ.4 installation instructions <proj4>` for more details).
  572. If you meet these requirements, then just substitute in the ``OSMGeoAdmin``
  573. option class in your ``admin.py`` file::
  574. admin.site.register(WorldBorders, admin.OSMGeoAdmin)
  575. .. rubric:: Footnotes
  576. .. [#] Special thanks to Bjørn Sandvik of `thematicmapping.org <http://thematicmapping.org>`_ for providing and maintaining this data set.
  577. .. [#] GeoDjango basic apps was written by Dane Springmeyer, Josh Livni, and Christopher Schmidt.
  578. .. [#] Here the point is for the `University of Houston Law Center <http://www.law.uh.edu/>`_ .
  579. .. [#] Open Geospatial Consortium, Inc., `OpenGIS Simple Feature Specification For SQL <http://www.opengis.org/docs/99-049.pdf>`_, Document 99-049.