2010-01-22 22:30:06 +08:00
|
|
|
from django.db import connections
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.db.models.query import sql
|
|
|
|
|
|
|
|
from django.contrib.gis.db.models.fields import GeometryField
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.gis.db.models.sql import aggregates as gis_aggregates
|
2009-01-16 03:35:04 +08:00
|
|
|
from django.contrib.gis.db.models.sql.conversion import AreaField, DistanceField, GeomField
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.db.models.sql.where import GeoWhereNode
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry
|
2008-08-06 02:13:06 +08:00
|
|
|
from django.contrib.gis.measure import Area, Distance
|
|
|
|
|
|
|
|
|
2012-04-30 10:18:30 +08:00
|
|
|
ALL_TERMS = set([
|
2009-12-22 23:18:51 +08:00
|
|
|
'bbcontains', 'bboverlaps', 'contained', 'contains',
|
|
|
|
'contains_properly', 'coveredby', 'covers', 'crosses', 'disjoint',
|
|
|
|
'distance_gt', 'distance_gte', 'distance_lt', 'distance_lte',
|
|
|
|
'dwithin', 'equals', 'exact',
|
|
|
|
'intersects', 'overlaps', 'relate', 'same_as', 'touches', 'within',
|
|
|
|
'left', 'right', 'overlaps_left', 'overlaps_right',
|
|
|
|
'overlaps_above', 'overlaps_below',
|
|
|
|
'strictly_above', 'strictly_below'
|
2012-04-30 10:18:30 +08:00
|
|
|
])
|
2009-12-22 23:18:51 +08:00
|
|
|
ALL_TERMS.update(sql.constants.QUERY_TERMS)
|
2009-04-02 00:01:50 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class GeoQuery(sql.Query):
|
|
|
|
"""
|
|
|
|
A single spatial SQL query.
|
|
|
|
"""
|
|
|
|
# Overridding the valid query terms.
|
|
|
|
query_terms = ALL_TERMS
|
2009-12-22 23:18:51 +08:00
|
|
|
aggregates_module = gis_aggregates
|
|
|
|
|
|
|
|
compiler = 'GeoSQLCompiler'
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
#### Methods overridden from the base Query class ####
|
2009-12-22 23:18:51 +08:00
|
|
|
def __init__(self, model, where=GeoWhereNode):
|
|
|
|
super(GeoQuery, self).__init__(model, where)
|
2008-08-06 02:13:06 +08:00
|
|
|
# The following attributes are customized for the GeoQuerySet.
|
|
|
|
# The GeoWhereNode and SpatialBackend classes contain backend-specific
|
|
|
|
# routines and functions.
|
|
|
|
self.custom_select = {}
|
|
|
|
self.transformed_srid = None
|
|
|
|
self.extra_select_fields = {}
|
|
|
|
|
|
|
|
def clone(self, *args, **kwargs):
|
|
|
|
obj = super(GeoQuery, self).clone(*args, **kwargs)
|
|
|
|
# Customized selection dictionary and transformed srid flag have
|
|
|
|
# to also be added to obj.
|
|
|
|
obj.custom_select = self.custom_select.copy()
|
|
|
|
obj.transformed_srid = self.transformed_srid
|
|
|
|
obj.extra_select_fields = self.extra_select_fields.copy()
|
|
|
|
return obj
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def convert_values(self, value, field, connection):
|
2010-03-30 06:20:38 +08:00
|
|
|
"""
|
|
|
|
Using the same routines that Oracle does we can convert our
|
2008-08-06 02:13:06 +08:00
|
|
|
extra selection objects into Geometry and Distance objects.
|
2008-11-06 02:15:48 +08:00
|
|
|
TODO: Make converted objects 'lazy' for less overhead.
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2012-03-01 01:46:23 +08:00
|
|
|
if connection.ops.oracle:
|
|
|
|
# Running through Oracle's first.
|
2009-12-22 23:18:51 +08:00
|
|
|
value = super(GeoQuery, self).convert_values(value, field or GeomField(), connection)
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2010-03-30 06:20:38 +08:00
|
|
|
if value is None:
|
|
|
|
# Output from spatial function is NULL (e.g., called
|
|
|
|
# function on a geometry field with NULL value).
|
|
|
|
pass
|
|
|
|
elif isinstance(field, DistanceField):
|
2008-08-06 02:13:06 +08:00
|
|
|
# Using the field's distance attribute, can instantiate
|
|
|
|
# `Distance` with the right context.
|
|
|
|
value = Distance(**{field.distance_att : value})
|
|
|
|
elif isinstance(field, AreaField):
|
|
|
|
value = Area(**{field.area_att : value})
|
2009-04-02 00:01:50 +08:00
|
|
|
elif isinstance(field, (GeomField, GeometryField)) and value:
|
2009-12-22 23:18:51 +08:00
|
|
|
value = Geometry(value)
|
2012-12-26 19:45:41 +08:00
|
|
|
elif field is not None:
|
|
|
|
return super(GeoQuery, self).convert_values(value, field, connection)
|
2008-08-06 02:13:06 +08:00
|
|
|
return value
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_aggregation(self, using):
|
|
|
|
# Remove any aggregates marked for reduction from the subquery
|
|
|
|
# and move them to the outer AggregateQuery.
|
|
|
|
connection = connections[using]
|
|
|
|
for alias, aggregate in self.aggregate_select.items():
|
|
|
|
if isinstance(aggregate, gis_aggregates.GeoAggregate):
|
|
|
|
if not getattr(aggregate, 'is_extent', False) or connection.ops.oracle:
|
|
|
|
self.extra_select_fields[alias] = GeomField()
|
|
|
|
return super(GeoQuery, self).get_aggregation(using)
|
|
|
|
|
|
|
|
def resolve_aggregate(self, value, aggregate, connection):
|
2009-01-15 19:06:34 +08:00
|
|
|
"""
|
|
|
|
Overridden from GeoQuery's normalize to handle the conversion of
|
|
|
|
GeoAggregate objects.
|
|
|
|
"""
|
|
|
|
if isinstance(aggregate, self.aggregates_module.GeoAggregate):
|
|
|
|
if aggregate.is_extent:
|
2009-11-17 02:49:00 +08:00
|
|
|
if aggregate.is_extent == '3D':
|
2009-12-22 23:18:51 +08:00
|
|
|
return connection.ops.convert_extent3d(value)
|
2009-11-17 02:49:00 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
return connection.ops.convert_extent(value)
|
2009-01-15 19:06:34 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
return connection.ops.convert_geom(value, aggregate.source)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
2009-12-22 23:18:51 +08:00
|
|
|
return super(GeoQuery, self).resolve_aggregate(value, aggregate, connection)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Private API utilities, subject to change.
|
|
|
|
def _geo_field(self, field_name=None):
|
|
|
|
"""
|
|
|
|
Returns the first Geometry field encountered; or specified via the
|
|
|
|
`field_name` keyword. The `field_name` may be a string specifying
|
|
|
|
the geometry field on this GeoQuery's model, or a lookup string
|
|
|
|
to a geometry field via a ForeignKey relation.
|
|
|
|
"""
|
|
|
|
if field_name is None:
|
|
|
|
# Incrementing until the first geographic field is found.
|
|
|
|
for fld in self.model._meta.fields:
|
|
|
|
if isinstance(fld, GeometryField): return fld
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
# Otherwise, check by the given field name -- which may be
|
|
|
|
# a lookup to a _related_ geographic field.
|
2009-03-04 06:10:15 +08:00
|
|
|
return GeoWhereNode._check_geo_field(self.model._meta, field_name)
|