Code


Version 4 (modified by tlp, 7 years ago) (diff)

Documentation on DataSource

GeoDjango Extras

TOC()? This documentation is only a stub, and will become more complete as it's written.

Built-in Models

The PostGIS spatial_ref_sys and geometry_columns tables may be accessed through the SpatialRefSys and GeometryColumns models, respectively.

SpatialRefSys

>>> from django.contrib.gis.models import SpatialRefSys
>>> wgs84 = SpatialRefSys.objects.get(srid=4326)
>>> print wgs84
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        TOWGS84[0,0,0,0,0,0,0],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.01745329251994328,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
>>> print wgs84.proj4text # PROJ.4 representation 
+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs

GeometryColumns

DataSource

>>> # read in a shapefile of zipcodes
>>> from django.contrib.gis.gdal import DataSource 
>>> ds = DataSource('zipcodep.shp')
>>>
>>> # get the number of layers in the datasource
>>> ds.layer_count
1
>>>
>>> # this shapefile has one layer, you can access the layer like this
>>> layer = ds[0]
>>>
>>> # get the field names of the layer
>>> print layer.fields
['AREA', 'PERIMETER', 'ZIP']
>>>
>>> # how many features are in this shapefile layer?
>>> layer.num_feat
150 
>>>
>>> # get the geometry type of the layer
>>> print layer.geom_type
>>> Polygon
>>>
>>> # get all the areas for the features for this layer (areas of zipcodes)
>>> layer.get_fields('area')
...
<class 'django.contrib.gis.gdal.error.OGRException'>: invalid field name: area
>>>
>>> # note that field names are case sensitive
>>> layer.get_fields('AREA')
...
 6144504439.3812199,
 0.0]
>>> # get the maximum area of the zipcodes for this layer
>>> max(layer.get_fields('AREA'))
>>> 6144504439.3812199


LayerMapping

Convenient utility for importing spatial data into geographic models from GDAL-supported data sources (e.g., SHP Files). See the [browser:django/branches/gis/django/contrib/gis/utils/layermapping.py source code] docstring for more details.

from django.contrib.gis.utils import LayerMapping

Measure

Robert Coup's [browser:django/branches/gis/django/contrib/gis/measure.py measure] module allows for convenient representation of distance units.

Distance

>>> from django.contrib.gis.measure import Distance
>>> d = Distance(mi=5) # 5 miles
>>> print d
5.0 mi
>>> print d.nm # What's that in nautical miles?
4.3448812095
>>> d += Distance(yd=100) - Distance(ft=5) # Adding 295 feet
>>> print d
5.05587121212 mi

Area