2013-11-03 16:52:40 +08:00
|
|
|
from django.db.models.sql.aggregates import Aggregate, Count
|
2009-01-15 19:06:34 +08:00
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-11-03 16:52:40 +08:00
|
|
|
__all__ = ['Count', 'Collect', 'Extent', 'Extent3D', 'MakeLine', 'Union']
|
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class GeoAggregate(Aggregate):
|
2009-12-22 23:18:51 +08:00
|
|
|
# Default SQL template for spatial aggregates.
|
|
|
|
sql_template = '%(function)s(%(field)s)'
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2009-01-16 03:35:04 +08:00
|
|
|
# Conversion class, if necessary.
|
|
|
|
conversion_class = None
|
|
|
|
|
|
|
|
# Flags for indicating the type of the aggregate.
|
2009-01-15 19:06:34 +08:00
|
|
|
is_extent = False
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def __init__(self, col, source=None, is_summary=False, tolerance=0.05, **extra):
|
2009-01-15 19:06:34 +08:00
|
|
|
super(GeoAggregate, self).__init__(col, source, is_summary, **extra)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
# Required by some Oracle aggregates.
|
|
|
|
self.tolerance = tolerance
|
2009-01-16 03:35:04 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
# Can't use geographic aggregates on non-geometry fields.
|
2009-09-14 01:40:46 +08:00
|
|
|
if not isinstance(self.source, GeometryField):
|
2009-01-15 19:06:34 +08:00
|
|
|
raise ValueError('Geospatial aggregates only allowed on geometry fields.')
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def as_sql(self, qn, connection):
|
2013-02-10 23:15:49 +08:00
|
|
|
"Return the aggregate, rendered as SQL with parameters."
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
if connection.ops.oracle:
|
|
|
|
self.extra['tolerance'] = self.tolerance
|
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
params = []
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
if hasattr(self.col, 'as_sql'):
|
2013-02-10 23:15:49 +08:00
|
|
|
field_name, params = self.col.as_sql(qn, connection)
|
2009-12-22 23:18:51 +08:00
|
|
|
elif isinstance(self.col, (list, tuple)):
|
2013-08-30 07:20:00 +08:00
|
|
|
field_name = '.'.join(qn(c) for c in self.col)
|
2009-12-22 23:18:51 +08:00
|
|
|
else:
|
|
|
|
field_name = self.col
|
|
|
|
|
|
|
|
sql_template, sql_function = connection.ops.spatial_aggregate_sql(self)
|
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
substitutions = {
|
2009-12-22 23:18:51 +08:00
|
|
|
'function': sql_function,
|
|
|
|
'field': field_name
|
|
|
|
}
|
2013-02-10 23:15:49 +08:00
|
|
|
substitutions.update(self.extra)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
return sql_template % substitutions, params
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
class Collect(GeoAggregate):
|
2009-12-22 23:18:51 +08:00
|
|
|
pass
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Extent(GeoAggregate):
|
2009-11-17 02:49:00 +08:00
|
|
|
is_extent = '2D'
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-11-17 02:49:00 +08:00
|
|
|
class Extent3D(GeoAggregate):
|
|
|
|
is_extent = '3D'
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class MakeLine(GeoAggregate):
|
2009-12-22 23:18:51 +08:00
|
|
|
pass
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2009-01-15 19:06:34 +08:00
|
|
|
class Union(GeoAggregate):
|
2009-12-22 23:18:51 +08:00
|
|
|
pass
|