Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

gis: lazy-geometry support has landed -- thanks Robert Coup!

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@5657 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit abccccc09519a10b4a860d8ddb47d29c73d4abcc 1 parent 5292d44
Justin Bronn authored July 12, 2007
2  django/contrib/gis/db/models/__init__.py
@@ -14,4 +14,4 @@
14 14
      GeometryCollectionField
15 15
 
16 16
 # The geographic mixin class.
17  
-from GeoMixin import GeoMixin
  17
+from mixin import GeoMixin
12  django/contrib/gis/db/models/fields/__init__.py
... ...
@@ -1,5 +1,6 @@
1 1
 # The Django base Field class.
2 2
 from django.db.models.fields import Field
  3
+from django.contrib.gis.db.models.proxy import GeometryProxy
3 4
 from django.contrib.gis.db.models.postgis import POSTGIS_TERMS, quotename
4 5
 from django.contrib.gis.oldforms import WKTField
5 6
 from django.utils.functional import curry
@@ -86,9 +87,20 @@ def _post_create_sql(self, style, db_table):
86 87
         else:
87 88
             return post_sql
88 89
 
  90
+    def _post_delete_sql(self, style, db_table):
  91
+        "Drops the geometry column."
  92
+        sql = style.SQL_KEYWORD('SELECT ') + \
  93
+            style.SQL_KEYWORD('DropGeometryColumn') + '(' + \
  94
+            style.SQL_TABLE(quotename(db_table)) + ', ' + \
  95
+            style.SQL_FIELD(quotename(self.column)) +  ');'
  96
+        return sql
  97
+
89 98
     def contribute_to_class(self, cls, name):
90 99
         super(GeometryField, self).contribute_to_class(cls, name)
91 100
 
  101
+        # setup for lazy-instantiated GEOSGeometry objects
  102
+        setattr(cls, self.attname, GeometryProxy(self))
  103
+
92 104
         # Adding needed accessor functions
93 105
         setattr(cls, 'get_%s_geos' % self.name, curry(cls._get_GEOM_geos, field=self))
94 106
         setattr(cls, 'get_%s_ogr' % self.name, curry(cls._get_GEOM_ogr, field=self, srid=self._srid))
17  django/contrib/gis/db/models/GeoMixin.py → django/contrib/gis/db/models/mixin.py
... ...
@@ -1,4 +1,5 @@
1 1
 # GEOS Routines
  2
+from warnings import warn
2 3
 from django.contrib.gis.geos import GEOSGeometry, hex_to_wkt, centroid, area
3 4
 from django.contrib.gis.gdal import OGRGeometry, SpatialReference
4 5
 
@@ -11,7 +12,8 @@ class GeoMixin:
11 12
     # routines are present for instantiations of the models.
12 13
     def _get_GEOM_geos(self, field):
13 14
         "Returns a GEOS Python object for the geometry."
14  
-        return GEOSGeometry(getattr(self, field.attname), 'hex')
  15
+        warn("use model.%s" % field.attname, DeprecationWarning) 
  16
+        return getattr(self, field.attname)
15 17
 
16 18
     def _get_GEOM_ogr(self, field, srid):
17 19
         "Returns an OGR Python object for the geometry."
@@ -20,6 +22,7 @@ def _get_GEOM_ogr(self, field, srid):
20 22
 
21 23
     def _get_GEOM_srid(self, srid):
22 24
         "Returns the spatial reference identifier (SRID) of the geometry."
  25
+        warn("use model.geometry_field.srid", DeprecationWarning)
23 26
         return srid
24 27
 
25 28
     def _get_GEOM_srs(self, srid):
@@ -28,17 +31,17 @@ def _get_GEOM_srs(self, srid):
28 31
 
29 32
     def _get_GEOM_wkt(self, field):
30 33
         "Returns the WKT of the geometry."
31  
-        hex = getattr(self, field.attname)
32  
-        return hex_to_wkt(hex)
  34
+        warn("use model.%s.centroid.wkt" % field.attname, DeprecationWarning) 
  35
+        return getattr(self, field.attname).wkt
33 36
 
34 37
     def _get_GEOM_centroid(self, field):
35 38
         "Returns the centroid of the geometry, in WKT."
36  
-        hex = getattr(self, field.attname)
37  
-        return centroid(hex)
  39
+        warn("use model.%s.centroid.wkt" % field.attname, DeprecationWarning) 
  40
+        return getattr(self, field.attname).centroid.wkt
38 41
     
39 42
     def _get_GEOM_area(self, field):
40 43
         "Returns the area of the geometry, in projected units."
41  
-        hex = getattr(self, field.attname)
42  
-        return area(hex)
  44
+        warn("use model.%s.area" % field.attname, DeprecationWarning)
  45
+        return getattr(self, field.attname).area
43 46
 
44 47
 
2  django/contrib/gis/db/models/postgis.py
@@ -38,7 +38,7 @@
38 38
 
39 39
 # PostGIS Geometry Functions -- most of these use GEOS.
40 40
 POSTGIS_GEOMETRY_FUNCTIONS = {
41  
-    'distance' : 'Distance',
  41
+    #'distance' : 'Distance', -- doesn't work right now.
42 42
     'equals' : 'Equals',
43 43
     'disjoint' : 'Disjoint',
44 44
     'touches' : 'Touches',
30  django/contrib/gis/db/models/proxy.py
... ...
@@ -0,0 +1,30 @@
  1
+"""
  2
+  The GeometryProxy object, allows for lazy-geometries.
  3
+
  4
+  Thanks to Robert Coup for providing this functionality (see #4322).
  5
+"""
  6
+
  7
+# GEOS Routines 
  8
+from django.contrib.gis.geos import GEOSGeometry, GEOSException 
  9
+
  10
+# TODO: docstrings & comments
  11
+class GeometryProxy(object): 
  12
+    def __init__(self, field): 
  13
+        self._field = field 
  14
+     
  15
+    def __get__(self, obj, type=None): 
  16
+        geom_value = obj.__dict__[self._field.attname] 
  17
+        if (geom_value is None) or (isinstance(geom_value, GEOSGeometry)): 
  18
+            geom = geom_value 
  19
+        else: 
  20
+            geom = GEOSGeometry(geom_value) 
  21
+            setattr(obj, self._field.attname, geom) 
  22
+        return geom 
  23
+     
  24
+    def __set__(self, obj, value): 
  25
+        if isinstance(value, GEOSGeometry): 
  26
+            if value and ((value.srid is None) and (self._field._srid is not None)): 
  27
+                value.set_srid(self._field._srid) 
  28
+     
  29
+        obj.__dict__[self._field.attname] = value 
  30
+        return value 

0 notes on commit abccccc

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