GeoDjango Extras
Table of Contents
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
>>>
>>> # get the extent of your layer (what is the bounding box that this layer covers?)
>>> layer.extent.wkt
'POLYGON((2901388.75258 13655150.1056,2901388.75258 14013756.5554,3265973.14723 14013756.5554,3265973.14723 13655150.1056,2901388.75258 13655150.1056))'
>>> # seeing this does not tell you much if you do not know what srs you are using
>>> print layer.srs
PROJCS["NAD_1983_StatePlane_Texas_South_Central_FIPS_4204_Feet",
GEOGCS["GCS_North_American_1983",
DATUM["North_American_Datum_1983",
SPHEROID["GRS_1980",6378137.0,298.257222101]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]],
PROJECTION["Lambert_Conformal_Conic_2SP"],
PARAMETER["False_Easting",1968500.0],
PARAMETER["False_Northing",13123333.33333333],
PARAMETER["Central_Meridian",-99.0],
PARAMETER["Standard_Parallel_1",28.38333333333333],
PARAMETER["Standard_Parallel_2",30.28333333333334],
PARAMETER["Latitude_Of_Origin",27.83333333333333],
UNIT["Foot_US",0.3048006096012192]]
>>>
LayerMapping
Convenient utility for importing spatial data into geographic models from GDAL-supported data sources (e.g., SHP Files). See the source code docstring for more details.
from django.contrib.gis.utils import LayerMapping
from django.contrib.gis.gdal import DataSource
from geoapp.models import Zipcode
ds = DataSource('/data/zipcodep.shp')
# set up a mapping dictionary
# the key corresponds to the attribute of the model you are saving to
# the value corresponds to the field name in the shapefile
mapping = {'zipcode' : 'ZIP',
'area' : 'AREA',
'perimeter' : 'PERIMETER',
'poly' : 'POLYGON'}
# if the shapefile does not have the srid explicitly set you will need to pass a srs
srs = SpatialRefSys.objects.get(srid=2278)
lm = LayerMapping(Zipcode, ds, mapping, source_srs=srs)
lm.save(verbose=True)
Measure
Robert Coup's measure module allows for convenient representation of distance units.
Supported units are:
- m: meters
- km: kilometers
- mi: miles
- ft: feet
- yd: yards
- nm: nautical miles
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
