app/django/contrib/gis/db/models/proxy.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 """
       
     2  The GeometryProxy object, allows for lazy-geometries.  The proxy uses
       
     3  Python descriptors for instantiating and setting Geometry objects
       
     4  corresponding to geographic model fields.
       
     5 
       
     6  Thanks to Robert Coup for providing this functionality (see #4322).
       
     7 """
       
     8 
       
     9 from types import NoneType, StringType, UnicodeType
       
    10 
       
    11 class GeometryProxy(object): 
       
    12     def __init__(self, klass, field): 
       
    13         """
       
    14         Proxy initializes on the given Geometry class (not an instance) and 
       
    15         the GeometryField.
       
    16         """
       
    17         self._field = field 
       
    18         self._klass = klass
       
    19      
       
    20     def __get__(self, obj, type=None): 
       
    21         """
       
    22         This accessor retrieves the geometry, initializing it using the geometry
       
    23         class specified during initialization and the HEXEWKB value of the field.  
       
    24         Currently, only GEOS or OGR geometries are supported.
       
    25         """
       
    26         # Getting the value of the field.
       
    27         geom_value = obj.__dict__[self._field.attname] 
       
    28         
       
    29         if isinstance(geom_value, self._klass): 
       
    30             geom = geom_value
       
    31         elif (geom_value is None) or (geom_value==''):
       
    32             geom = None
       
    33         else: 
       
    34             # Otherwise, a Geometry object is built using the field's contents,
       
    35             # and the model's corresponding attribute is set.
       
    36             geom = self._klass(geom_value)
       
    37             setattr(obj, self._field.attname, geom) 
       
    38         return geom 
       
    39      
       
    40     def __set__(self, obj, value):
       
    41         """
       
    42         This accessor sets the proxied geometry with the geometry class
       
    43         specified during initialization.  Values of None, HEXEWKB, or WKT may
       
    44         be used to set the geometry as well.
       
    45         """
       
    46         # The OGC Geometry type of the field.
       
    47         gtype = self._field._geom
       
    48         
       
    49         # The geometry type must match that of the field -- unless the
       
    50         # general GeometryField is used.
       
    51         if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
       
    52             # Assigning the SRID to the geometry.
       
    53             if value.srid is None: value.srid = self._field._srid
       
    54         elif isinstance(value, (NoneType, StringType, UnicodeType)):
       
    55             # Set with None, WKT, or HEX
       
    56             pass
       
    57         else:
       
    58             raise TypeError('cannot set %s GeometryProxy with value of type: %s' % (obj.__class__.__name__, type(value)))
       
    59 
       
    60         # Setting the objects dictionary with the value, and returning.
       
    61         obj.__dict__[self._field.attname] = value 
       
    62         return value