Author: jbronn
Date: 2007-07-12 01:47:40 -0500 (Thu, 12 Jul 2007)
New Revision: 5657
Added:
django/branches/gis/django/contrib/gis/db/models/mixin.py
django/branches/gis/django/contrib/gis/db/models/proxy.py
Removed:
django/branches/gis/django/contrib/gis/db/models/GeoMixin.py
Modified:
django/branches/gis/django/contrib/gis/db/models/__init__.py
django/branches/gis/django/contrib/gis/db/models/fields/__init__.py
django/branches/gis/django/contrib/gis/db/models/postgis.py
Log:
gis: lazy-geometry support has landed -- thanks Robert Coup!
Deleted: django/branches/gis/django/contrib/gis/db/models/GeoMixin.py
===================================================================
--- django/branches/gis/django/contrib/gis/db/models/GeoMixin.py
2007-07-12 06:10:14 UTC (rev 5656)
+++ django/branches/gis/django/contrib/gis/db/models/GeoMixin.py
2007-07-12 06:47:40 UTC (rev 5657)
@@ -1,44 +0,0 @@
-# GEOS Routines
-from django.contrib.gis.geos import GEOSGeometry, hex_to_wkt, centroid, area
-from django.contrib.gis.gdal import OGRGeometry, SpatialReference
-
-# Until model subclassing is a possibility, a mixin class is used to add
-# the necessary functions that may be contributed for geographic objects.
-class GeoMixin:
- "The Geographic Mixin class provides routines for geographic objects."
-
- # A subclass of Model is specifically needed so that these geographic
- # routines are present for instantiations of the models.
- def _get_GEOM_geos(self, field):
- "Returns a GEOS Python object for the geometry."
- return GEOSGeometry(getattr(self, field.attname), 'hex')
-
- def _get_GEOM_ogr(self, field, srid):
- "Returns an OGR Python object for the geometry."
- return OGRGeometry(hex_to_wkt(getattr(self, field.attname)),
- SpatialReference('EPSG:%d' % srid))
-
- def _get_GEOM_srid(self, srid):
- "Returns the spatial reference identifier (SRID) of the geometry."
- return srid
-
- def _get_GEOM_srs(self, srid):
- "Returns ane OGR Spatial Reference object of the geometry."
- return SpatialReference('EPSG:%d' % srid)
-
- def _get_GEOM_wkt(self, field):
- "Returns the WKT of the geometry."
- hex = getattr(self, field.attname)
- return hex_to_wkt(hex)
-
- def _get_GEOM_centroid(self, field):
- "Returns the centroid of the geometry, in WKT."
- hex = getattr(self, field.attname)
- return centroid(hex)
-
- def _get_GEOM_area(self, field):
- "Returns the area of the geometry, in projected units."
- hex = getattr(self, field.attname)
- return area(hex)
-
-
Modified: django/branches/gis/django/contrib/gis/db/models/__init__.py
===================================================================
--- django/branches/gis/django/contrib/gis/db/models/__init__.py
2007-07-12 06:10:14 UTC (rev 5656)
+++ django/branches/gis/django/contrib/gis/db/models/__init__.py
2007-07-12 06:47:40 UTC (rev 5657)
@@ -14,4 +14,4 @@
GeometryCollectionField
# The geographic mixin class.
-from GeoMixin import GeoMixin
+from mixin import GeoMixin
Modified: django/branches/gis/django/contrib/gis/db/models/fields/__init__.py
===================================================================
--- django/branches/gis/django/contrib/gis/db/models/fields/__init__.py
2007-07-12 06:10:14 UTC (rev 5656)
+++ django/branches/gis/django/contrib/gis/db/models/fields/__init__.py
2007-07-12 06:47:40 UTC (rev 5657)
@@ -1,5 +1,6 @@
# The Django base Field class.
from django.db.models.fields import Field
+from django.contrib.gis.db.models.proxy import GeometryProxy
from django.contrib.gis.db.models.postgis import POSTGIS_TERMS, quotename
from django.contrib.gis.oldforms import WKTField
from django.utils.functional import curry
@@ -86,9 +87,20 @@
else:
return post_sql
+ def _post_delete_sql(self, style, db_table):
+ "Drops the geometry column."
+ sql = style.SQL_KEYWORD('SELECT ') + \
+ style.SQL_KEYWORD('DropGeometryColumn') + '(' + \
+ style.SQL_TABLE(quotename(db_table)) + ', ' + \
+ style.SQL_FIELD(quotename(self.column)) + ');'
+ return sql
+
def contribute_to_class(self, cls, name):
super(GeometryField, self).contribute_to_class(cls, name)
+ # setup for lazy-instantiated GEOSGeometry objects
+ setattr(cls, self.attname, GeometryProxy(self))
+
# Adding needed accessor functions
setattr(cls, 'get_%s_geos' % self.name, curry(cls._get_GEOM_geos,
field=self))
setattr(cls, 'get_%s_ogr' % self.name, curry(cls._get_GEOM_ogr,
field=self, srid=self._srid))
Copied: django/branches/gis/django/contrib/gis/db/models/mixin.py (from rev
5636, django/branches/gis/django/contrib/gis/db/models/GeoMixin.py)
===================================================================
--- django/branches/gis/django/contrib/gis/db/models/mixin.py
(rev 0)
+++ django/branches/gis/django/contrib/gis/db/models/mixin.py 2007-07-12
06:47:40 UTC (rev 5657)
@@ -0,0 +1,47 @@
+# GEOS Routines
+from warnings import warn
+from django.contrib.gis.geos import GEOSGeometry, hex_to_wkt, centroid, area
+from django.contrib.gis.gdal import OGRGeometry, SpatialReference
+
+# Until model subclassing is a possibility, a mixin class is used to add
+# the necessary functions that may be contributed for geographic objects.
+class GeoMixin:
+ "The Geographic Mixin class provides routines for geographic objects."
+
+ # A subclass of Model is specifically needed so that these geographic
+ # routines are present for instantiations of the models.
+ def _get_GEOM_geos(self, field):
+ "Returns a GEOS Python object for the geometry."
+ warn("use model.%s" % field.attname, DeprecationWarning)
+ return getattr(self, field.attname)
+
+ def _get_GEOM_ogr(self, field, srid):
+ "Returns an OGR Python object for the geometry."
+ return OGRGeometry(hex_to_wkt(getattr(self, field.attname)),
+ SpatialReference('EPSG:%d' % srid))
+
+ def _get_GEOM_srid(self, srid):
+ "Returns the spatial reference identifier (SRID) of the geometry."
+ warn("use model.geometry_field.srid", DeprecationWarning)
+ return srid
+
+ def _get_GEOM_srs(self, srid):
+ "Returns ane OGR Spatial Reference object of the geometry."
+ return SpatialReference('EPSG:%d' % srid)
+
+ def _get_GEOM_wkt(self, field):
+ "Returns the WKT of the geometry."
+ warn("use model.%s.centroid.wkt" % field.attname, DeprecationWarning)
+ return getattr(self, field.attname).wkt
+
+ def _get_GEOM_centroid(self, field):
+ "Returns the centroid of the geometry, in WKT."
+ warn("use model.%s.centroid.wkt" % field.attname, DeprecationWarning)
+ return getattr(self, field.attname).centroid.wkt
+
+ def _get_GEOM_area(self, field):
+ "Returns the area of the geometry, in projected units."
+ warn("use model.%s.area" % field.attname, DeprecationWarning)
+ return getattr(self, field.attname).area
+
+
Modified: django/branches/gis/django/contrib/gis/db/models/postgis.py
===================================================================
--- django/branches/gis/django/contrib/gis/db/models/postgis.py 2007-07-12
06:10:14 UTC (rev 5656)
+++ django/branches/gis/django/contrib/gis/db/models/postgis.py 2007-07-12
06:47:40 UTC (rev 5657)
@@ -38,7 +38,7 @@
# PostGIS Geometry Functions -- most of these use GEOS.
POSTGIS_GEOMETRY_FUNCTIONS = {
- 'distance' : 'Distance',
+ #'distance' : 'Distance', -- doesn't work right now.
'equals' : 'Equals',
'disjoint' : 'Disjoint',
'touches' : 'Touches',
Added: django/branches/gis/django/contrib/gis/db/models/proxy.py
===================================================================
--- django/branches/gis/django/contrib/gis/db/models/proxy.py
(rev 0)
+++ django/branches/gis/django/contrib/gis/db/models/proxy.py 2007-07-12
06:47:40 UTC (rev 5657)
@@ -0,0 +1,30 @@
+"""
+ The GeometryProxy object, allows for lazy-geometries.
+
+ Thanks to Robert Coup for providing this functionality (see #4322).
+"""
+
+# GEOS Routines
+from django.contrib.gis.geos import GEOSGeometry, GEOSException
+
+# TODO: docstrings & comments
+class GeometryProxy(object):
+ def __init__(self, field):
+ self._field = field
+
+ def __get__(self, obj, type=None):
+ geom_value = obj.__dict__[self._field.attname]
+ if (geom_value is None) or (isinstance(geom_value, GEOSGeometry)):
+ geom = geom_value
+ else:
+ geom = GEOSGeometry(geom_value)
+ setattr(obj, self._field.attname, geom)
+ return geom
+
+ def __set__(self, obj, value):
+ if isinstance(value, GEOSGeometry):
+ if value and ((value.srid is None) and (self._field._srid is not
None)):
+ value.set_srid(self._field._srid)
+
+ obj.__dict__[self._field.attname] = value
+ return value
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---