"""
GeoDjango introspection rules
"""

import django
from django.conf import settings

from south.modelsinspector import add_introspection_rules


def has_spatialite():
    "Checks for the presence of SpataiLite"
    try:
        from ctypes.util import find_library
    except ImportError:
        return False
    from django.conf import settings
    return bool(getattr(settings, 'SPATIALITE_LIBRARY_PATH', find_library('spatialite')))


def has_geos():
    try:
        from django.contrib.gis.geos import libgeos
    except (ImportError, OSError):
        return False
    else:
        return True


# First, work out if GIS is enabled
# (If it isn't importing the field will fail)
has_gis = has_geos() and \
          ((settings.DATABASE_ENGINE in ["postgresql", "postgresql_psycopg2", "mysql"]) or \
          (settings.DATABASE_ENGINE == "sqlite3" and has_spatialite()))

if has_gis:
    # Alright,import the field
    from django.contrib.gis.db.models.fields import GeometryField
    
    # Make some introspection rules
    if django.VERSION[0] == 1 and django.VERSION[1] >= 1:
        # Django 1.1's gis module renamed these.
        rules = [
            (
                (GeometryField, ),
                [],
                {
                    "srid": ["srid", {"default": 4326}],
                    "spatial_index": ["spatial_index", {"default": True}],
                    "dim": ["dim", {"default": 2}],
                },
            ),
        ]
    else:
        rules = [
            (
                (GeometryField, ),
                [],
                {
                    "srid": ["_srid", {"default": 4326}],
                    "spatial_index": ["_spatial_index", {"default": True}],
                    "dim": ["_dim", {"default": 2}],
                },
            ),
        ]
    
    # Install them
    add_introspection_rules(rules, ["^django\.contrib\.gis"])
@@ -874,16 +874,226 @@
 te()))%0A%0A
+# Build a tuple of possible database errors%0Adatabase_error_classes = tuple()%0Atry:%0A    from psycopg2 import ProgrammingError%0Aexcept ImportError:%0A    pass%0Aelse:%0A    database_error_classes += (ProgrammingError,)%0A%0A
 if has_g
@@ -1123,24 +1123,37 @@
 t the field%0A
+    try:%0A    
     from dja
@@ -1206,25 +1206,93 @@
 ryField%0A    
-%0A
+except database_error_classes:%0A        has_gis = False%0A    else:%0A    
     # Make s
@@ -1315,16 +1315,20 @@
 n rules%0A
+    
     if d
@@ -1385,16 +1385,20 @@
         
+    
 # Django
@@ -1430,16 +1430,20 @@
  these.%0A
+    
         
@@ -1456,34 +1456,42 @@
 = %5B%0A            
-(%0A
+    (%0A    
                 
@@ -1517,32 +1517,36 @@
                 
+    
 %5B%5D,%0A            
@@ -1541,34 +1541,42 @@
                 
+ 
-%7B%0A
+   %7B%0A    
                 
@@ -1628,32 +1628,36 @@
                 
+    
 %22spatial_index%22:
@@ -1707,32 +1707,36 @@
                 
+ 
+   
 %22dim%22: %5B%22dim%22, %7B
@@ -1759,35 +1759,43 @@
                 
-%7D,%0A
+    %7D,%0A    
             ),%0A 
@@ -1797,26 +1797,34 @@
  ),%0A        
+  
-%5D%0A
+  %5D%0A    
     else:%0A  
@@ -1821,32 +1821,36 @@
   else:%0A        
+    
 rules = %5B%0A      
@@ -1847,34 +1847,42 @@
 = %5B%0A            
+  
-(%0A
+  (%0A    
                 
@@ -1908,32 +1908,36 @@
                 
+    
 %5B%5D,%0A            
@@ -1932,34 +1932,42 @@
                 
+   
+ 
 %7B%0A
+    
                 
@@ -2020,32 +2020,36 @@
                 
+    
 %22spatial_index%22:
@@ -2100,32 +2100,36 @@
                 
+   
+ 
 %22dim%22: %5B%22_dim%22, 
@@ -2153,35 +2153,43 @@
                 
-%7D,%0A
+    %7D,%0A    
             ),%0A 
@@ -2179,32 +2179,36 @@
              ),%0A
+    
         %5D%0A    %0A 
@@ -2205,17 +2205,25 @@
   %5D%0A    
-%0A
+    %0A    
     # In
@@ -2233,16 +2233,20 @@
 ll them%0A
+    
     add_

Fix geodhjjango detection