2017-04-01 03:59:44 +08:00
|
|
|
from collections import defaultdict, namedtuple
|
2017-03-09 23:17:41 +08:00
|
|
|
from contextlib import suppress
|
2016-12-16 05:36:18 +08:00
|
|
|
|
2016-06-14 22:18:33 +08:00
|
|
|
from django.contrib.gis import forms, gdal
|
2015-06-19 23:46:03 +08:00
|
|
|
from django.contrib.gis.db.models.proxy import SpatialProxy
|
2016-04-22 00:03:14 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.contrib.gis.geometry.backend import Geometry, GeometryException
|
2015-06-19 23:46:03 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.fields import Field
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2010-01-19 05:49:13 +08:00
|
|
|
# Local cache of the spatial_ref_sys table, which holds SRID data for each
|
|
|
|
# spatial database alias. This cache exists so that the database isn't queried
|
|
|
|
# for SRID info each time a distance query is constructed.
|
2016-12-16 05:36:18 +08:00
|
|
|
_srid_cache = defaultdict(dict)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2017-04-01 04:02:28 +08:00
|
|
|
SRIDCacheEntry = namedtuple('SRIDCacheEntry', ['units', 'units_name', 'spheroid', 'geodetic'])
|
2017-04-01 03:59:44 +08:00
|
|
|
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def get_srid_info(srid, connection):
|
2009-04-11 02:08:37 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the units, unit name, and spheroid WKT associated with the
|
2009-04-11 02:08:37 +08:00
|
|
|
given SRID from the `spatial_ref_sys` (or equivalent) spatial database
|
2010-01-19 05:49:13 +08:00
|
|
|
table for the given database connection. These results are cached.
|
2009-04-11 02:08:37 +08:00
|
|
|
"""
|
2016-12-16 06:09:21 +08:00
|
|
|
from django.contrib.gis.gdal import SpatialReference
|
2009-04-11 02:08:37 +08:00
|
|
|
global _srid_cache
|
|
|
|
|
2010-01-19 05:49:13 +08:00
|
|
|
try:
|
|
|
|
# The SpatialRefSys model for the spatial backend.
|
|
|
|
SpatialRefSys = connection.ops.spatial_ref_sys()
|
|
|
|
except NotImplementedError:
|
2016-12-16 05:36:18 +08:00
|
|
|
SpatialRefSys = None
|
2010-01-19 05:49:13 +08:00
|
|
|
|
2016-12-16 05:36:18 +08:00
|
|
|
alias, get_srs = (
|
|
|
|
(connection.alias, lambda srid: SpatialRefSys.objects.using(connection.alias).get(srid=srid).srs)
|
|
|
|
if SpatialRefSys else
|
|
|
|
(None, SpatialReference)
|
|
|
|
)
|
|
|
|
if srid not in _srid_cache[alias]:
|
|
|
|
srs = get_srs(srid)
|
|
|
|
units, units_name = srs.units
|
2017-04-01 03:59:44 +08:00
|
|
|
_srid_cache[alias][srid] = SRIDCacheEntry(
|
|
|
|
units=units,
|
|
|
|
units_name=units_name,
|
|
|
|
spheroid='SPHEROID["%s",%s,%s]' % (srs['spheroid'], srs.semi_major, srs.inverse_flattening),
|
2017-04-01 04:02:28 +08:00
|
|
|
geodetic=srs.geographic,
|
2017-04-01 03:59:44 +08:00
|
|
|
)
|
2009-04-11 02:08:37 +08:00
|
|
|
|
2016-12-16 05:36:18 +08:00
|
|
|
return _srid_cache[alias][srid]
|
2009-04-11 01:04:35 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class GeoSelectFormatMixin:
|
2014-12-01 15:28:01 +08:00
|
|
|
def select_format(self, compiler, sql, params):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the selection format string, depending on the requirements
|
2014-12-01 15:28:01 +08:00
|
|
|
of the spatial backend. For example, Oracle and MySQL require custom
|
|
|
|
selection formats in order to retrieve geometries in OGC WKT. For all
|
2017-01-25 04:31:57 +08:00
|
|
|
other fields, return a simple '%s' format string.
|
2014-12-01 15:28:01 +08:00
|
|
|
"""
|
|
|
|
connection = compiler.connection
|
|
|
|
if connection.ops.select:
|
|
|
|
# This allows operations to be done on fields in the SELECT,
|
|
|
|
# overriding their values -- used by the Oracle and MySQL
|
2017-03-21 21:13:18 +08:00
|
|
|
# spatial backends to get database values as WKT.
|
|
|
|
sql = connection.ops.select % sql
|
|
|
|
return sql, params
|
2014-12-01 15:28:01 +08:00
|
|
|
|
|
|
|
|
2015-06-19 23:46:03 +08:00
|
|
|
class BaseSpatialField(Field):
|
|
|
|
"""
|
|
|
|
The Base GIS Field.
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-06-19 23:46:03 +08:00
|
|
|
It's used as a base class for GeometryField and RasterField. Defines
|
|
|
|
properties that are common to all GIS fields such as the characteristics
|
|
|
|
of the spatial reference system of the field.
|
|
|
|
"""
|
|
|
|
description = _("The base GIS field.")
|
2016-06-21 17:08:29 +08:00
|
|
|
empty_strings_allowed = False
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-06-19 23:46:03 +08:00
|
|
|
def __init__(self, verbose_name=None, srid=4326, spatial_index=True, **kwargs):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2015-06-19 23:46:03 +08:00
|
|
|
The initialization function for base spatial fields. Takes the following
|
2008-08-06 02:13:06 +08:00
|
|
|
as keyword arguments:
|
|
|
|
|
|
|
|
srid:
|
|
|
|
The spatial reference system identifier, an OGC standard.
|
|
|
|
Defaults to 4326 (WGS84).
|
|
|
|
|
|
|
|
spatial_index:
|
|
|
|
Indicates whether to create a spatial index. Defaults to True.
|
|
|
|
Set this instead of 'db_index' for geographic fields since index
|
|
|
|
creation is different for geometry columns.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Setting the index flag with the value of the `spatial_index` keyword.
|
2009-03-31 01:15:49 +08:00
|
|
|
self.spatial_index = spatial_index
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2008-08-26 12:55:56 +08:00
|
|
|
# Setting the SRID and getting the units. Unit information must be
|
2008-08-06 02:13:06 +08:00
|
|
|
# easily available in the field instance for distance queries.
|
2009-03-31 01:15:49 +08:00
|
|
|
self.srid = srid
|
2009-04-11 00:58:29 +08:00
|
|
|
|
2008-08-26 12:55:56 +08:00
|
|
|
# Setting the verbose_name keyword argument with the positional
|
2008-08-07 05:40:00 +08:00
|
|
|
# first parameter, so this works like normal fields.
|
|
|
|
kwargs['verbose_name'] = verbose_name
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(**kwargs)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-27 20:56:33 +08:00
|
|
|
def deconstruct(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
name, path, args, kwargs = super().deconstruct()
|
2015-06-19 23:46:03 +08:00
|
|
|
# Always include SRID for less fragility; include spatial index if it's
|
|
|
|
# not the default value.
|
2013-11-27 20:56:33 +08:00
|
|
|
kwargs['srid'] = self.srid
|
2013-11-28 06:42:39 +08:00
|
|
|
if self.spatial_index is not True:
|
2013-11-27 20:56:33 +08:00
|
|
|
kwargs['spatial_index'] = self.spatial_index
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2015-06-19 23:46:03 +08:00
|
|
|
def db_type(self, connection):
|
|
|
|
return connection.ops.geo_db_type(self)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def spheroid(self, connection):
|
2017-04-01 03:59:44 +08:00
|
|
|
return get_srid_info(self.srid, connection).spheroid
|
2009-04-11 02:08:37 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def units(self, connection):
|
2017-04-01 03:59:44 +08:00
|
|
|
return get_srid_info(self.srid, connection).units
|
2009-04-11 02:08:37 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def units_name(self, connection):
|
2017-04-01 03:59:44 +08:00
|
|
|
return get_srid_info(self.srid, connection).units_name
|
2009-04-11 00:58:29 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def geodetic(self, connection):
|
2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return true if this field's SRID corresponds with a coordinate
|
2008-08-06 02:13:06 +08:00
|
|
|
system that uses non-projected units (e.g., latitude/longitude).
|
|
|
|
"""
|
2017-04-01 04:02:28 +08:00
|
|
|
return get_srid_info(self.srid, connection).geodetic
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2015-06-19 23:46:03 +08:00
|
|
|
def get_placeholder(self, value, compiler, connection):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the placeholder for the spatial column for the
|
2015-06-19 23:46:03 +08:00
|
|
|
given value.
|
|
|
|
"""
|
|
|
|
return connection.ops.get_geom_placeholder(self, value, compiler)
|
|
|
|
|
2016-04-22 00:03:14 +08:00
|
|
|
def get_srid(self, obj):
|
|
|
|
"""
|
|
|
|
Return the default SRID for the given geometry or raster, taking into
|
|
|
|
account the SRID set for the field. For example, if the input geometry
|
|
|
|
or raster doesn't have an SRID, then the SRID of the field will be
|
|
|
|
returned.
|
|
|
|
"""
|
|
|
|
srid = obj.srid # SRID of given geometry.
|
|
|
|
if srid is None or self.srid == -1 or (srid == -1 and self.srid != -1):
|
|
|
|
return self.srid
|
|
|
|
else:
|
|
|
|
return srid
|
|
|
|
|
|
|
|
def get_db_prep_save(self, value, connection):
|
|
|
|
"""
|
|
|
|
Prepare the value for saving in the database.
|
|
|
|
"""
|
2016-06-17 23:03:50 +08:00
|
|
|
if isinstance(value, Geometry) or value:
|
2016-04-22 00:03:14 +08:00
|
|
|
return connection.ops.Adapter(self.get_prep_value(value))
|
2016-06-17 23:03:50 +08:00
|
|
|
else:
|
|
|
|
return None
|
2016-04-22 00:03:14 +08:00
|
|
|
|
2016-05-28 00:43:17 +08:00
|
|
|
def get_raster_prep_value(self, value, is_candidate):
|
|
|
|
"""
|
|
|
|
Return a GDALRaster if conversion is successful, otherwise return None.
|
|
|
|
"""
|
2016-06-14 22:18:33 +08:00
|
|
|
if isinstance(value, gdal.GDALRaster):
|
2016-05-28 00:43:17 +08:00
|
|
|
return value
|
|
|
|
elif is_candidate:
|
2017-03-09 23:17:41 +08:00
|
|
|
with suppress(GDALException):
|
2016-06-14 22:18:33 +08:00
|
|
|
return gdal.GDALRaster(value)
|
2016-05-28 00:43:17 +08:00
|
|
|
elif isinstance(value, dict):
|
|
|
|
try:
|
2016-06-14 22:18:33 +08:00
|
|
|
return gdal.GDALRaster(value)
|
2016-05-28 00:43:17 +08:00
|
|
|
except GDALException:
|
|
|
|
raise ValueError("Couldn't create spatial object from lookup value '%s'." % value)
|
|
|
|
|
2016-04-22 00:03:14 +08:00
|
|
|
def get_prep_value(self, value):
|
2017-07-20 22:08:55 +08:00
|
|
|
obj = super().get_prep_value(value)
|
2016-04-22 00:03:14 +08:00
|
|
|
# When the input is not a geometry or raster, attempt to construct one
|
|
|
|
# from the given string input.
|
2016-05-28 00:43:17 +08:00
|
|
|
if isinstance(obj, Geometry):
|
2016-04-22 00:03:14 +08:00
|
|
|
pass
|
2016-05-28 00:43:17 +08:00
|
|
|
else:
|
|
|
|
# Check if input is a candidate for conversion to raster or geometry.
|
2016-12-29 23:27:49 +08:00
|
|
|
is_candidate = isinstance(obj, (bytes, str)) or hasattr(obj, '__geo_interface__')
|
2016-06-14 22:18:33 +08:00
|
|
|
# Try to convert the input to raster.
|
|
|
|
raster = self.get_raster_prep_value(obj, is_candidate)
|
2016-05-28 00:43:17 +08:00
|
|
|
|
|
|
|
if raster:
|
|
|
|
obj = raster
|
|
|
|
elif is_candidate:
|
2016-04-22 00:03:14 +08:00
|
|
|
try:
|
2016-05-28 00:43:17 +08:00
|
|
|
obj = Geometry(obj)
|
|
|
|
except (GeometryException, GDALException):
|
2016-04-22 00:03:14 +08:00
|
|
|
raise ValueError("Couldn't create spatial object from lookup value '%s'." % obj)
|
2016-05-28 00:43:17 +08:00
|
|
|
else:
|
|
|
|
raise ValueError('Cannot use object with type %s for a spatial lookup parameter.' % type(obj).__name__)
|
2016-04-22 00:03:14 +08:00
|
|
|
|
|
|
|
# Assigning the SRID value.
|
|
|
|
obj.srid = self.get_srid(obj)
|
2017-07-20 22:08:55 +08:00
|
|
|
return obj
|
2016-04-22 00:03:14 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2015-06-19 23:46:03 +08:00
|
|
|
class GeometryField(GeoSelectFormatMixin, BaseSpatialField):
|
|
|
|
"""
|
|
|
|
The base Geometry field -- maps to the OpenGIS Specification Geometry type.
|
|
|
|
"""
|
|
|
|
description = _("The base Geometry field -- maps to the OpenGIS Specification Geometry type.")
|
|
|
|
form_class = forms.GeometryField
|
|
|
|
# The OpenGIS Geometry name.
|
|
|
|
geom_type = 'GEOMETRY'
|
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def __init__(self, verbose_name=None, dim=2, geography=False, *, extent=(-180.0, -90.0, 180.0, 90.0),
|
|
|
|
tolerance=0.05, **kwargs):
|
2015-06-19 23:46:03 +08:00
|
|
|
"""
|
|
|
|
The initialization function for geometry fields. In addition to the
|
|
|
|
parameters from BaseSpatialField, it takes the following as keyword
|
|
|
|
arguments:
|
|
|
|
|
|
|
|
dim:
|
|
|
|
The number of dimensions for this geometry. Defaults to 2.
|
|
|
|
|
|
|
|
extent:
|
|
|
|
Customize the extent, in a 4-tuple of WGS 84 coordinates, for the
|
|
|
|
geometry field entry in the `USER_SDO_GEOM_METADATA` table. Defaults
|
|
|
|
to (-180.0, -90.0, 180.0, 90.0).
|
|
|
|
|
|
|
|
tolerance:
|
|
|
|
Define the tolerance, in meters, to use for the geometry field
|
|
|
|
entry in the `USER_SDO_GEOM_METADATA` table. Defaults to 0.05.
|
|
|
|
"""
|
|
|
|
# Setting the dimension of the geometry field.
|
|
|
|
self.dim = dim
|
|
|
|
|
|
|
|
# Is this a geography rather than a geometry column?
|
|
|
|
self.geography = geography
|
|
|
|
|
|
|
|
# Oracle-specific private attributes for creating the entry in
|
|
|
|
# `USER_SDO_GEOM_METADATA`
|
2017-02-02 00:41:56 +08:00
|
|
|
self._extent = extent
|
|
|
|
self._tolerance = tolerance
|
2015-06-19 23:46:03 +08:00
|
|
|
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(verbose_name=verbose_name, **kwargs)
|
2015-06-19 23:46:03 +08:00
|
|
|
|
|
|
|
def deconstruct(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
name, path, args, kwargs = super().deconstruct()
|
2015-06-19 23:46:03 +08:00
|
|
|
# Include kwargs if they're not the default values.
|
|
|
|
if self.dim != 2:
|
|
|
|
kwargs['dim'] = self.dim
|
|
|
|
if self.geography is not False:
|
|
|
|
kwargs['geography'] = self.geography
|
|
|
|
return name, path, args, kwargs
|
|
|
|
|
2015-11-09 14:33:40 +08:00
|
|
|
def get_db_prep_value(self, value, connection, *args, **kwargs):
|
|
|
|
return connection.ops.Adapter(
|
2017-01-21 21:13:44 +08:00
|
|
|
super().get_db_prep_value(value, connection, *args, **kwargs),
|
2015-11-09 14:33:40 +08:00
|
|
|
**({'geography': True} if self.geography else {})
|
|
|
|
)
|
|
|
|
|
2015-02-20 18:53:59 +08:00
|
|
|
def from_db_value(self, value, expression, connection, context):
|
2015-11-04 14:30:05 +08:00
|
|
|
if value:
|
2017-07-20 17:57:06 +08:00
|
|
|
value = Geometry(value)
|
2015-11-04 14:30:05 +08:00
|
|
|
srid = value.srid
|
|
|
|
if not srid and self.srid != -1:
|
|
|
|
value.srid = self.srid
|
2014-08-12 20:08:40 +08:00
|
|
|
return value
|
|
|
|
|
2014-07-23 17:41:06 +08:00
|
|
|
def contribute_to_class(self, cls, name, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().contribute_to_class(cls, name, **kwargs)
|
2008-08-26 12:55:56 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Setup for lazy-instantiated Geometry object.
|
2015-06-19 23:46:03 +08:00
|
|
|
setattr(cls, self.attname, SpatialProxy(Geometry, self))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
2013-10-27 09:27:42 +08:00
|
|
|
defaults = {'form_class': self.form_class,
|
|
|
|
'geom_type': self.geom_type,
|
|
|
|
'srid': self.srid,
|
2008-08-06 02:13:06 +08:00
|
|
|
}
|
|
|
|
defaults.update(kwargs)
|
2014-03-31 03:11:05 +08:00
|
|
|
if (self.dim > 2 and 'widget' not in kwargs and
|
2013-03-16 18:39:18 +08:00
|
|
|
not getattr(defaults['form_class'].widget, 'supports_3d', False)):
|
|
|
|
defaults['widget'] = forms.Textarea
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().formfield(**defaults)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# The OpenGIS Geometry Type Fields
|
|
|
|
class PointField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'POINT'
|
2013-03-16 18:39:18 +08:00
|
|
|
form_class = forms.PointField
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Point")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class LineStringField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'LINESTRING'
|
2013-03-16 18:39:18 +08:00
|
|
|
form_class = forms.LineStringField
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Line string")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class PolygonField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'POLYGON'
|
2013-03-16 18:39:18 +08:00
|
|
|
form_class = forms.PolygonField
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Polygon")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class MultiPointField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'MULTIPOINT'
|
2013-03-16 18:39:18 +08:00
|
|
|
form_class = forms.MultiPointField
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Multi-point")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class MultiLineStringField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'MULTILINESTRING'
|
2013-03-16 18:39:18 +08:00
|
|
|
form_class = forms.MultiLineStringField
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Multi-line string")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class MultiPolygonField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'MULTIPOLYGON'
|
2013-03-16 18:39:18 +08:00
|
|
|
form_class = forms.MultiPolygonField
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Multi polygon")
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class GeometryCollectionField(GeometryField):
|
2009-03-31 01:15:49 +08:00
|
|
|
geom_type = 'GEOMETRYCOLLECTION'
|
2013-03-16 18:39:18 +08:00
|
|
|
form_class = forms.GeometryCollectionField
|
2009-12-17 02:13:34 +08:00
|
|
|
description = _("Geometry collection")
|
2013-12-25 21:13:18 +08:00
|
|
|
|
|
|
|
|
2014-12-01 15:28:01 +08:00
|
|
|
class ExtentField(GeoSelectFormatMixin, Field):
|
2013-12-25 21:13:18 +08:00
|
|
|
"Used as a return value from an extent aggregate"
|
|
|
|
|
|
|
|
description = _("Extent Aggregate Field")
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return "ExtentField"
|
2015-06-19 23:46:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RasterField(BaseSpatialField):
|
|
|
|
"""
|
|
|
|
Raster field for GeoDjango -- evaluates into GDALRaster objects.
|
|
|
|
"""
|
|
|
|
|
|
|
|
description = _("Raster Field")
|
|
|
|
geom_type = 'RASTER'
|
2016-04-22 00:03:14 +08:00
|
|
|
geography = False
|
2015-06-19 23:46:03 +08:00
|
|
|
|
|
|
|
def _check_connection(self, connection):
|
|
|
|
# Make sure raster fields are used only on backends with raster support.
|
|
|
|
if not connection.features.gis_enabled or not connection.features.supports_raster:
|
|
|
|
raise ImproperlyConfigured('Raster fields require backends with raster support.')
|
|
|
|
|
|
|
|
def db_type(self, connection):
|
|
|
|
self._check_connection(connection)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().db_type(connection)
|
2015-06-19 23:46:03 +08:00
|
|
|
|
|
|
|
def from_db_value(self, value, expression, connection, context):
|
|
|
|
return connection.ops.parse_raster(value)
|
|
|
|
|
|
|
|
def contribute_to_class(self, cls, name, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().contribute_to_class(cls, name, **kwargs)
|
2015-06-19 23:46:03 +08:00
|
|
|
# Setup for lazy-instantiated Raster object. For large querysets, the
|
|
|
|
# instantiation of all GDALRasters can potentially be expensive. This
|
|
|
|
# delays the instantiation of the objects to the moment of evaluation
|
|
|
|
# of the raster attribute.
|
2016-06-14 22:18:33 +08:00
|
|
|
setattr(cls, self.attname, SpatialProxy(gdal.GDALRaster, self))
|
2016-04-22 00:03:14 +08:00
|
|
|
|
|
|
|
def get_transform(self, name):
|
2017-03-28 10:20:18 +08:00
|
|
|
from django.contrib.gis.db.models.lookups import RasterBandTransform
|
2016-04-22 00:03:14 +08:00
|
|
|
try:
|
|
|
|
band_index = int(name)
|
|
|
|
return type(
|
|
|
|
'SpecificRasterBandTransform',
|
|
|
|
(RasterBandTransform, ),
|
|
|
|
{'band_index': band_index}
|
|
|
|
)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_transform(name)
|