2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2010-02-25 05:20:02 +08:00
|
|
|
The GeometryProxy object, allows for lazy-geometries. The proxy uses
|
|
|
|
Python descriptors for instantiating and setting Geometry objects
|
|
|
|
corresponding to geographic model fields.
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2010-02-25 05:20:02 +08:00
|
|
|
Thanks to Robert Coup for providing this functionality (see #4322).
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2010-02-25 05:20:02 +08:00
|
|
|
class GeometryProxy(object):
|
|
|
|
def __init__(self, klass, field):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2010-02-25 05:20:02 +08:00
|
|
|
Proxy initializes on the given Geometry class (not an instance) and
|
2008-08-06 02:13:06 +08:00
|
|
|
the GeometryField.
|
|
|
|
"""
|
2010-02-25 05:20:02 +08:00
|
|
|
self._field = field
|
2008-08-06 02:13:06 +08:00
|
|
|
self._klass = klass
|
2010-02-25 05:20:02 +08:00
|
|
|
|
|
|
|
def __get__(self, obj, type=None):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
This accessor retrieves the geometry, initializing it using the geometry
|
2010-02-25 05:20:02 +08:00
|
|
|
class specified during initialization and the HEXEWKB value of the field.
|
2008-08-06 02:13:06 +08:00
|
|
|
Currently, only GEOS or OGR geometries are supported.
|
|
|
|
"""
|
2010-02-25 05:20:02 +08:00
|
|
|
if obj is None:
|
|
|
|
# Accessed on a class, not an instance
|
|
|
|
return self
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the value of the field.
|
2010-02-25 05:20:02 +08:00
|
|
|
geom_value = obj.__dict__[self._field.attname]
|
|
|
|
|
|
|
|
if isinstance(geom_value, self._klass):
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = geom_value
|
2013-10-23 18:09:29 +08:00
|
|
|
elif (geom_value is None) or (geom_value == ''):
|
2008-08-06 02:13:06 +08:00
|
|
|
geom = None
|
2010-02-25 05:20:02 +08:00
|
|
|
else:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Otherwise, a Geometry object is built using the field's contents,
|
|
|
|
# and the model's corresponding attribute is set.
|
|
|
|
geom = self._klass(geom_value)
|
2010-02-25 05:20:02 +08:00
|
|
|
setattr(obj, self._field.attname, geom)
|
|
|
|
return geom
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def __set__(self, obj, value):
|
|
|
|
"""
|
|
|
|
This accessor sets the proxied geometry with the geometry class
|
|
|
|
specified during initialization. Values of None, HEXEWKB, or WKT may
|
|
|
|
be used to set the geometry as well.
|
|
|
|
"""
|
|
|
|
# The OGC Geometry type of the field.
|
2009-03-31 01:15:49 +08:00
|
|
|
gtype = self._field.geom_type
|
2010-02-25 05:20:02 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# The geometry type must match that of the field -- unless the
|
|
|
|
# general GeometryField is used.
|
|
|
|
if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
|
|
|
|
# Assigning the SRID to the geometry.
|
2013-10-17 16:17:41 +08:00
|
|
|
if value.srid is None:
|
|
|
|
value.srid = self._field.srid
|
2014-05-28 15:22:53 +08:00
|
|
|
elif value is None or isinstance(value, six.string_types + (six.memoryview,)):
|
2010-02-25 05:20:02 +08:00
|
|
|
# Set with None, WKT, HEX, or WKB
|
2008-08-06 02:13:06 +08:00
|
|
|
pass
|
|
|
|
else:
|
2013-10-07 03:28:17 +08:00
|
|
|
raise TypeError('Cannot set %s GeometryProxy (%s) with value of type: %s' % (
|
|
|
|
obj.__class__.__name__, gtype, type(value)))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Setting the objects dictionary with the value, and returning.
|
2010-02-25 05:20:02 +08:00
|
|
|
obj.__dict__[self._field.attname] = value
|
|
|
|
return value
|