summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Bronn <jbronn@gmail.com>2009-03-30 22:54:24 +0000
committerJustin Bronn <jbronn@gmail.com>2009-03-30 22:54:24 +0000
commita1e7723c790258c994517eee9dd373d49506d8e9 (patch)
tree57609b9083661c5af35bdc1ff31d2358088a39cc
parent13ea4a913386d8374b6df82c49d4c3c895b926ba (diff)
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
-rw-r--r--django/contrib/gis/utils/srs.py42
1 files changed, 32 insertions, 10 deletions
diff --git a/django/contrib/gis/utils/srs.py b/django/contrib/gis/utils/srs.py
index 4df27961af..3fbf5d6103 100644
--- a/django/contrib/gis/utils/srs.py
+++ b/django/contrib/gis/utils/srs.py
@@ -1,27 +1,49 @@
-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
information to the PostGIS `spatial_ref_sys` table -- enabling
spatial transformations with PostGIS. This is handy for adding
- spatial reference systems not included by default with PostGIS.
- For example, the following adds the so-called "Google Maps Mercator
+ spatial reference systems not included by default with PostGIS.
+ For example, the following adds the so-called "Google Maps Mercator
Projection" (available in GDAL 1.5):
-
+
>>> add_postgis_srs(SpatialReference(900913))
- Note: By default, the `auth_name` is set to 'EPSG' -- this should
- probably be changed.
+ 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.
"""
+ from django.contrib.gis.db.backend import SpatialBackend
from django.contrib.gis.models import SpatialRefSys
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):
srs = SpatialReference(srs)
if srs.srid is None:
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.
- sr, created = SpatialRefSys.objects.get_or_create(
- srid=srs.srid, auth_name='EPSG', auth_srid=srs.srid,
- srtext=srs.wkt, proj4text=srs.proj4)
+ sr, created = SpatialRefSys.objects.get_or_create(**kwargs)