2014-05-23 03:51:30 +08:00
|
|
|
import re
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
from django.contrib.gis.db.models.fields import BaseSpatialField
|
2015-03-17 08:38:55 +08:00
|
|
|
from django.db.models.expressions import Col, Expression
|
2016-12-01 00:22:56 +08:00
|
|
|
from django.db.models.lookups import Lookup, Transform
|
2016-10-28 23:20:23 +08:00
|
|
|
from django.db.models.sql.query import Query
|
2014-05-23 03:51:30 +08:00
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
|
2016-04-22 00:03:14 +08:00
|
|
|
class RasterBandTransform(Transform):
|
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
return compiler.compile(self.lhs)
|
|
|
|
|
|
|
|
|
2014-01-18 17:09:43 +08:00
|
|
|
class GISLookup(Lookup):
|
2014-05-23 03:51:30 +08:00
|
|
|
sql_template = None
|
|
|
|
transform_func = None
|
|
|
|
distance = False
|
2016-04-22 00:03:14 +08:00
|
|
|
band_rhs = None
|
|
|
|
band_lhs = None
|
2014-05-23 03:51:30 +08:00
|
|
|
|
2015-10-07 04:05:53 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2015-10-07 04:05:53 +08:00
|
|
|
self.template_params = {}
|
|
|
|
|
2016-04-22 00:03:14 +08:00
|
|
|
def process_band_indices(self, only_lhs=False):
|
|
|
|
"""
|
|
|
|
Extract the lhs band index from the band transform class and the rhs
|
|
|
|
band index from the input tuple.
|
|
|
|
"""
|
|
|
|
# PostGIS band indices are 1-based, so the band index needs to be
|
|
|
|
# increased to be consistent with the GDALRaster band indices.
|
|
|
|
if only_lhs:
|
|
|
|
self.band_rhs = 1
|
|
|
|
self.band_lhs = self.lhs.band_index + 1
|
|
|
|
return
|
|
|
|
|
|
|
|
if isinstance(self.lhs, RasterBandTransform):
|
|
|
|
self.band_lhs = self.lhs.band_index + 1
|
|
|
|
else:
|
|
|
|
self.band_lhs = 1
|
|
|
|
|
|
|
|
self.band_rhs = self.rhs[1]
|
|
|
|
if len(self.rhs) == 1:
|
|
|
|
self.rhs = self.rhs[0]
|
|
|
|
else:
|
|
|
|
self.rhs = (self.rhs[0], ) + self.rhs[2:]
|
|
|
|
|
2014-05-23 03:51:30 +08:00
|
|
|
def get_db_prep_lookup(self, value, connection):
|
|
|
|
# get_db_prep_lookup is called by process_rhs from super class
|
|
|
|
if isinstance(value, (tuple, list)):
|
|
|
|
# First param is assumed to be the geometric object
|
|
|
|
params = [connection.ops.Adapter(value[0])] + list(value)[1:]
|
|
|
|
else:
|
|
|
|
params = [connection.ops.Adapter(value)]
|
|
|
|
return ('%s', params)
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def process_rhs(self, compiler, connection):
|
2016-10-28 23:20:23 +08:00
|
|
|
if isinstance(self.rhs, Query):
|
|
|
|
# If rhs is some Query, don't touch it.
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().process_rhs(compiler, connection)
|
2014-05-23 03:51:30 +08:00
|
|
|
|
|
|
|
geom = self.rhs
|
2013-12-25 21:13:18 +08:00
|
|
|
if isinstance(self.rhs, Col):
|
2014-01-18 17:09:43 +08:00
|
|
|
# Make sure the F Expression destination field exists, and
|
|
|
|
# set an `srid` attribute with the same as that of the
|
|
|
|
# destination.
|
2013-12-25 21:13:18 +08:00
|
|
|
geo_fld = self.rhs.output_field
|
|
|
|
if not hasattr(geo_fld, 'srid'):
|
2014-01-18 17:09:43 +08:00
|
|
|
raise ValueError('No geographic field found in expression.')
|
|
|
|
self.rhs.srid = geo_fld.srid
|
2017-04-11 01:26:26 +08:00
|
|
|
sql, _ = compiler.compile(geom)
|
|
|
|
return connection.ops.get_geom_placeholder(self.lhs.output_field, geom, compiler) % sql, []
|
2015-03-17 08:38:55 +08:00
|
|
|
elif isinstance(self.rhs, Expression):
|
2016-04-22 00:03:14 +08:00
|
|
|
raise ValueError('Complex expressions not supported for spatial fields.')
|
2014-05-23 03:51:30 +08:00
|
|
|
elif isinstance(self.rhs, (list, tuple)):
|
|
|
|
geom = self.rhs[0]
|
2016-04-22 00:03:14 +08:00
|
|
|
# Check if a band index was passed in the query argument.
|
|
|
|
if ((len(self.rhs) == 2 and not self.lookup_name == 'relate') or
|
|
|
|
(len(self.rhs) == 3 and self.lookup_name == 'relate')):
|
|
|
|
self.process_band_indices()
|
|
|
|
elif len(self.rhs) > 2:
|
|
|
|
raise ValueError('Tuple too long for lookup %s.' % self.lookup_name)
|
|
|
|
elif isinstance(self.lhs, RasterBandTransform):
|
|
|
|
self.process_band_indices(only_lhs=True)
|
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
rhs, rhs_params = super().process_rhs(compiler, connection)
|
2014-11-16 09:56:42 +08:00
|
|
|
rhs = connection.ops.get_geom_placeholder(self.lhs.output_field, geom, compiler)
|
2014-05-23 03:51:30 +08:00
|
|
|
return rhs, rhs_params
|
|
|
|
|
2015-09-23 15:25:08 +08:00
|
|
|
def get_rhs_op(self, connection, rhs):
|
|
|
|
# Unlike BuiltinLookup, the GIS get_rhs_op() implementation should return
|
|
|
|
# an object (SpatialOperator) with an as_sql() method to allow for more
|
|
|
|
# complex computations (where the lhs part can be mixed in).
|
|
|
|
return connection.ops.gis_operators[self.lookup_name]
|
|
|
|
|
2014-11-16 09:56:42 +08:00
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
lhs_sql, sql_params = self.process_lhs(compiler, connection)
|
|
|
|
rhs_sql, rhs_params = self.process_rhs(compiler, connection)
|
2014-05-23 03:51:30 +08:00
|
|
|
sql_params.extend(rhs_params)
|
|
|
|
|
2015-10-07 04:05:53 +08:00
|
|
|
template_params = {'lhs': lhs_sql, 'rhs': rhs_sql, 'value': '%s'}
|
|
|
|
template_params.update(self.template_params)
|
2015-09-23 15:25:08 +08:00
|
|
|
rhs_op = self.get_rhs_op(connection, rhs_sql)
|
|
|
|
return rhs_op.as_sql(connection, self, template_params, sql_params)
|
2014-05-23 03:51:30 +08:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------
|
|
|
|
# Geometry operators
|
|
|
|
# ------------------
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class OverlapsLeftLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The overlaps_left operator returns true if A's bounding box overlaps or is to the
|
|
|
|
left of B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'overlaps_left'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class OverlapsRightLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'overlaps_right' operator returns true if A's bounding box overlaps or is to the
|
|
|
|
right of B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'overlaps_right'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class OverlapsBelowLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'overlaps_below' operator returns true if A's bounding box overlaps or is below
|
|
|
|
B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'overlaps_below'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class OverlapsAboveLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'overlaps_above' operator returns true if A's bounding box overlaps or is above
|
|
|
|
B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'overlaps_above'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class LeftLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'left' operator returns true if A's bounding box is strictly to the left
|
|
|
|
of B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'left'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class RightLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'right' operator returns true if A's bounding box is strictly to the right
|
|
|
|
of B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'right'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class StrictlyBelowLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'strictly_below' operator returns true if A's bounding box is strictly below B's
|
|
|
|
bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'strictly_below'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class StrictlyAboveLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'strictly_above' operator returns true if A's bounding box is strictly above B's
|
|
|
|
bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'strictly_above'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class SameAsLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The "~=" operator is the "same as" operator. It tests actual geometric
|
|
|
|
equality of two features. So if A and B are the same feature,
|
|
|
|
vertex-by-vertex, the operator returns true.
|
|
|
|
"""
|
|
|
|
lookup_name = 'same_as'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
BaseSpatialField.register_lookup(SameAsLookup, 'exact')
|
2014-05-23 03:51:30 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class BBContainsLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'bbcontains' operator returns true if A's bounding box completely contains
|
|
|
|
by B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'bbcontains'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class BBOverlapsLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'bboverlaps' operator returns true if A's bounding box overlaps B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'bboverlaps'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class ContainedLookup(GISLookup):
|
|
|
|
"""
|
|
|
|
The 'contained' operator returns true if A's bounding box is completely contained
|
|
|
|
by B's bounding box.
|
|
|
|
"""
|
|
|
|
lookup_name = 'contained'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2014-05-23 03:51:30 +08:00
|
|
|
# ------------------
|
|
|
|
# Geometry functions
|
|
|
|
# ------------------
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class ContainsLookup(GISLookup):
|
|
|
|
lookup_name = 'contains'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class ContainsProperlyLookup(GISLookup):
|
|
|
|
lookup_name = 'contains_properly'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class CoveredByLookup(GISLookup):
|
|
|
|
lookup_name = 'coveredby'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class CoversLookup(GISLookup):
|
|
|
|
lookup_name = 'covers'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class CrossesLookup(GISLookup):
|
|
|
|
lookup_name = 'crosses'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class DisjointLookup(GISLookup):
|
|
|
|
lookup_name = 'disjoint'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class EqualsLookup(GISLookup):
|
|
|
|
lookup_name = 'equals'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class IntersectsLookup(GISLookup):
|
|
|
|
lookup_name = 'intersects'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class OverlapsLookup(GISLookup):
|
|
|
|
lookup_name = 'overlaps'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class RelateLookup(GISLookup):
|
|
|
|
lookup_name = 'relate'
|
|
|
|
sql_template = '%(func)s(%(lhs)s, %(rhs)s, %%s)'
|
|
|
|
pattern_regex = re.compile(r'^[012TF\*]{9}$')
|
|
|
|
|
|
|
|
def get_db_prep_lookup(self, value, connection):
|
|
|
|
if len(value) != 2:
|
|
|
|
raise ValueError('relate must be passed a two-tuple')
|
|
|
|
# Check the pattern argument
|
|
|
|
backend_op = connection.ops.gis_operators[self.lookup_name]
|
|
|
|
if hasattr(backend_op, 'check_relate_argument'):
|
|
|
|
backend_op.check_relate_argument(value[1])
|
|
|
|
else:
|
|
|
|
pattern = value[1]
|
2016-12-29 23:27:49 +08:00
|
|
|
if not isinstance(pattern, str) or not self.pattern_regex.match(pattern):
|
2014-05-23 03:51:30 +08:00
|
|
|
raise ValueError('Invalid intersection matrix pattern "%s".' % pattern)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_db_prep_lookup(value, connection)
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class TouchesLookup(GISLookup):
|
|
|
|
lookup_name = 'touches'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class WithinLookup(GISLookup):
|
|
|
|
lookup_name = 'within'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2014-05-23 03:51:30 +08:00
|
|
|
class DistanceLookupBase(GISLookup):
|
|
|
|
distance = True
|
2015-10-07 04:05:53 +08:00
|
|
|
sql_template = '%(func)s(%(lhs)s, %(rhs)s) %(op)s %(value)s'
|
2014-05-23 03:51:30 +08:00
|
|
|
|
2015-10-07 04:05:53 +08:00
|
|
|
def process_rhs(self, compiler, connection):
|
2016-04-22 00:03:14 +08:00
|
|
|
if not isinstance(self.rhs, (tuple, list)) or not 2 <= len(self.rhs) <= 4:
|
|
|
|
raise ValueError("2, 3, or 4-element tuple required for '%s' lookup." % self.lookup_name)
|
|
|
|
elif len(self.rhs) == 4 and not self.rhs[3] == 'spheroid':
|
|
|
|
raise ValueError("For 4-element tuples the last argument must be the 'speroid' directive.")
|
|
|
|
|
|
|
|
# Check if the second parameter is a band index.
|
|
|
|
if len(self.rhs) > 2 and not self.rhs[2] == 'spheroid':
|
|
|
|
self.process_band_indices()
|
|
|
|
|
2015-10-07 04:05:53 +08:00
|
|
|
params = [connection.ops.Adapter(self.rhs[0])]
|
2016-04-22 00:03:14 +08:00
|
|
|
|
2015-10-07 04:05:53 +08:00
|
|
|
# Getting the distance parameter in the units of the field.
|
|
|
|
dist_param = self.rhs[1]
|
|
|
|
if hasattr(dist_param, 'resolve_expression'):
|
|
|
|
dist_param = dist_param.resolve_expression(compiler.query)
|
|
|
|
sql, expr_params = compiler.compile(dist_param)
|
|
|
|
self.template_params['value'] = sql
|
|
|
|
params.extend(expr_params)
|
2014-05-23 03:51:30 +08:00
|
|
|
else:
|
2015-10-07 04:05:53 +08:00
|
|
|
params += connection.ops.get_distance(
|
|
|
|
self.lhs.output_field, (dist_param,) + self.rhs[2:],
|
2017-02-11 18:47:20 +08:00
|
|
|
self.lookup_name,
|
2015-10-07 04:05:53 +08:00
|
|
|
)
|
|
|
|
rhs = connection.ops.get_geom_placeholder(self.lhs.output_field, params[0], compiler)
|
|
|
|
return (rhs, params)
|
2014-05-23 03:51:30 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class DWithinLookup(DistanceLookupBase):
|
|
|
|
lookup_name = 'dwithin'
|
|
|
|
sql_template = '%(func)s(%(lhs)s, %(rhs)s, %%s)'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class DistanceGTLookup(DistanceLookupBase):
|
|
|
|
lookup_name = 'distance_gt'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class DistanceGTELookup(DistanceLookupBase):
|
|
|
|
lookup_name = 'distance_gte'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class DistanceLTLookup(DistanceLookupBase):
|
|
|
|
lookup_name = 'distance_lt'
|
2016-11-13 01:11:23 +08:00
|
|
|
|
|
|
|
|
2017-03-28 10:20:18 +08:00
|
|
|
@BaseSpatialField.register_lookup
|
2014-05-23 03:51:30 +08:00
|
|
|
class DistanceLTELookup(DistanceLookupBase):
|
|
|
|
lookup_name = 'distance_lte'
|