2009-03-31 06:54:24 +08:00
|
|
|
def add_postgis_srs(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
This function takes a GDAL SpatialReference system and adds its
|
|
|
|
information to the PostGIS `spatial_ref_sys` table -- enabling
|
|
|
|
spatial transformations with PostGIS. This is handy for adding
|
2009-03-31 06:54:24 +08:00
|
|
|
spatial reference systems not included by default with PostGIS.
|
|
|
|
For example, the following adds the so-called "Google Maps Mercator
|
2008-08-06 02:13:06 +08:00
|
|
|
Projection" (available in GDAL 1.5):
|
2009-03-31 06:54:24 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
>>> add_postgis_srs(SpatialReference(900913))
|
|
|
|
|
2009-03-31 06:54:24 +08:00
|
|
|
Keyword Arguments:
|
|
|
|
auth_name: This keyword may be customized with the value of the
|
|
|
|
`auth_name` field. Defaults to 'EPSG'.
|
|
|
|
|
|
|
|
auth_srid: This keyword may be customized with the value of the
|
|
|
|
`auth_srid` field. Defaults to the SRID determined
|
|
|
|
by GDAL.
|
|
|
|
|
|
|
|
ref_sys_name: For SpatiaLite users only, sets the value of the
|
|
|
|
the `ref_sys_name` field. Defaults to the name
|
|
|
|
determined by GDAL.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2009-03-31 06:54:24 +08:00
|
|
|
from django.contrib.gis.db.backend import SpatialBackend
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.models import SpatialRefSys
|
|
|
|
from django.contrib.gis.gdal import SpatialReference
|
|
|
|
|
2009-03-31 06:54:24 +08:00
|
|
|
if SpatialBackend.oracle or SpatialBackend.mysql:
|
|
|
|
raise Exception('This utility not supported on Oracle or MySQL spatial backends.')
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
if not isinstance(srs, SpatialReference):
|
|
|
|
srs = SpatialReference(srs)
|
|
|
|
|
|
|
|
if srs.srid is None:
|
|
|
|
raise Exception('Spatial reference requires an SRID to be compatible with PostGIS.')
|
2009-03-31 06:54:24 +08:00
|
|
|
|
|
|
|
# Initializing the keyword arguments dictionary for both PostGIS and SpatiaLite.
|
|
|
|
kwargs = {'srid' : srs.srid,
|
|
|
|
'auth_name' : auth_name,
|
|
|
|
'auth_srid' : auth_srid or srs.srid,
|
|
|
|
'proj4text' : srs.proj4,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Backend-specific keyword settings.
|
|
|
|
if SpatialBackend.postgis: kwargs['srtext'] = srs.wkt
|
|
|
|
if SpatialBackend.spatialite: kwargs['ref_sys_name'] = ref_sys_name or srs.name
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Creating the spatial_ref_sys model.
|
2009-03-31 06:54:24 +08:00
|
|
|
sr, created = SpatialRefSys.objects.get_or_create(**kwargs)
|