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
from django.contrib.gis.db.backend import SpatialBackend
6
# Default SQL template for spatial aggregates.
7
geo_template = '%(function)s(%(field)s)'
9
# Default conversion functions for aggregates; will be overridden if implemented
10
# for the spatial backend.
11
def convert_extent(box):
12
raise NotImplementedError('Aggregate extent not implemented for this spatial backend.')
14
def convert_geom(wkt, geo_field):
15
raise NotImplementedError('Aggregate method not implemented for this spatial backend.')
17
if SpatialBackend.postgis:
18
def convert_extent(box):
19
# Box text will be something like "BOX(-90.0 30.0, -85.0 40.0)";
20
# parsing out and returning as a 4-tuple.
21
ll, ur = box[4:-1].split(',')
22
xmin, ymin = map(float, ll.split())
23
xmax, ymax = map(float, ur.split())
24
return (xmin, ymin, xmax, ymax)
26
def convert_geom(hex, geo_field):
27
if hex: return SpatialBackend.Geometry(hex)
29
elif SpatialBackend.oracle:
30
# Oracle spatial aggregates need a tolerance.
31
geo_template = '%(function)s(SDOAGGRTYPE(%(field)s,%(tolerance)s))'
33
def convert_extent(clob):
35
# Oracle returns a polygon for the extent, we construct
36
# the 4-tuple from the coordinates in the polygon.
37
poly = SpatialBackend.Geometry(clob.read())
39
ll, ur = shell[0], shell[2]
42
return (xmin, ymin, xmax, ymax)
46
def convert_geom(clob, geo_field):
48
return SpatialBackend.Geometry(clob.read(), geo_field.srid)
51
elif SpatialBackend.spatialite:
52
# SpatiaLite returns WKT.
53
def convert_geom(wkt, geo_field):
55
return SpatialBackend.Geometry(wkt, geo_field.srid)
59
class GeoAggregate(Aggregate):
60
# Overriding the SQL template with the geographic one.
61
sql_template = geo_template
63
# Conversion class, if necessary.
64
conversion_class = None
66
# Flags for indicating the type of the aggregate.
69
def __init__(self, col, source=None, is_summary=False, **extra):
70
super(GeoAggregate, self).__init__(col, source, is_summary, **extra)
72
if not self.is_extent and SpatialBackend.oracle:
73
self.extra.setdefault('tolerance', 0.05)
75
# Can't use geographic aggregates on non-geometry fields.
76
if not isinstance(self.source, GeometryField):
77
raise ValueError('Geospatial aggregates only allowed on geometry fields.')
79
# Making sure the SQL function is available for this spatial backend.
80
if not self.sql_function:
81
raise NotImplementedError('This aggregate functionality not implemented for your spatial backend.')
83
class Collect(GeoAggregate):
84
conversion_class = GeomField
85
sql_function = SpatialBackend.collect
87
class Extent(GeoAggregate):
89
sql_function = SpatialBackend.extent
91
if SpatialBackend.oracle:
92
# Have to change Extent's attributes here for Oracle.
93
Extent.conversion_class = GeomField
94
Extent.sql_template = '%(function)s(%(field)s)'
96
class MakeLine(GeoAggregate):
97
conversion_class = GeomField
98
sql_function = SpatialBackend.make_line
100
class Union(GeoAggregate):
101
conversion_class = GeomField
102
sql_function = SpatialBackend.unionagg