| 1 |
from django.contrib.gis.db.models.fields import BaseSpatialField |
| 2 |
from django.contrib.gis.measure import Distance |
| 3 |
from django.db import NotSupportedError |
| 4 |
from django.db.models import Expression, Lookup, Transform |
| 5 |
from django.db.models.sql.query import Query |
| 6 |
from django.utils.regex_helper import _lazy_re_compile |
| 7 |
|
| 8 |
|
| 9 |
class RasterBandTransform(Transform): |
| 10 |
def as_sql(self, compiler, connection): |
| 11 |
return compiler.compile(self.lhs) |
| 12 |
|
| 13 |
|
| 14 |
class GISLookup(Lookup): |
| 15 |
sql_template = None |
| 16 |
transform_func = None |
| 17 |
distance = False |
| 18 |
band_rhs = None |
| 19 |
band_lhs = None |
| 20 |
|
| 21 |
def __init__(self, lhs, rhs): |
| 22 |
rhs, *self.rhs_params = rhs if isinstance(rhs, (list, tuple)) else [rhs] |
| 23 |
super().__init__(lhs, rhs) |
| 24 |
self.template_params = {} |
| 25 |
self.process_rhs_params() |
| 26 |
|
| 27 |
def process_rhs_params(self): |
| 28 |
if self.rhs_params: |
| 29 |
# Check if a band index was passed in the query argument. |
| 30 |
if len(self.rhs_params) == (2 if self.lookup_name == "relate" else 1): |
| 31 |
self.process_band_indices() |
| 32 |
elif len(self.rhs_params) > 1: |
| 33 |
raise ValueError("Tuple too long for lookup %s." % self.lookup_name) |
| 34 |
elif isinstance(self.lhs, RasterBandTransform): |
| 35 |
self.process_band_indices(only_lhs=True) |
| 36 |
|
| 37 |
def process_band_indices(self, only_lhs=False): |
| 38 |
""" |
| 39 |
Extract the lhs band index from the band transform class and the rhs |
| 40 |
band index from the input tuple. |
| 41 |
""" |
| 42 |
# PostGIS band indices are 1-based, so the band index needs to be |
| 43 |
# increased to be consistent with the GDALRaster band indices. |
| 44 |
if only_lhs: |
| 45 |
self.band_rhs = 1 |
| 46 |
self.band_lhs = self.lhs.band_index + 1 |
| 47 |
return |
| 48 |
|
| 49 |
if isinstance(self.lhs, RasterBandTransform): |
| 50 |
self.band_lhs = self.lhs.band_index + 1 |
| 51 |
else: |
| 52 |
self.band_lhs = 1 |
| 53 |
|
| 54 |
self.band_rhs, *self.rhs_params = self.rhs_params |
| 55 |
|
| 56 |
def get_db_prep_lookup(self, value, connection): |
| 57 |
# get_db_prep_lookup is called by process_rhs from super class |
| 58 |
return ("%s", [connection.ops.Adapter(value)]) |
| 59 |
|
| 60 |
def process_rhs(self, compiler, connection): |
| 61 |
if isinstance(self.rhs, Query): |
| 62 |
# If rhs is some Query, don't touch it. |
| 63 |
return super().process_rhs(compiler, connection) |
| 64 |
if isinstance(self.rhs, Expression): |
| 65 |
self.rhs = self.rhs.resolve_expression(compiler.query) |
| 66 |
rhs, rhs_params = super().process_rhs(compiler, connection) |
| 67 |
placeholder = connection.ops.get_geom_placeholder( |
| 68 |
self.lhs.output_field, self.rhs, compiler |
| 69 |
) |
| 70 |
return placeholder % rhs, rhs_params |
| 71 |
|
| 72 |
def get_rhs_op(self, connection, rhs): |
| 73 |
# Unlike BuiltinLookup, the GIS get_rhs_op() implementation should return |
| 74 |
# an object (SpatialOperator) with an as_sql() method to allow for more |
| 75 |
# complex computations (where the lhs part can be mixed in). |
| 76 |
return connection.ops.gis_operators[self.lookup_name] |
| 77 |
|
| 78 |
def as_sql(self, compiler, connection): |
| 79 |
lhs_sql, lhs_params = self.process_lhs(compiler, connection) |
| 80 |
rhs_sql, rhs_params = self.process_rhs(compiler, connection) |
| 81 |
sql_params = (*lhs_params, *rhs_params) |
| 82 |
|
| 83 |
template_params = { |
| 84 |
"lhs": lhs_sql, |
| 85 |
"rhs": rhs_sql, |
| 86 |
"value": "%s", |
| 87 |
**self.template_params, |
| 88 |
} |
| 89 |
rhs_op = self.get_rhs_op(connection, rhs_sql) |
| 90 |
return rhs_op.as_sql(connection, self, template_params, sql_params) |
| 91 |
|
| 92 |
|
| 93 |
# ------------------ |
| 94 |
# Geometry operators |
| 95 |
# ------------------ |
| 96 |
|
| 97 |
|
| 98 |
@BaseSpatialField.register_lookup |
| 99 |
class OverlapsLeftLookup(GISLookup): |
| 100 |
""" |
| 101 |
The overlaps_left operator returns true if A's bounding box overlaps or is to the |
| 102 |
left of B's bounding box. |
| 103 |
""" |
| 104 |
|
| 105 |
lookup_name = "overlaps_left" |
| 106 |
|
| 107 |
|
| 108 |
@BaseSpatialField.register_lookup |
| 109 |
class OverlapsRightLookup(GISLookup): |
| 110 |
""" |
| 111 |
The 'overlaps_right' operator returns true if A's bounding box overlaps or is to the |
| 112 |
right of B's bounding box. |
| 113 |
""" |
| 114 |
|
| 115 |
lookup_name = "overlaps_right" |
| 116 |
|
| 117 |
|
| 118 |
@BaseSpatialField.register_lookup |
| 119 |
class OverlapsBelowLookup(GISLookup): |
| 120 |
""" |
| 121 |
The 'overlaps_below' operator returns true if A's bounding box overlaps or is below |
| 122 |
B's bounding box. |
| 123 |
""" |
| 124 |
|
| 125 |
lookup_name = "overlaps_below" |
| 126 |
|
| 127 |
|
| 128 |
@BaseSpatialField.register_lookup |
| 129 |
class OverlapsAboveLookup(GISLookup): |
| 130 |
""" |
| 131 |
The 'overlaps_above' operator returns true if A's bounding box overlaps or is above |
| 132 |
B's bounding box. |
| 133 |
""" |
| 134 |
|
| 135 |
lookup_name = "overlaps_above" |
| 136 |
|
| 137 |
|
| 138 |
@BaseSpatialField.register_lookup |
| 139 |
class LeftLookup(GISLookup): |
| 140 |
""" |
| 141 |
The 'left' operator returns true if A's bounding box is strictly to the left |
| 142 |
of B's bounding box. |
| 143 |
""" |
| 144 |
|
| 145 |
lookup_name = "left" |
| 146 |
|
| 147 |
|
| 148 |
@BaseSpatialField.register_lookup |
| 149 |
class RightLookup(GISLookup): |
| 150 |
""" |
| 151 |
The 'right' operator returns true if A's bounding box is strictly to the right |
| 152 |
of B's bounding box. |
| 153 |
""" |
| 154 |
|
| 155 |
lookup_name = "right" |
| 156 |
|
| 157 |
|
| 158 |
@BaseSpatialField.register_lookup |
| 159 |
class StrictlyBelowLookup(GISLookup): |
| 160 |
""" |
| 161 |
The 'strictly_below' operator returns true if A's bounding box is strictly below B's |
| 162 |
bounding box. |
| 163 |
""" |
| 164 |
|
| 165 |
lookup_name = "strictly_below" |
| 166 |
|
| 167 |
|
| 168 |
@BaseSpatialField.register_lookup |
| 169 |
class StrictlyAboveLookup(GISLookup): |
| 170 |
""" |
| 171 |
The 'strictly_above' operator returns true if A's bounding box is strictly above B's |
| 172 |
bounding box. |
| 173 |
""" |
| 174 |
|
| 175 |
lookup_name = "strictly_above" |
| 176 |
|
| 177 |
|
| 178 |
@BaseSpatialField.register_lookup |
| 179 |
class SameAsLookup(GISLookup): |
| 180 |
""" |
| 181 |
The "~=" operator is the "same as" operator. It tests actual geometric |
| 182 |
equality of two features. So if A and B are the same feature, |
| 183 |
vertex-by-vertex, the operator returns true. |
| 184 |
""" |
| 185 |
|
| 186 |
lookup_name = "same_as" |
| 187 |
|
| 188 |
|
| 189 |
BaseSpatialField.register_lookup(SameAsLookup, "exact") |
| 190 |
|
| 191 |
|
| 192 |
@BaseSpatialField.register_lookup |
| 193 |
class BBContainsLookup(GISLookup): |
| 194 |
""" |
| 195 |
The 'bbcontains' operator returns true if A's bounding box completely contains |
| 196 |
by B's bounding box. |
| 197 |
""" |
| 198 |
|
| 199 |
lookup_name = "bbcontains" |
| 200 |
|
| 201 |
|
| 202 |
@BaseSpatialField.register_lookup |
| 203 |
class BBOverlapsLookup(GISLookup): |
| 204 |
""" |
| 205 |
The 'bboverlaps' operator returns true if A's bounding box overlaps B's |
| 206 |
bounding box. |
| 207 |
""" |
| 208 |
|
| 209 |
lookup_name = "bboverlaps" |
| 210 |
|
| 211 |
|
| 212 |
@BaseSpatialField.register_lookup |
| 213 |
class ContainedLookup(GISLookup): |
| 214 |
""" |
| 215 |
The 'contained' operator returns true if A's bounding box is completely contained |
| 216 |
by B's bounding box. |
| 217 |
""" |
| 218 |
|
| 219 |
lookup_name = "contained" |
| 220 |
|
| 221 |
|
| 222 |
# ------------------ |
| 223 |
# Geometry functions |
| 224 |
# ------------------ |
| 225 |
|
| 226 |
|
| 227 |
@BaseSpatialField.register_lookup |
| 228 |
class ContainsLookup(GISLookup): |
| 229 |
lookup_name = "contains" |
| 230 |
|
| 231 |
|
| 232 |
@BaseSpatialField.register_lookup |
| 233 |
class ContainsProperlyLookup(GISLookup): |
| 234 |
lookup_name = "contains_properly" |
| 235 |
|
| 236 |
|
| 237 |
@BaseSpatialField.register_lookup |
| 238 |
class CoveredByLookup(GISLookup): |
| 239 |
lookup_name = "coveredby" |
| 240 |
|
| 241 |
|
| 242 |
@BaseSpatialField.register_lookup |
| 243 |
class CoversLookup(GISLookup): |
| 244 |
lookup_name = "covers" |
| 245 |
|
| 246 |
|
| 247 |
@BaseSpatialField.register_lookup |
| 248 |
class CrossesLookup(GISLookup): |
| 249 |
lookup_name = "crosses" |
| 250 |
|
| 251 |
|
| 252 |
@BaseSpatialField.register_lookup |
| 253 |
class DisjointLookup(GISLookup): |
| 254 |
lookup_name = "disjoint" |
| 255 |
|
| 256 |
|
| 257 |
@BaseSpatialField.register_lookup |
| 258 |
class EqualsLookup(GISLookup): |
| 259 |
lookup_name = "equals" |
| 260 |
|
| 261 |
|
| 262 |
@BaseSpatialField.register_lookup |
| 263 |
class IntersectsLookup(GISLookup): |
| 264 |
lookup_name = "intersects" |
| 265 |
|
| 266 |
|
| 267 |
@BaseSpatialField.register_lookup |
| 268 |
class OverlapsLookup(GISLookup): |
| 269 |
lookup_name = "overlaps" |
| 270 |
|
| 271 |
|
| 272 |
@BaseSpatialField.register_lookup |
| 273 |
class RelateLookup(GISLookup): |
| 274 |
lookup_name = "relate" |
| 275 |
sql_template = "%(func)s(%(lhs)s, %(rhs)s, %%s)" |
| 276 |
pattern_regex = _lazy_re_compile(r"^[012TF*]{9}$") |
| 277 |
|
| 278 |
def process_rhs(self, compiler, connection): |
| 279 |
# Check the pattern argument |
| 280 |
pattern = self.rhs_params[0] |
| 281 |
backend_op = connection.ops.gis_operators[self.lookup_name] |
| 282 |
if hasattr(backend_op, "check_relate_argument"): |
| 283 |
backend_op.check_relate_argument(pattern) |
| 284 |
elif not isinstance(pattern, str) or not self.pattern_regex.match(pattern): |
| 285 |
raise ValueError('Invalid intersection matrix pattern "%s".' % pattern) |
| 286 |
sql, params = super().process_rhs(compiler, connection) |
| 287 |
return sql, params + [pattern] |
| 288 |
|
| 289 |
|
| 290 |
@BaseSpatialField.register_lookup |
| 291 |
class TouchesLookup(GISLookup): |
| 292 |
lookup_name = "touches" |
| 293 |
|
| 294 |
|
| 295 |
@BaseSpatialField.register_lookup |
| 296 |
class WithinLookup(GISLookup): |
| 297 |
lookup_name = "within" |
| 298 |
|
| 299 |
|
| 300 |
class DistanceLookupBase(GISLookup): |
| 301 |
distance = True |
| 302 |
sql_template = "%(func)s(%(lhs)s, %(rhs)s) %(op)s %(value)s" |
| 303 |
|
| 304 |
def process_rhs_params(self): |
| 305 |
if not 1 <= len(self.rhs_params) <= 3: |
| 306 |
raise ValueError( |
| 307 |
"2, 3, or 4-element tuple required for '%s' lookup." % self.lookup_name |
| 308 |
) |
| 309 |
elif len(self.rhs_params) == 3 and self.rhs_params[2] != "spheroid": |
| 310 |
raise ValueError( |
| 311 |
"For 4-element tuples the last argument must be the 'spheroid' " |
| 312 |
"directive." |
| 313 |
) |
| 314 |
|
| 315 |
# Check if the second parameter is a band index. |
| 316 |
if len(self.rhs_params) > 1 and self.rhs_params[1] != "spheroid": |
| 317 |
self.process_band_indices() |
| 318 |
|
| 319 |
def process_distance(self, compiler, connection): |
| 320 |
dist_param = self.rhs_params[0] |
| 321 |
return ( |
| 322 |
compiler.compile(dist_param.resolve_expression(compiler.query)) |
| 323 |
if hasattr(dist_param, "resolve_expression") |
| 324 |
else ( |
| 325 |
"%s", |
| 326 |
connection.ops.get_distance( |
| 327 |
self.lhs.output_field, self.rhs_params, self.lookup_name |
| 328 |
), |
| 329 |
) |
| 330 |
) |
| 331 |
|
| 332 |
|
| 333 |
@BaseSpatialField.register_lookup |
| 334 |
class DWithinLookup(DistanceLookupBase): |
| 335 |
lookup_name = "dwithin" |
| 336 |
sql_template = "%(func)s(%(lhs)s, %(rhs)s, %(value)s)" |
| 337 |
|
| 338 |
def process_distance(self, compiler, connection): |
| 339 |
dist_param = self.rhs_params[0] |
| 340 |
if ( |
| 341 |
not connection.features.supports_dwithin_distance_expr |
| 342 |
and hasattr(dist_param, "resolve_expression") |
| 343 |
and not isinstance(dist_param, Distance) |
| 344 |
): |
| 345 |
raise NotSupportedError( |
| 346 |
"This backend does not support expressions for specifying " |
| 347 |
"distance in the dwithin lookup." |
| 348 |
) |
| 349 |
return super().process_distance(compiler, connection) |
| 350 |
|
| 351 |
def process_rhs(self, compiler, connection): |
| 352 |
dist_sql, dist_params = self.process_distance(compiler, connection) |
| 353 |
self.template_params["value"] = dist_sql |
| 354 |
rhs_sql, params = super().process_rhs(compiler, connection) |
| 355 |
return rhs_sql, params + dist_params |
| 356 |
|
| 357 |
|
| 358 |
class DistanceLookupFromFunction(DistanceLookupBase): |
| 359 |
def as_sql(self, compiler, connection): |
| 360 |
spheroid = ( |
| 361 |
len(self.rhs_params) == 2 and self.rhs_params[-1] == "spheroid" |
| 362 |
) or None |
| 363 |
distance_expr = connection.ops.distance_expr_for_lookup( |
| 364 |
self.lhs, self.rhs, spheroid=spheroid |
| 365 |
) |
| 366 |
sql, params = compiler.compile(distance_expr.resolve_expression(compiler.query)) |
| 367 |
dist_sql, dist_params = self.process_distance(compiler, connection) |
| 368 |
return ( |
| 369 |
"%(func)s %(op)s %(dist)s" % {"func": sql, "op": self.op, "dist": dist_sql}, |
| 370 |
params + dist_params, |
| 371 |
) |
| 372 |
|
| 373 |
|
| 374 |
@BaseSpatialField.register_lookup |
| 375 |
class DistanceGTLookup(DistanceLookupFromFunction): |
| 376 |
lookup_name = "distance_gt" |
| 377 |
op = ">" |
| 378 |
|
| 379 |
|
| 380 |
@BaseSpatialField.register_lookup |
| 381 |
class DistanceGTELookup(DistanceLookupFromFunction): |
| 382 |
lookup_name = "distance_gte" |
| 383 |
op = ">=" |
| 384 |
|
| 385 |
|
| 386 |
@BaseSpatialField.register_lookup |
| 387 |
class DistanceLTLookup(DistanceLookupFromFunction): |
| 388 |
lookup_name = "distance_lt" |
| 389 |
op = "<" |
| 390 |
|
| 391 |
|
| 392 |
@BaseSpatialField.register_lookup |
| 393 |
class DistanceLTELookup(DistanceLookupFromFunction): |
| 394 |
lookup_name = "distance_lte" |
| 395 |
op = "<=" |