Changes between Initial Version and Version 1 of GeoDjangoModelAPI


Ignore:
Timestamp:
Jun 2, 2007, 10:33:12 PM (18 years ago)
Author:
jbronn
Comment:

added GeoDjango model api

Legend:

Unmodified
Added
Removed
Modified
  • GeoDjangoModelAPI

    v1 v1  
     1[[TOC]]
     2= Model API =
     3This section explores the details of the GeoDjango Model API.  Throughout this section, we'll be using the following geographic model of a Zip code as our example:
     4{{{
     5#!python
     6from django.contrib.gis.db import models
     7
     8class Zip(models.Model, models.GeoMixin):
     9  code = models.CharField(maxlength=5)
     10  poly = models.PolygonField()
     11
     12  object = models.GeoManager()
     13}}}
     14
     15== Field Types ==
     16
     17The following geometry-enabled fields are available:
     18 * {{{PointField}}}
     19 * {{{LineStringField}}}
     20 * {{{PolygonField}}}
     21 * {{{MultiPointField}}}
     22 * {{{MultiLineStringField}}}
     23 * {{{MultiPolygonField}}}
     24 * {{{GeometryCollectionField}}}
     25
     26Each of these fields correspond with OpenGIS [http://en.wikipedia.org/wiki/Simple_Features Simple Features]. 
     27
     28== Field Options ==
     29
     30In addition to the regular [http://www.djangoproject.com/documentation/model-api/#field-options field options] available for Django models, Geographic-enabled models have  the following additional options:
     31 * {{{srid}}}
     32   * Sets the SRID (Spatial Reference System Identity) of geometry to the given value.  Defaults to 4326 (WGS84).  ''See'' Open GIS Consortium, Inc., ''[http://www.opengis.org/docs/99-049.pdf OpenGIS Simple Feature Specification For SQL]'', Document 99-049 (May 5, 1999), at  Ch. 2.3.8 (Geometry Values and Spatial Reference Systems, pg. 39).
     33 * {{{index}}}
     34   * Defaults to True.  Creates a GiST index for the given geometry.  Update the index with the PostgreSQL command {{{VACUUM ANALYZE}}} (may take a while to execute depending on how large your geographic-enabled tables are).  Please note that this is different from the {{{db_index}}} field option since geographic indexes are created in a different manner than regular indexes.
     35
     36== GeoMixin ==
     37
     38The [http://code.djangoproject.com/browser/django/branches/gis/django/contrib/gis/db/models/GeoMixin.py GeoMixin] is necessary for all geographic-enabled models.  The mixin provides extra instance methods (discussed in the database api docs) for geographic models without subclassing (aka ModelInheritance) -- which is not yet functional in Django.
     39
     40== GeoManager ==
     41
     42In order to conduct geographic queries, each geographic model requires {{{GeoManager}}}.  This [http://www.djangoproject.com/documentation/model-api/#managers manager] allows for the proper SQL construction for geographic queries; thus, without it, all geographic filters will fail.  It should also be noted that a {{{GeoManager}}} will be required even if the model does not have a geographic field itself, ''i.e.'',in the case of a {{{ForeignKey}}} to a geographic model.  For example, if we had an {{{Address}}} model with a {{{ForeignKey}}} to our {{{Zip}}} geographic model:
     43{{{
     44#!python
     45from django.contrib.gis.db import models
     46
     47class Address(models.Model):
     48    num = models.IntegerField()
     49    street = models.CharField(maxlength=100)
     50    city = models.CharField(maxlength=100)
     51    state = models.USStateField()
     52    zip = models.ForeignKey(Zip)
     53
     54    objects = models.GeoManager()
     55}}}
     56
     57The geographic manager is needed to do spatial queries on [http://www.djangoproject.com/documentation/db-api/#related-objects related Zip objects], for example:
     58{{{
     59>>> qs = Address.objects.filter(zip__poly__contains='POINT(-104.590948 38.319914)')
     60}}}
Back to Top