proxy.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. The SpatialProxy object allows for lazy-geometries and lazy-rasters. The proxy
  3. uses Python descriptors for instantiating and setting Geometry or Raster
  4. objects corresponding to geographic model fields.
  5. Thanks to Robert Coup for providing this functionality (see #4322).
  6. """
  7. from django.db.models.query_utils import DeferredAttribute
  8. class SpatialProxy(DeferredAttribute):
  9. def __init__(self, klass, field, load_func=None):
  10. """
  11. Initialize on the given Geometry or Raster class (not an instance)
  12. and the corresponding field.
  13. """
  14. self._klass = klass
  15. self._load_func = load_func or klass
  16. super().__init__(field)
  17. def __get__(self, instance, cls=None):
  18. """
  19. Retrieve the geometry or raster, initializing it using the
  20. corresponding class specified during initialization and the value of
  21. the field. Currently, GEOS or OGR geometries as well as GDALRasters are
  22. supported.
  23. """
  24. if instance is None:
  25. # Accessed on a class, not an instance
  26. return self
  27. # Getting the value of the field.
  28. try:
  29. geo_value = instance.__dict__[self.field.attname]
  30. except KeyError:
  31. geo_value = super().__get__(instance, cls)
  32. if isinstance(geo_value, self._klass):
  33. geo_obj = geo_value
  34. elif (geo_value is None) or (geo_value == ''):
  35. geo_obj = None
  36. else:
  37. # Otherwise, a geometry or raster object is built using the field's
  38. # contents, and the model's corresponding attribute is set.
  39. geo_obj = self._load_func(geo_value)
  40. setattr(instance, self.field.attname, geo_obj)
  41. return geo_obj
  42. def __set__(self, instance, value):
  43. """
  44. Retrieve the proxied geometry or raster with the corresponding class
  45. specified during initialization.
  46. To set geometries, use values of None, HEXEWKB, or WKT.
  47. To set rasters, use JSON or dict values.
  48. """
  49. # The geographic type of the field.
  50. gtype = self.field.geom_type
  51. if gtype == 'RASTER' and (value is None or isinstance(value, (str, dict, self._klass))):
  52. # For raster fields, assure input is None or a string, dict, or
  53. # raster instance.
  54. pass
  55. elif isinstance(value, self._klass):
  56. # The geometry type must match that of the field -- unless the
  57. # general GeometryField is used.
  58. if value.srid is None:
  59. # Assigning the field SRID if the geometry has no SRID.
  60. value.srid = self.field.srid
  61. elif value is None or isinstance(value, (str, memoryview)):
  62. # Set geometries with None, WKT, HEX, or WKB
  63. pass
  64. else:
  65. raise TypeError('Cannot set %s SpatialProxy (%s) with value of type: %s' % (
  66. instance.__class__.__name__, gtype, type(value)))
  67. # Setting the objects dictionary with the value, and returning.
  68. instance.__dict__[self.field.attname] = value
  69. return value