Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

gis: NULL geometries are now allowed (thanks Robert Coup!), field par…

…ameter no longer needed for _post_create_sql(), and added extra instance methods for SpatialReference/SRID.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5448 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6538a26a4728e26328ad97163cf094ec92800a45 1 parent b962a44
Justin Bronn authored June 09, 2007
20  django/contrib/gis/db/models/GeoMixin.py
@@ -5,31 +5,39 @@
5 5
 # Until model subclassing is a possibility, a mixin class is used to add
6 6
 # the necessary functions that may be contributed for geographic objects.
7 7
 class GeoMixin:
8  
-    "The Geographic Mixin class, provides routines for geographic objects."
  8
+    "The Geographic Mixin class provides routines for geographic objects."
9 9
 
10 10
     # A subclass of Model is specifically needed so that these geographic
11 11
     # routines are present for instantiations of the models.
12 12
     def _get_GEOM_geos(self, field):
13  
-        "Gets a GEOS Python object for the geometry."
  13
+        "Returns a GEOS Python object for the geometry."
14 14
         return GEOSGeometry(getattr(self, field.attname), 'hex')
15 15
 
16 16
     def _get_GEOM_ogr(self, field, srid):
17  
-        "Gets an OGR Python object for the geometry."
  17
+        "Returns an OGR Python object for the geometry."
18 18
         return OGRGeometry(hex_to_wkt(getattr(self, field.attname)),
19 19
                            SpatialReference('EPSG:%d' % srid))
20 20
 
  21
+    def _get_GEOM_srid(self, srid):
  22
+        "Returns the spatial reference identifier (SRID) of the geometry."
  23
+        return srid
  24
+
  25
+    def _get_GEOM_srs(self, srid):
  26
+        "Returns ane OGR Spatial Reference object of the geometry."
  27
+        return SpatialReference('EPSG:%d' % srid)
  28
+
21 29
     def _get_GEOM_wkt(self, field):
22  
-        "Gets the WKT of the geometry."
  30
+        "Returns the WKT of the geometry."
23 31
         hex = getattr(self, field.attname)
24 32
         return hex_to_wkt(hex)
25 33
 
26 34
     def _get_GEOM_centroid(self, field):
27  
-        "Gets the centroid of the geometry, in WKT."
  35
+        "Returns the centroid of the geometry, in WKT."
28 36
         hex = getattr(self, field.attname)
29 37
         return centroid(hex)
30 38
     
31 39
     def _get_GEOM_area(self, field):
32  
-        "Gets the area of the geometry, in projected units."
  40
+        "Returns the area of the geometry, in projected units."
33 41
         hex = getattr(self, field.attname)
34 42
         return area(hex)
35 43
 
37  django/contrib/gis/db/models/fields/__init__.py
@@ -33,47 +33,56 @@ def __init__(self, srid=4326, index=True, dim=2, **kwargs):
33 33
         self._dim = dim
34 34
         super(GeometryField, self).__init__(**kwargs) # Calling the parent initializtion function
35 35
 
36  
-    def _add_geom(self, style, db_table, field):
  36
+    def _add_geom(self, style, db_table):
37 37
         """Constructs the addition of the geometry to the table using the
38  
-        AddGeometryColumn(...) PostGIS (and OGC standard) function.
  38
+        AddGeometryColumn(...) PostGIS (and OGC standard) stored procedure.
39 39
 
40  
-        Takes the style object (provides syntax highlighting) as well as the
41  
-         database table and field.
  40
+        Takes the style object (provides syntax highlighting) and the
  41
+         database table as parameters.
42 42
         """
43 43
         sql = style.SQL_KEYWORD('SELECT ') + \
44 44
               style.SQL_TABLE('AddGeometryColumn') + '(' + \
45 45
               style.SQL_TABLE(quotename(db_table)) + ', ' + \
46  
-              style.SQL_FIELD(quotename(field)) + ', ' + \
  46
+              style.SQL_FIELD(quotename(self.column)) + ', ' + \
47 47
               style.SQL_FIELD(str(self._srid)) + ', ' + \
48 48
               style.SQL_COLTYPE(quotename(self._geom)) + ', ' + \
49 49
               style.SQL_KEYWORD(str(self._dim)) + ');'
  50
+
  51
+        if not self.null:
  52
+            # Add a NOT NULL constraint to the field
  53
+            sql += '\n' + \
  54
+                   style.SQL_KEYWORD('ALTER TABLE ') + \
  55
+                   style.SQL_TABLE(quotename(db_table, dbl=True)) + \
  56
+                   style.SQL_KEYWORD(' ALTER ') + \
  57
+                   style.SQL_FIELD(quotename(self.column, dbl=True)) + \
  58
