| 1 |
import json |
| 2 |
|
| 3 |
from django.contrib.gis.db.models.fields import BaseSpatialField |
| 4 |
from django.contrib.gis.db.models.functions import Distance |
| 5 |
from django.contrib.gis.db.models.lookups import ( |
| 6 |
DistanceLookupBase, |
| 7 |
GISLookup, |
| 8 |
RasterBandTransform, |
| 9 |
) |
| 10 |
from django.contrib.gis.gdal import GDALRaster |
| 11 |
from django.contrib.gis.geos import GEOSGeometry |
| 12 |
from django.contrib.gis.measure import D |
| 13 |
from django.contrib.gis.shortcuts import numpy |
| 14 |
from django.db import connection |
| 15 |
from django.db.models import F, Func, Q |
| 16 |
from django.test import TransactionTestCase, skipUnlessDBFeature |
| 17 |
from django.test.utils import CaptureQueriesContext |
| 18 |
|
| 19 |
from ..data.rasters.textrasters import JSON_RASTER |
| 20 |
from .models import RasterModel, RasterRelatedModel |
| 21 |
|
| 22 |
|
| 23 |
@skipUnlessDBFeature("supports_raster") |
| 24 |
class RasterFieldTest(TransactionTestCase): |
| 25 |
available_apps = ["gis_tests.rasterapp"] |
| 26 |
|
| 27 |
def setUp(self): |
| 28 |
rast = GDALRaster( |
| 29 |
{ |
| 30 |
"srid": 4326, |
| 31 |
"origin": [0, 0], |
| 32 |
"scale": [-1, 1], |
| 33 |
"skew": [0, 0], |
| 34 |
"width": 5, |
| 35 |
"height": 5, |
| 36 |
"nr_of_bands": 2, |
| 37 |
"bands": [{"data": range(25)}, {"data": range(25, 50)}], |
| 38 |
} |
| 39 |
) |
| 40 |
model_instance = RasterModel.objects.create( |
| 41 |
rast=rast, |
| 42 |
rastprojected=rast, |
| 43 |
geom="POINT (-95.37040 29.70486)", |
| 44 |
) |
| 45 |
RasterRelatedModel.objects.create(rastermodel=model_instance) |
| 46 |
|
| 47 |
def test_field_null_value(self): |
| 48 |
""" |
| 49 |
Test creating a model where the RasterField has a null value. |
| 50 |
""" |
| 51 |
r = RasterModel.objects.create(rast=None) |
| 52 |
r.refresh_from_db() |
| 53 |
self.assertIsNone(r.rast) |
| 54 |
|
| 55 |
def test_access_band_data_directly_from_queryset(self): |
| 56 |
RasterModel.objects.create(rast=JSON_RASTER) |
| 57 |
qs = RasterModel.objects.all() |
| 58 |
qs[0].rast.bands[0].data() |
| 59 |
|
| 60 |
def test_deserialize_with_pixeltype_flags(self): |
| 61 |
no_data = 3 |
| 62 |
rast = GDALRaster( |
| 63 |
{ |
| 64 |
"srid": 4326, |
| 65 |
"origin": [0, 0], |
| 66 |
"scale": [-1, 1], |
| 67 |
"skew": [0, 0], |
| 68 |
"width": 1, |
| 69 |
"height": 1, |
| 70 |
"nr_of_bands": 1, |
| 71 |
"bands": [{"data": [no_data], "nodata_value": no_data}], |
| 72 |
} |
| 73 |
) |
| 74 |
r = RasterModel.objects.create(rast=rast) |
| 75 |
RasterModel.objects.filter(pk=r.pk).update( |
| 76 |
rast=Func(F("rast"), function="ST_SetBandIsNoData"), |
| 77 |
) |
| 78 |
r.refresh_from_db() |
| 79 |
band = r.rast.bands[0].data() |
| 80 |
if numpy: |
| 81 |
band = band.flatten().tolist() |
| 82 |
self.assertEqual(band, [no_data]) |
| 83 |
self.assertEqual(r.rast.bands[0].nodata_value, no_data) |
| 84 |
|
| 85 |
def test_model_creation(self): |
| 86 |
""" |
| 87 |
Test RasterField through a test model. |
| 88 |
""" |
| 89 |
# Create model instance from JSON raster |
| 90 |
r = RasterModel.objects.create(rast=JSON_RASTER) |
| 91 |
r.refresh_from_db() |
| 92 |
# Test raster metadata properties |
| 93 |
self.assertEqual((5, 5), (r.rast.width, r.rast.height)) |
| 94 |
self.assertEqual([0.0, -1.0, 0.0, 0.0, 0.0, 1.0], r.rast.geotransform) |
| 95 |
self.assertIsNone(r.rast.bands[0].nodata_value) |
| 96 |
# Compare srs |
| 97 |
self.assertEqual(r.rast.srs.srid, 4326) |
| 98 |
# Compare pixel values |
| 99 |
band = r.rast.bands[0].data() |
| 100 |
# If numpy, convert result to list |
| 101 |
if numpy: |
| 102 |
band = band.flatten().tolist() |
| 103 |
# Loop through rows in band data and assert single |
| 104 |
# value is as expected. |
| 105 |
self.assertEqual( |
| 106 |
[ |
| 107 |
0.0, |
| 108 |
1.0, |
| 109 |
2.0, |
| 110 |
3.0, |
| 111 |
4.0, |
| 112 |
5.0, |
| 113 |
6.0, |
| 114 |
7.0, |
| 115 |
8.0, |
| 116 |
9.0, |
| 117 |
10.0, |
| 118 |
11.0, |
| 119 |
12.0, |
| 120 |
13.0, |
| 121 |
14.0, |
| 122 |
15.0, |
| 123 |
16.0, |
| 124 |
17.0, |
| 125 |
18.0, |
| 126 |
19.0, |
| 127 |
20.0, |
| 128 |
21.0, |
| 129 |
22.0, |
| 130 |
23.0, |
| 131 |
24.0, |
| 132 |
], |
| 133 |
band, |
| 134 |
) |
| 135 |
|
| 136 |
def test_implicit_raster_transformation(self): |
| 137 |
""" |
| 138 |
Test automatic transformation of rasters with srid different from the |
| 139 |
field srid. |
| 140 |
""" |
| 141 |
# Parse json raster |
| 142 |
rast = json.loads(JSON_RASTER) |
| 143 |
# Update srid to another value |
| 144 |
rast["srid"] = 3086 |
| 145 |
# Save model and get it from db |
| 146 |
r = RasterModel.objects.create(rast=rast) |
| 147 |
r.refresh_from_db() |
| 148 |
# Confirm raster has been transformed to the default srid |
| 149 |
self.assertEqual(r.rast.srs.srid, 4326) |
| 150 |
# Confirm geotransform is in lat/lon |
| 151 |
expected = [ |
| 152 |
-87.9298551266551, |
| 153 |
9.459646421449934e-06, |
| 154 |
0.0, |
| 155 |
23.94249275457565, |
| 156 |
0.0, |
| 157 |
-9.459646421449934e-06, |
| 158 |
] |
| 159 |
for val, exp in zip(r.rast.geotransform, expected): |
| 160 |
self.assertAlmostEqual(exp, val) |
| 161 |
|
| 162 |
def test_verbose_name_arg(self): |
| 163 |
""" |
| 164 |
RasterField should accept a positional verbose name argument. |
| 165 |
""" |
| 166 |
self.assertEqual( |
| 167 |
RasterModel._meta.get_field("rast").verbose_name, "A Verbose Raster Name" |
| 168 |
) |
| 169 |
|
| 170 |
def test_all_gis_lookups_with_rasters(self): |
| 171 |
""" |
| 172 |
Evaluate all possible lookups for all input combinations (i.e. |
| 173 |
raster-raster, raster-geom, geom-raster) and for projected and |
| 174 |
unprojected coordinate systems. This test just checks that the lookup |
| 175 |
can be called, but doesn't check if the result makes logical sense. |
| 176 |
""" |
| 177 |
from django.contrib.gis.db.backends.postgis.operations import PostGISOperations |
| 178 |
|
| 179 |
# Create test raster and geom. |
| 180 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 181 |
stx_pnt = GEOSGeometry("POINT (-95.370401017314293 29.704867409475465)", 4326) |
| 182 |
stx_pnt.transform(3086) |
| 183 |
|
| 184 |
lookups = [ |
| 185 |
(name, lookup) |
| 186 |
for name, lookup in BaseSpatialField.get_lookups().items() |
| 187 |
if issubclass(lookup, GISLookup) |
| 188 |
] |
| 189 |
self.assertNotEqual(lookups, [], "No lookups found") |
| 190 |
# Loop through all the GIS lookups. |
| 191 |
for name, lookup in lookups: |
| 192 |
# Construct lookup filter strings. |
| 193 |
combo_keys = [ |
| 194 |
field + name |
| 195 |
for field in [ |
| 196 |
"rast__", |
| 197 |
"rast__", |
| 198 |
"rastprojected__0__", |
| 199 |
"rast__", |
| 200 |
"rastprojected__", |
| 201 |
"geom__", |
| 202 |
"rast__", |
| 203 |
] |
| 204 |
] |
| 205 |
if issubclass(lookup, DistanceLookupBase): |
| 206 |
# Set lookup values for distance lookups. |
| 207 |
combo_values = [ |
| 208 |
(rast, 50, "spheroid"), |
| 209 |
(rast, 0, 50, "spheroid"), |
| 210 |
(rast, 0, D(km=1)), |
| 211 |
(stx_pnt, 0, 500), |
| 212 |
(stx_pnt, D(km=1000)), |
| 213 |
(rast, 500), |
| 214 |
(json.loads(JSON_RASTER), 500), |
| 215 |
] |
| 216 |
elif name == "relate": |
| 217 |
# Set lookup values for the relate lookup. |
| 218 |
combo_values = [ |
| 219 |
(rast, "T*T***FF*"), |
| 220 |
(rast, 0, "T*T***FF*"), |
| 221 |
(rast, 0, "T*T***FF*"), |
| 222 |
(stx_pnt, 0, "T*T***FF*"), |
| 223 |
(stx_pnt, "T*T***FF*"), |
| 224 |
(rast, "T*T***FF*"), |
| 225 |
(json.loads(JSON_RASTER), "T*T***FF*"), |
| 226 |
] |
| 227 |
elif name == "isvalid": |
| 228 |
# The isvalid lookup doesn't make sense for rasters. |
| 229 |
continue |
| 230 |
elif PostGISOperations.gis_operators[name].func: |
| 231 |
# Set lookup values for all function based operators. |
| 232 |
combo_values = [ |
| 233 |
rast, |
| 234 |
(rast, 0), |
| 235 |
(rast, 0), |
| 236 |
(stx_pnt, 0), |
| 237 |
stx_pnt, |
| 238 |
rast, |
| 239 |
json.loads(JSON_RASTER), |
| 240 |
] |
| 241 |
else: |
| 242 |
# Override band lookup for these, as it's not supported. |
| 243 |
combo_keys[2] = "rastprojected__" + name |
| 244 |
# Set lookup values for all other operators. |
| 245 |
combo_values = [ |
| 246 |
rast, |
| 247 |
None, |
| 248 |
rast, |
| 249 |
stx_pnt, |
| 250 |
stx_pnt, |
| 251 |
rast, |
| 252 |
json.loads(JSON_RASTER), |
| 253 |
] |
| 254 |
|
| 255 |
# Create query filter combinations. |
| 256 |
self.assertEqual( |
| 257 |
len(combo_keys), |
| 258 |
len(combo_values), |
| 259 |
"Number of lookup names and values should be the same", |
| 260 |
) |
| 261 |
combos = [x for x in zip(combo_keys, combo_values) if x[1]] |
| 262 |
self.assertEqual( |
| 263 |
[(n, x) for n, x in enumerate(combos) if x in combos[:n]], |
| 264 |
[], |
| 265 |
"There are repeated test lookups", |
| 266 |
) |
| 267 |
combos = [{k: v} for k, v in combos] |
| 268 |
|
| 269 |
for combo in combos: |
| 270 |
# Apply this query filter. |
| 271 |
qs = RasterModel.objects.filter(**combo) |
| 272 |
|
| 273 |
# Evaluate normal filter qs. |
| 274 |
self.assertIn(qs.count(), [0, 1]) |
| 275 |
|
| 276 |
# Evaluate on conditional Q expressions. |
| 277 |
qs = RasterModel.objects.filter(Q(**combos[0]) & Q(**combos[1])) |
| 278 |
self.assertIn(qs.count(), [0, 1]) |
| 279 |
|
| 280 |
def test_dwithin_gis_lookup_output_with_rasters(self): |
| 281 |
""" |
| 282 |
Check the logical functionality of the dwithin lookup for different |
| 283 |
input parameters. |
| 284 |
""" |
| 285 |
# Create test raster and geom. |
| 286 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 287 |
stx_pnt = GEOSGeometry("POINT (-95.370401017314293 29.704867409475465)", 4326) |
| 288 |
stx_pnt.transform(3086) |
| 289 |
|
| 290 |
# Filter raster with different lookup raster formats. |
| 291 |
qs = RasterModel.objects.filter(rastprojected__dwithin=(rast, D(km=1))) |
| 292 |
self.assertEqual(qs.count(), 1) |
| 293 |
|
| 294 |
qs = RasterModel.objects.filter( |
| 295 |
rastprojected__dwithin=(json.loads(JSON_RASTER), D(km=1)) |
| 296 |
) |
| 297 |
self.assertEqual(qs.count(), 1) |
| 298 |
|
| 299 |
qs = RasterModel.objects.filter(rastprojected__dwithin=(JSON_RASTER, D(km=1))) |
| 300 |
self.assertEqual(qs.count(), 1) |
| 301 |
|
| 302 |
# Filter in an unprojected coordinate system. |
| 303 |
qs = RasterModel.objects.filter(rast__dwithin=(rast, 40)) |
| 304 |
self.assertEqual(qs.count(), 1) |
| 305 |
|
| 306 |
# Filter with band index transform. |
| 307 |
qs = RasterModel.objects.filter(rast__1__dwithin=(rast, 1, 40)) |
| 308 |
self.assertEqual(qs.count(), 1) |
| 309 |
qs = RasterModel.objects.filter(rast__1__dwithin=(rast, 40)) |
| 310 |
self.assertEqual(qs.count(), 1) |
| 311 |
qs = RasterModel.objects.filter(rast__dwithin=(rast, 1, 40)) |
| 312 |
self.assertEqual(qs.count(), 1) |
| 313 |
|
| 314 |
# Filter raster by geom. |
| 315 |
qs = RasterModel.objects.filter(rast__dwithin=(stx_pnt, 500)) |
| 316 |
self.assertEqual(qs.count(), 1) |
| 317 |
|
| 318 |
qs = RasterModel.objects.filter(rastprojected__dwithin=(stx_pnt, D(km=10000))) |
| 319 |
self.assertEqual(qs.count(), 1) |
| 320 |
|
| 321 |
qs = RasterModel.objects.filter(rast__dwithin=(stx_pnt, 5)) |
| 322 |
self.assertEqual(qs.count(), 0) |
| 323 |
|
| 324 |
qs = RasterModel.objects.filter(rastprojected__dwithin=(stx_pnt, D(km=100))) |
| 325 |
self.assertEqual(qs.count(), 0) |
| 326 |
|
| 327 |
# Filter geom by raster. |
| 328 |
qs = RasterModel.objects.filter(geom__dwithin=(rast, 500)) |
| 329 |
self.assertEqual(qs.count(), 1) |
| 330 |
|
| 331 |
# Filter through related model. |
| 332 |
qs = RasterRelatedModel.objects.filter(rastermodel__rast__dwithin=(rast, 40)) |
| 333 |
self.assertEqual(qs.count(), 1) |
| 334 |
|
| 335 |
# Filter through related model with band index transform |
| 336 |
qs = RasterRelatedModel.objects.filter(rastermodel__rast__1__dwithin=(rast, 40)) |
| 337 |
self.assertEqual(qs.count(), 1) |
| 338 |
|
| 339 |
# Filter through conditional statements. |
| 340 |
qs = RasterModel.objects.filter( |
| 341 |
Q(rast__dwithin=(rast, 40)) |
| 342 |
& Q(rastprojected__dwithin=(stx_pnt, D(km=10000))) |
| 343 |
) |
| 344 |
self.assertEqual(qs.count(), 1) |
| 345 |
|
| 346 |
# Filter through different lookup. |
| 347 |
qs = RasterModel.objects.filter(rastprojected__bbcontains=rast) |
| 348 |
self.assertEqual(qs.count(), 1) |
| 349 |
|
| 350 |
def test_lookup_input_tuple_too_long(self): |
| 351 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 352 |
msg = "Tuple too long for lookup bbcontains." |
| 353 |
with self.assertRaisesMessage(ValueError, msg): |
| 354 |
RasterModel.objects.filter(rast__bbcontains=(rast, 1, 2)) |
| 355 |
|
| 356 |
def test_lookup_input_band_not_allowed(self): |
| 357 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 358 |
qs = RasterModel.objects.filter(rast__bbcontains=(rast, 1)) |
| 359 |
msg = "Band indices are not allowed for this operator, it works on bbox only." |
| 360 |
with self.assertRaisesMessage(ValueError, msg): |
| 361 |
qs.count() |
| 362 |
|
| 363 |
def test_lookup_invalid_band_rhs(self): |
| 364 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 365 |
qs = RasterModel.objects.filter(rast__contains=(rast, "evil")) |
| 366 |
msg = "Band index must be an integer, but got 'str'." |
| 367 |
with self.assertRaisesMessage(TypeError, msg): |
| 368 |
qs.count() |
| 369 |
|
| 370 |
def test_lookup_invalid_band_lhs(self): |
| 371 |
""" |
| 372 |
Typical left-hand side usage is protected against non-integers, but for |
| 373 |
defense-in-depth purposes, construct custom lookups that evade the |
| 374 |
`int()` and `+ 1` checks in the lookups shipped by django.contrib.gis. |
| 375 |
""" |
| 376 |
|
| 377 |
# Evade the int() call in RasterField.get_transform(). |
| 378 |
class MyRasterBandTransform(RasterBandTransform): |
| 379 |
band_index = "evil" |
| 380 |
|
| 381 |
def process_band_indices(self, *args, **kwargs): |
| 382 |
self.band_lhs = self.lhs.band_index |
| 383 |
self.band_rhs, *self.rhs_params = self.rhs_params |
| 384 |
|
| 385 |
# Evade the `+ 1` call in BaseSpatialField.process_band_indices(). |
| 386 |
ContainsLookup = RasterModel._meta.get_field("rast").get_lookup("contains") |
| 387 |
|
| 388 |
class MyContainsLookup(ContainsLookup): |
| 389 |
def process_band_indices(self, *args, **kwargs): |
| 390 |
self.band_lhs = self.lhs.band_index |
| 391 |
self.band_rhs, *self.rhs_params = self.rhs_params |
| 392 |
|
| 393 |
RasterField = RasterModel._meta.get_field("rast") |
| 394 |
RasterField.register_lookup(MyContainsLookup, "contains") |
| 395 |
self.addCleanup(RasterField.register_lookup, ContainsLookup, "contains") |
| 396 |
|
| 397 |
qs = RasterModel.objects.annotate( |
| 398 |
transformed=MyRasterBandTransform("rast") |
| 399 |
).filter(transformed__contains=(F("transformed"), 1)) |
| 400 |
msg = "Band index must be an integer, but got 'str'." |
| 401 |
with self.assertRaisesMessage(TypeError, msg): |
| 402 |
list(qs) |
| 403 |
|
| 404 |
def test_isvalid_lookup_with_raster_error(self): |
| 405 |
qs = RasterModel.objects.filter(rast__isvalid=True) |
| 406 |
msg = ( |
| 407 |
"IsValid function requires a GeometryField in position 1, got RasterField." |
| 408 |
) |
| 409 |
with self.assertRaisesMessage(TypeError, msg): |
| 410 |
qs.count() |
| 411 |
|
| 412 |
def test_result_of_gis_lookup_with_rasters(self): |
| 413 |
# Point is in the interior |
| 414 |
qs = RasterModel.objects.filter( |
| 415 |
rast__contains=GEOSGeometry("POINT (-0.5 0.5)", 4326) |
| 416 |
) |
| 417 |
self.assertEqual(qs.count(), 1) |
| 418 |
# Point is in the exterior |
| 419 |
qs = RasterModel.objects.filter( |
| 420 |
rast__contains=GEOSGeometry("POINT (0.5 0.5)", 4326) |
| 421 |
) |
| 422 |
self.assertEqual(qs.count(), 0) |
| 423 |
# A point on the boundary is not contained properly |
| 424 |
qs = RasterModel.objects.filter( |
| 425 |
rast__contains_properly=GEOSGeometry("POINT (0 0)", 4326) |
| 426 |
) |
| 427 |
self.assertEqual(qs.count(), 0) |
| 428 |
# Raster is located left of the point |
| 429 |
qs = RasterModel.objects.filter(rast__left=GEOSGeometry("POINT (1 0)", 4326)) |
| 430 |
self.assertEqual(qs.count(), 1) |
| 431 |
|
| 432 |
def test_lookup_with_raster_bbox(self): |
| 433 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 434 |
# Shift raster upward |
| 435 |
rast.origin.y = 2 |
| 436 |
# The raster in the model is not strictly below |
| 437 |
qs = RasterModel.objects.filter(rast__strictly_below=rast) |
| 438 |
self.assertEqual(qs.count(), 0) |
| 439 |
# Shift raster further upward |
| 440 |
rast.origin.y = 6 |
| 441 |
# The raster in the model is strictly below |
| 442 |
qs = RasterModel.objects.filter(rast__strictly_below=rast) |
| 443 |
self.assertEqual(qs.count(), 1) |
| 444 |
|
| 445 |
def test_lookup_with_polygonized_raster(self): |
| 446 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 447 |
# Move raster to overlap with the model point on the left side |
| 448 |
rast.origin.x = -95.37040 + 1 |
| 449 |
rast.origin.y = 29.70486 |
| 450 |
# Raster overlaps with point in model |
| 451 |
qs = RasterModel.objects.filter(geom__intersects=rast) |
| 452 |
self.assertEqual(qs.count(), 1) |
| 453 |
# Change left side of raster to be nodata values |
| 454 |
rast.bands[0].data(data=[0, 0, 0, 1, 1], shape=(5, 1)) |
| 455 |
rast.bands[0].nodata_value = 0 |
| 456 |
qs = RasterModel.objects.filter(geom__intersects=rast) |
| 457 |
# Raster does not overlap anymore after polygonization |
| 458 |
# where the nodata zone is not included. |
| 459 |
self.assertEqual(qs.count(), 0) |
| 460 |
|
| 461 |
def test_lookup_value_error(self): |
| 462 |
# Test with invalid dict lookup parameter |
| 463 |
obj = {} |
| 464 |
msg = "Couldn't create spatial object from lookup value '%s'." % obj |
| 465 |
with self.assertRaisesMessage(ValueError, msg): |
| 466 |
RasterModel.objects.filter(geom__intersects=obj) |
| 467 |
# Test with invalid string lookup parameter |
| 468 |
obj = "00000" |
| 469 |
msg = "Couldn't create spatial object from lookup value '%s'." % obj |
| 470 |
with self.assertRaisesMessage(ValueError, msg): |
| 471 |
RasterModel.objects.filter(geom__intersects=obj) |
| 472 |
|
| 473 |
def test_db_function_errors(self): |
| 474 |
""" |
| 475 |
Errors are raised when using DB functions with raster content. |
| 476 |
""" |
| 477 |
point = GEOSGeometry("SRID=3086;POINT (-697024.9213808845 683729.1705516104)") |
| 478 |
rast = GDALRaster(json.loads(JSON_RASTER)) |
| 479 |
msg = "Distance function requires a geometric argument in position 2." |
| 480 |
with self.assertRaisesMessage(TypeError, msg): |
| 481 |
RasterModel.objects.annotate(distance_from_point=Distance("geom", rast)) |
| 482 |
with self.assertRaisesMessage(TypeError, msg): |
| 483 |
RasterModel.objects.annotate( |
| 484 |
distance_from_point=Distance("rastprojected", rast) |
| 485 |
) |
| 486 |
msg = ( |
| 487 |
"Distance function requires a GeometryField in position 1, got RasterField." |
| 488 |
) |
| 489 |
with self.assertRaisesMessage(TypeError, msg): |
| 490 |
RasterModel.objects.annotate( |
| 491 |
distance_from_point=Distance("rastprojected", point) |
| 492 |
).count() |
| 493 |
|
| 494 |
def test_lhs_with_index_rhs_without_index(self): |
| 495 |
with CaptureQueriesContext(connection) as queries: |
| 496 |
RasterModel.objects.filter( |
| 497 |
rast__0__contains=json.loads(JSON_RASTER) |
| 498 |
).exists() |
| 499 |
# It's easier to check the indexes in the generated SQL than to write |
| 500 |
# tests that cover all index combinations. |
| 501 |
self.assertRegex(queries[-1]["sql"], r"WHERE ST_Contains\([^)]*, 1, [^)]*, 1\)") |