Raster field for GeoDjango -- evaluates into GDALRaster objects.
| 387 | |
| 388 | |
| 389 | class RasterField(BaseSpatialField): |
| 390 | """ |
| 391 | Raster field for GeoDjango -- evaluates into GDALRaster objects. |
| 392 | """ |
| 393 | |
| 394 | description = _("Raster Field") |
| 395 | geom_type = "RASTER" |
| 396 | geography = False |
| 397 | |
| 398 | def _check_connection(self, connection): |
| 399 | # Make sure raster fields are used only on backends with raster |
| 400 | # support. |
| 401 | if ( |
| 402 | not connection.features.gis_enabled |
| 403 | or not connection.features.supports_raster |
| 404 | ): |
| 405 | raise ImproperlyConfigured( |
| 406 | "Raster fields require backends with raster support." |
| 407 | ) |
| 408 | |
| 409 | def db_type(self, connection): |
| 410 | self._check_connection(connection) |
| 411 | return super().db_type(connection) |
| 412 | |
| 413 | def from_db_value(self, value, expression, connection): |
| 414 | return connection.ops.parse_raster(value) |
| 415 | |
| 416 | def contribute_to_class(self, cls, name, **kwargs): |
| 417 | super().contribute_to_class(cls, name, **kwargs) |
| 418 | # Setup for lazy-instantiated Raster object. For large querysets, the |
| 419 | # instantiation of all GDALRasters can potentially be expensive. This |
| 420 | # delays the instantiation of the objects to the moment of evaluation |
| 421 | # of the raster attribute. |
| 422 | setattr(cls, self.attname, SpatialProxy(gdal.GDALRaster, self)) |
| 423 | |
| 424 | def get_transform(self, name): |
| 425 | from django.contrib.gis.db.models.lookups import RasterBandTransform |
| 426 | |
| 427 | try: |
| 428 | band_index = int(name) |
| 429 | return type( |
| 430 | "SpecificRasterBandTransform", |
| 431 | (RasterBandTransform,), |
| 432 | {"band_index": band_index}, |
| 433 | ) |
| 434 | except ValueError: |
| 435 | pass |
| 436 | return super().get_transform(name) |