PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/db/models/sql/aggregates.py

https://code.google.com/p/mango-py/
Python | 61 lines | 39 code | 17 blank | 5 comment | 6 complexity | 604c437fbccfb9af1c3590ae3a60588e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.db.models.sql.aggregates import *
  2. from django.contrib.gis.db.models.fields import GeometryField
  3. from django.contrib.gis.db.models.sql.conversion import GeomField
  4. class GeoAggregate(Aggregate):
  5. # Default SQL template for spatial aggregates.
  6. sql_template = '%(function)s(%(field)s)'
  7. # Conversion class, if necessary.
  8. conversion_class = None
  9. # Flags for indicating the type of the aggregate.
  10. is_extent = False
  11. def __init__(self, col, source=None, is_summary=False, tolerance=0.05, **extra):
  12. super(GeoAggregate, self).__init__(col, source, is_summary, **extra)
  13. # Required by some Oracle aggregates.
  14. self.tolerance = tolerance
  15. # Can't use geographic aggregates on non-geometry fields.
  16. if not isinstance(self.source, GeometryField):
  17. raise ValueError('Geospatial aggregates only allowed on geometry fields.')
  18. def as_sql(self, qn, connection):
  19. "Return the aggregate, rendered as SQL."
  20. if connection.ops.oracle:
  21. self.extra['tolerance'] = self.tolerance
  22. if hasattr(self.col, 'as_sql'):
  23. field_name = self.col.as_sql(qn, connection)
  24. elif isinstance(self.col, (list, tuple)):
  25. field_name = '.'.join([qn(c) for c in self.col])
  26. else:
  27. field_name = self.col
  28. sql_template, sql_function = connection.ops.spatial_aggregate_sql(self)
  29. params = {
  30. 'function': sql_function,
  31. 'field': field_name
  32. }
  33. params.update(self.extra)
  34. return sql_template % params
  35. class Collect(GeoAggregate):
  36. pass
  37. class Extent(GeoAggregate):
  38. is_extent = '2D'
  39. class Extent3D(GeoAggregate):
  40. is_extent = '3D'
  41. class MakeLine(GeoAggregate):
  42. pass
  43. class Union(GeoAggregate):
  44. pass