+                   style.SQL_KEYWORD(' SET NOT NULL') + ';' 
50 59
         return sql
51 60
 
52  
-    def _geom_index(self, style, db_table, field,
  61
+    def _geom_index(self, style, db_table,
53 62
                     index_type='GIST', index_opts='GIST_GEOMETRY_OPS'):
54 63
         "Creates a GiST index for this geometry field."
55 64
         sql = style.SQL_KEYWORD('CREATE INDEX ') + \
56  
-              style.SQL_TABLE(quotename('%s_%s_id' % (db_table, field), dbl=True)) + \
  65
+              style.SQL_TABLE(quotename('%s_%s_id' % (db_table, self.column), dbl=True)) + \
57 66
               style.SQL_KEYWORD(' ON ') + \
58 67
               style.SQL_TABLE(quotename(db_table, dbl=True)) + \
59 68
               style.SQL_KEYWORD(' USING ') + \
60 69
               style.SQL_COLTYPE(index_type) + ' ( ' + \
61  
-              style.SQL_FIELD(quotename(field, dbl=True)) + ' ' + \
  70
+              style.SQL_FIELD(quotename(self.column, dbl=True)) + ' ' + \
62 71
               style.SQL_KEYWORD(index_opts) + ' );'
63 72
         return sql
64 73
 
65  
-    def _post_create_sql(self, style, db_table, field):
  74
+    def _post_create_sql(self, style, db_table):
66 75
         """Returns SQL that will be executed after the model has been
67 76
         created. Geometry columns must be added after creation with the
68 77
         PostGIS AddGeometryColumn() function."""
69 78
 
70 79
         # Getting the AddGeometryColumn() SQL necessary to create a PostGIS
71 80
         # geometry field.
72  
-        post_sql = self._add_geom(style, db_table, field)
  81
+        post_sql = self._add_geom(style, db_table)
73 82
 
74 83
         # If the user wants to index this data, then get the indexing SQL as well.
75 84
         if self._index:
76  
-            return '%s\n%s' % (post_sql, self._geom_index(style, db_table, field))
  85
+            return '%s\n%s' % (post_sql, self._geom_index(style, db_table))
77 86
         else:
78 87
             return post_sql
79 88
 
@@ -83,6 +92,8 @@ def contribute_to_class(self, cls, name):
83 92
         # Adding needed accessor functions
84 93
         setattr(cls, 'get_%s_geos' % self.name, curry(cls._get_GEOM_geos, field=self))
85 94
         setattr(cls, 'get_%s_ogr' % self.name, curry(cls._get_GEOM_ogr, field=self, srid=self._srid))
  95
+        setattr(cls, 'get_%s_srid' % self.name, curry(cls._get_GEOM_srid, srid=self._srid))
  96
+        setattr(cls, 'get_%s_srs' % self.name, curry(cls._get_GEOM_srs, srid=self._srid))
86 97
         setattr(cls, 'get_%s_wkt' % self.name, curry(cls._get_GEOM_wkt, field=self))
87 98
         setattr(cls, 'get_%s_centroid' % self.name, curry(cls._get_GEOM_centroid, field=self))
88 99
         setattr(cls, 'get_%s_area' % self.name, curry(cls._get_GEOM_area, field=self))
@@ -94,12 +105,12 @@ def get_db_prep_lookup(self, lookup_type, value):
94 105
         """Returns field's value prepared for database lookup; the SRID of the geometry is
95 106
         included by default in these queries."""
96 107
         if lookup_type in POSTGIS_TERMS:
97  
-            return ['SRID=%d;%s' % (self._srid, value)]
  108
+            return [value and ("SRID=%d;%s" % (self._srid, value)) or None]
98 109
         raise TypeError("Field has invalid lookup: %s" % lookup_type)
99 110
 
100 111
     def get_db_prep_save(self, value):
101 112
         "Making sure the SRID is included before saving."
102  
-        return 'SRID=%d;%s' % (self._srid, value)
  113
+        return value and ("SRID=%d;%s" % (self._srid, value)) or None
103 114
 
104 115
     def get_manipulator_field_objs(self):
105 116
         "Using the WKTField (defined above) to be our manipulator."
2  django/core/management.py
@@ -388,7 +388,7 @@ def get_custom_sql_for_model(model):
388 388
 
389 389
     for f in opts.fields:
390 390
         if hasattr(f, '_post_create_sql'):
391  
-            output.append(f._post_create_sql(style, model._meta.db_table, f.column))
  391
+            output.append(f._post_create_sql(style, model._meta.db_table))
392 392
 
393 393
     return output
394 394
 

0 notes on commit 6538a26

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