Fixed the `add_postgis_srs` routine to support SpatiaLite.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10229 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2009-03-30 22:54:24 +00:00
parent 13ea4a9133
commit a1e7723c79
1 changed files with 32 additions and 10 deletions

View File

@ -1,4 +1,4 @@
def add_postgis_srs(srs): def add_postgis_srs(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None):
""" """
This function takes a GDAL SpatialReference system and adds its This function takes a GDAL SpatialReference system and adds its
information to the PostGIS `spatial_ref_sys` table -- enabling information to the PostGIS `spatial_ref_sys` table -- enabling
@ -9,19 +9,41 @@ def add_postgis_srs(srs):
>>> add_postgis_srs(SpatialReference(900913)) >>> add_postgis_srs(SpatialReference(900913))
Note: By default, the `auth_name` is set to 'EPSG' -- this should Keyword Arguments:
probably be changed. 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.
""" """
from django.contrib.gis.db.backend import SpatialBackend
from django.contrib.gis.models import SpatialRefSys from django.contrib.gis.models import SpatialRefSys
from django.contrib.gis.gdal import SpatialReference from django.contrib.gis.gdal import SpatialReference
if SpatialBackend.oracle or SpatialBackend.mysql:
raise Exception('This utility not supported on Oracle or MySQL spatial backends.')
if not isinstance(srs, SpatialReference): if not isinstance(srs, SpatialReference):
srs = SpatialReference(srs) srs = SpatialReference(srs)
if srs.srid is None: if srs.srid is None:
raise Exception('Spatial reference requires an SRID to be compatible with PostGIS.') raise Exception('Spatial reference requires an SRID to be compatible with PostGIS.')
# 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
# Creating the spatial_ref_sys model. # Creating the spatial_ref_sys model.
sr, created = SpatialRefSys.objects.get_or_create( sr, created = SpatialRefSys.objects.get_or_create(**kwargs)
srid=srs.srid, auth_name='EPSG', auth_srid=srs.srid,
srtext=srs.wkt, proj4text=srs.proj4)