Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12591 -- Renamed `add_postgis_srs` to `add_srs_entry` (keeping…

… backwards-compatible alias), added `database` keyword for multi-db, removed deprecated `SpatialBackend` references; no longer use `get_or_create` and look for existing entry based on SRID value.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12228 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 19fad1641408cd9db3053499296380e1a097cf3f 1 parent a2a5602
Justin Bronn authored January 14, 2010
2  django/contrib/gis/utils/__init__.py
@@ -6,7 +6,7 @@
6 6
 if HAS_GDAL:
7 7
     from django.contrib.gis.utils.ogrinfo import ogrinfo, sample
8 8
     from django.contrib.gis.utils.ogrinspect import mapping, ogrinspect
9  
-    from django.contrib.gis.utils.srs import add_postgis_srs
  9
+    from django.contrib.gis.utils.srs import add_postgis_srs, add_srs_entry
10 10
     try:
11 11
         # LayerMapping requires DJANGO_SETTINGS_MODULE to be set, 
12 12
         # so this needs to be in try/except.
84  django/contrib/gis/utils/srs.py
... ...
@@ -1,49 +1,77 @@
1  
-def add_postgis_srs(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None):
  1
+from django.contrib.gis.gdal import SpatialReference
  2
+from django.db import connections, DEFAULT_DB_ALIAS
  3
+
  4
+def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
  5
+                  database=DEFAULT_DB_ALIAS):
2 6
     """
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):
  7
+    This function takes a GDAL SpatialReference system and adds its information
  8
+    to the `spatial_ref_sys` table of the spatial backend.  Doing this enables
  9
+    database-level spatial transformations for the backend.  Thus, this utility
  10
+    is useful for adding spatial reference systems not included by default with
  11
+    the backend -- for example, the so-called "Google Maps Mercator Projection"
  12
+    is excluded in PostGIS 1.3 and below, and the following adds it to the
  13
+    `spatial_ref_sys` table:
9 14
 
10  
-    >>> add_postgis_srs(SpatialReference(900913))
  15
+    >>> from django.contrib.gis.utils import add_srs_entry
  16
+    >>> add_srs_entry(900913)
11 17
 
12 18
     Keyword Arguments:
13  
-     auth_name: This keyword may be customized with the value of the
14  
-                `auth_name` field.  Defaults to 'EPSG'.
  19
+     auth_name:
  20
+       This keyword may be customized with the value of the `auth_name` field.
  21
+       Defaults to 'EPSG'.
15 22
 
16  
-     auth_srid: This keyword may be customized with the value of the
17  
-                `auth_srid` field.  Defaults to the SRID determined
18  
-                by GDAL.
  23
+     auth_srid:
  24
+       This keyword may be customized with the value of the `auth_srid` field.
  25
+       Defaults to the SRID determined by GDAL.
19 26
 
20  
-     ref_sys_name: For SpatiaLite users only, sets the value of the
21  
-                   the `ref_sys_name` field.  Defaults to the name
22  
-                   determined by GDAL.
23  
-    """
24  
-    from django.contrib.gis.db.backend import SpatialBackend
25  
-    from django.contrib.gis.models import SpatialRefSys
26  
-    from django.contrib.gis.gdal import SpatialReference
  27
+     ref_sys_name:
  28
+       For SpatiaLite users only, sets the value of the the `ref_sys_name` field.
  29
+       Defaults to the name determined by GDAL.
27 30
 
28  
-    if SpatialBackend.oracle or SpatialBackend.mysql:
29  
-        raise Exception('This utility not supported on Oracle or MySQL spatial backends.')
  31
+     database:
  32
+      The name of the database connection to use; the default is the value
  33
+      of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, it's value
  34
+      is 'default').
  35
+    """
  36
+    connection = connections[database]
  37
+    if not hasattr(connection.ops, 'spatial_version'):
  38
+        raise Exception('The `add_srs_entry` utility only works '
  39
+                        'with spatial backends.')
  40
+    if connection.ops.oracle or connection.ops.mysql:
  41
+        raise Exception('This utility does not support the '
  42
+                        'Oracle or MySQL spatial backends.')
  43
+    SpatialRefSys = connection.ops.spatial_ref_sys()
30 44
 
  45
+    # If argument is not a `SpatialReference` instance, use it as parameter
  46
+    # to construct a `SpatialReference` instance.
31 47
     if not isinstance(srs, SpatialReference):
32 48
         srs = SpatialReference(srs)
33 49
 
34 50
     if srs.srid is None:
35  
-        raise Exception('Spatial reference requires an SRID to be compatible with PostGIS.')
  51
+        raise Exception('Spatial reference requires an SRID to be '
  52
+                        'compatible with the spatial backend.')
36 53
 
37  
-    # Initializing the keyword arguments dictionary for both PostGIS and SpatiaLite.
  54
+    # Initializing the keyword arguments dictionary for both PostGIS
  55
+    # and SpatiaLite.
38 56
     kwargs = {'srid' : srs.srid,
39 57
               'auth_name' : auth_name,
40 58
               'auth_srid' : auth_srid or srs.srid,
41 59
               'proj4text' : srs.proj4,
42 60
               }
43 61
 
44  
-    # Backend-specific keyword settings.
45  
-    if SpatialBackend.postgis: kwargs['srtext'] = srs.wkt
46  
-    if SpatialBackend.spatialite: kwargs['ref_sys_name'] = ref_sys_name or srs.name
  62
+    # Backend-specific fields for the SpatialRefSys model.
  63
+    if connection.ops.postgis:
  64
+        kwargs['srtext'] = srs.wkt
  65
+    if connection.ops.spatialite:
  66
+        kwargs['ref_sys_name'] = ref_sys_name or srs.name
47 67
 
48 68
     # Creating the spatial_ref_sys model.
49  
-    sr, created = SpatialRefSys.objects.get_or_create(**kwargs)
  69
+    try:
  70
+        # Try getting via SRID only, because using all kwargs may
  71
+        # differ from exact wkt/proj in database.
  72
+        sr = SpatialRefSys.objects.get(srid=srs.srid)
  73
+    except SpatialRefSys.DoesNotExist:
  74
+        sr = SpatialRefSys.objects.create(**kwargs)
  75
+
  76
+# Alias is for backwards-compatibility purposes.
  77
+add_postgis_srs = add_srs_entry

0 notes on commit 19fad16

Please sign in to comment.
Something went wrong with that request. Please try again.