2013-12-25 21:13:18 +08:00
|
|
|
from django.db.models.aggregates import Aggregate
|
2015-01-15 03:48:55 +08:00
|
|
|
from django.contrib.gis.db.models.fields import ExtentField
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-10-18 19:25:30 +08:00
|
|
|
__all__ = ['Collect', 'Extent', 'Extent3D', 'MakeLine', 'Union']
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
class GeoAggregate(Aggregate):
|
|
|
|
function = None
|
|
|
|
is_extent = False
|
|
|
|
|
|
|
|
def as_sql(self, compiler, connection):
|
2015-01-17 13:03:46 +08:00
|
|
|
# this will be called again in parent, but it's needed now - before
|
|
|
|
# we get the spatial_aggregate_name
|
|
|
|
connection.ops.check_expression_support(self)
|
2015-01-15 18:35:35 +08:00
|
|
|
self.function = connection.ops.spatial_aggregate_name(self.name)
|
2013-12-25 21:13:18 +08:00
|
|
|
return super(GeoAggregate, self).as_sql(compiler, connection)
|
|
|
|
|
2015-01-15 18:35:35 +08:00
|
|
|
def as_oracle(self, compiler, connection):
|
|
|
|
if not hasattr(self, 'tolerance'):
|
|
|
|
self.tolerance = 0.05
|
|
|
|
self.extra['tolerance'] = self.tolerance
|
|
|
|
if not self.is_extent:
|
|
|
|
self.template = '%(function)s(SDOAGGRTYPE(%(expressions)s,%(tolerance)s))'
|
|
|
|
return self.as_sql(compiler, connection)
|
|
|
|
|
2015-01-15 03:48:55 +08:00
|
|
|
def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
|
|
|
|
c = super(GeoAggregate, self).resolve_expression(query, allow_joins, reuse, summarize, for_save)
|
|
|
|
if not hasattr(c.input_field.field, 'geom_type'):
|
2013-12-25 21:13:18 +08:00
|
|
|
raise ValueError('Geospatial aggregates only allowed on geometry fields.')
|
|
|
|
return c
|
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
def convert_value(self, value, connection, context):
|
2013-12-25 21:13:18 +08:00
|
|
|
return connection.ops.convert_geom(value, self.output_field)
|
|
|
|
|
|
|
|
|
|
|
|
class Collect(GeoAggregate):
|
2009-03-31 01:15:49 +08:00
|
|
|
name = 'Collect'
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
class Extent(GeoAggregate):
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'Extent'
|
2013-12-25 21:13:18 +08:00
|
|
|
is_extent = '2D'
|
|
|
|
|
|
|
|
def __init__(self, expression, **extra):
|
|
|
|
super(Extent, self).__init__(expression, output_field=ExtentField(), **extra)
|
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
def convert_value(self, value, connection, context):
|
|
|
|
return connection.ops.convert_extent(value, context.get('transformed_srid'))
|
2009-01-15 19:06:34 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
class Extent3D(GeoAggregate):
|
2009-11-17 02:49:00 +08:00
|
|
|
name = 'Extent3D'
|
2013-12-25 21:13:18 +08:00
|
|
|
is_extent = '3D'
|
|
|
|
|
|
|
|
def __init__(self, expression, **extra):
|
|
|
|
super(Extent3D, self).__init__(expression, output_field=ExtentField(), **extra)
|
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
def convert_value(self, value, connection, context):
|
|
|
|
return connection.ops.convert_extent3d(value, context.get('transformed_srid'))
|
2009-11-17 02:49:00 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
class MakeLine(GeoAggregate):
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'MakeLine'
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2013-12-25 21:13:18 +08:00
|
|
|
class Union(GeoAggregate):
|
2009-01-15 19:06:34 +08:00
|
|
|
name = 'Union'
|