~canonical-django/canonical-django/project-template

« back to all changes in this revision

Viewing changes to trunk/python-packages/django/contrib/gis/utils/srs.py

  • Committer: Matthew Nuzum
  • Date: 2008-11-13 05:46:03 UTC
  • Revision ID: matthew.nuzum@canonical.com-20081113054603-v0kvr6z6xyexvqt3
adding to version control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
def add_postgis_srs(srs):
 
2
    """
 
3
    This function takes a GDAL SpatialReference system and adds its
 
4
    information to the PostGIS `spatial_ref_sys` table -- enabling
 
5
    spatial transformations with PostGIS.  This is handy for adding
 
6
    spatial reference systems not included by default with PostGIS.  
 
7
    For example, the following adds the so-called "Google Maps Mercator 
 
8
    Projection" (available in GDAL 1.5):
 
9
    
 
10
    >>> add_postgis_srs(SpatialReference(900913))
 
11
 
 
12
    Note: By default, the `auth_name` is set to 'EPSG' -- this should
 
13
    probably be changed.
 
14
    """
 
15
    from django.contrib.gis.models import SpatialRefSys
 
16
    from django.contrib.gis.gdal import SpatialReference
 
17
 
 
18
    if not isinstance(srs, SpatialReference):
 
19
        srs = SpatialReference(srs)
 
20
 
 
21
    if srs.srid is None:
 
22
        raise Exception('Spatial reference requires an SRID to be compatible with PostGIS.')
 
23
   
 
24
    # Creating the spatial_ref_sys model.
 
25
    sr, created = SpatialRefSys.objects.get_or_create(
 
26
        srid=srs.srid, auth_name='EPSG', auth_srid=srs.srid, 
 
27
        srtext=srs.wkt, proj4text=srs.proj4)