~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/contrib/gis/db/models/sql/aggregates.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
 
 
6
# Default SQL template for spatial aggregates.
 
7
geo_template = '%(function)s(%(field)s)'
 
8
 
 
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.')
 
13
 
 
14
def convert_geom(wkt, geo_field):
 
15
    raise NotImplementedError('Aggregate method not implemented for this spatial backend.')
 
16
 
 
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)
 
25
 
 
26
    def convert_geom(hex, geo_field):
 
27
        if hex: return SpatialBackend.Geometry(hex)
 
28
        else: return None
 
29
elif SpatialBackend.oracle:
 
30
    # Oracle spatial aggregates need a tolerance.
 
31
    geo_template = '%(function)s(SDOAGGRTYPE(%(field)s,%(tolerance)s))'
 
32
 
 
33
    def convert_extent(clob):
 
34
        if 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())
 
38
            shell = poly.shell
 
39
            ll, ur = shell[0], shell[2]
 
40
            xmin, ymin = ll
 
41
            xmax, ymax = ur
 
42
            return (xmin, ymin, xmax, ymax)
 
43
        else:
 
44
            return None
 
45
    
 
46
    def convert_geom(clob, geo_field):
 
47
        if clob: 
 
48
            return SpatialBackend.Geometry(clob.read(), geo_field.srid)
 
49
        else:
 
50
            return None
 
51
elif SpatialBackend.spatialite:
 
52
    # SpatiaLite returns WKT.
 
53
    def convert_geom(wkt, geo_field):
 
54
        if wkt:
 
55
            return SpatialBackend.Geometry(wkt, geo_field.srid)
 
56
        else:
 
57
            return None
 
58
 
 
59
class GeoAggregate(Aggregate):
 
60
    # Overriding the SQL template with the geographic one.
 
61
    sql_template = geo_template
 
62
 
 
63
    # Conversion class, if necessary.
 
64
    conversion_class = None
 
65
 
 
66
    # Flags for indicating the type of the aggregate.
 
67
    is_extent = False
 
68
 
 
69
    def __init__(self, col, source=None, is_summary=False, **extra):
 
70
        super(GeoAggregate, self).__init__(col, source, is_summary, **extra)
 
71
 
 
72
        if not self.is_extent and SpatialBackend.oracle:
 
73
            self.extra.setdefault('tolerance', 0.05)
 
74
 
 
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.')
 
78
 
 
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.')
 
82
 
 
83
class Collect(GeoAggregate):
 
84
    conversion_class = GeomField
 
85
    sql_function = SpatialBackend.collect
 
86
 
 
87
class Extent(GeoAggregate):
 
88
    is_extent = True
 
89
    sql_function = SpatialBackend.extent
 
90
        
 
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)'
 
95
 
 
96
class MakeLine(GeoAggregate):
 
97
    conversion_class = GeomField
 
98
    sql_function = SpatialBackend.make_line
 
99
 
 
100
class Union(GeoAggregate):
 
101
    conversion_class = GeomField
 
102
    sql_function = SpatialBackend.unionagg