You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aggregates.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from django.contrib.gis.db.models.fields import (
  2. ExtentField, GeometryCollectionField, GeometryField, LineStringField,
  3. )
  4. from django.db.models.aggregates import Aggregate
  5. from django.utils.functional import cached_property
  6. __all__ = ['Collect', 'Extent', 'Extent3D', 'MakeLine', 'Union']
  7. class GeoAggregate(Aggregate):
  8. function = None
  9. is_extent = False
  10. @cached_property
  11. def output_field(self):
  12. return self.output_field_class(self.source_expressions[0].output_field.srid)
  13. def as_sql(self, compiler, connection, function=None, **extra_context):
  14. # this will be called again in parent, but it's needed now - before
  15. # we get the spatial_aggregate_name
  16. connection.ops.check_expression_support(self)
  17. return super().as_sql(
  18. compiler,
  19. connection,
  20. function=function or connection.ops.spatial_aggregate_name(self.name),
  21. **extra_context
  22. )
  23. def as_oracle(self, compiler, connection, **extra_context):
  24. tolerance = self.extra.get('tolerance') or getattr(self, 'tolerance', 0.05)
  25. template = None if self.is_extent else '%(function)s(SDOAGGRTYPE(%(expressions)s,%(tolerance)s))'
  26. return self.as_sql(compiler, connection, template=template, tolerance=tolerance, **extra_context)
  27. def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
  28. c = super().resolve_expression(query, allow_joins, reuse, summarize, for_save)
  29. for expr in c.get_source_expressions():
  30. if not hasattr(expr.field, 'geom_type'):
  31. raise ValueError('Geospatial aggregates only allowed on geometry fields.')
  32. return c
  33. class Collect(GeoAggregate):
  34. name = 'Collect'
  35. output_field_class = GeometryCollectionField
  36. class Extent(GeoAggregate):
  37. name = 'Extent'
  38. is_extent = '2D'
  39. def __init__(self, expression, **extra):
  40. super().__init__(expression, output_field=ExtentField(), **extra)
  41. def convert_value(self, value, expression, connection):
  42. return connection.ops.convert_extent(value)
  43. class Extent3D(GeoAggregate):
  44. name = 'Extent3D'
  45. is_extent = '3D'
  46. def __init__(self, expression, **extra):
  47. super().__init__(expression, output_field=ExtentField(), **extra)
  48. def convert_value(self, value, expression, connection):
  49. return connection.ops.convert_extent3d(value)
  50. class MakeLine(GeoAggregate):
  51. name = 'MakeLine'
  52. output_field_class = LineStringField
  53. class Union(GeoAggregate):
  54. name = 'Union'
  55. output_field_class = GeometryField