2015-11-24 22:48:00 +08:00
|
|
|
from ctypes import byref, c_double, c_int, c_void_p
|
2014-12-03 03:39:34 +08:00
|
|
|
|
2015-07-09 16:52:11 +08:00
|
|
|
from django.contrib.gis.gdal.error import GDALException
|
2014-12-03 03:39:34 +08:00
|
|
|
from django.contrib.gis.gdal.prototypes import raster as capi
|
2017-05-24 03:07:16 +08:00
|
|
|
from django.contrib.gis.gdal.raster.base import GDALRasterBase
|
2015-03-14 02:49:02 +08:00
|
|
|
from django.contrib.gis.shortcuts import numpy
|
2014-12-03 03:39:34 +08:00
|
|
|
from django.utils.encoding import force_text
|
|
|
|
|
2017-09-19 08:39:36 +08:00
|
|
|
from .const import (
|
|
|
|
GDAL_COLOR_TYPES, GDAL_INTEGER_TYPES, GDAL_PIXEL_TYPES, GDAL_TO_CTYPES,
|
|
|
|
)
|
2014-12-03 03:39:34 +08:00
|
|
|
|
|
|
|
|
2017-05-24 03:07:16 +08:00
|
|
|
class GDALBand(GDALRasterBase):
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Wrap a GDAL raster band, needs to be obtained from a GDALRaster object.
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, source, index):
|
|
|
|
self.source = source
|
2015-03-14 02:49:02 +08:00
|
|
|
self._ptr = capi.get_ds_raster_band(source._ptr, index)
|
2014-12-03 03:39:34 +08:00
|
|
|
|
2015-11-24 22:48:00 +08:00
|
|
|
def _flush(self):
|
|
|
|
"""
|
|
|
|
Call the flush method on the Band's parent raster and force a refresh
|
|
|
|
of the statistics attribute when requested the next time.
|
|
|
|
"""
|
|
|
|
self.source._flush()
|
|
|
|
self._stats_refresh = True
|
|
|
|
|
2014-12-03 03:39:34 +08:00
|
|
|
@property
|
|
|
|
def description(self):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the description string of the band.
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
2015-03-14 02:49:02 +08:00
|
|
|
return force_text(capi.get_band_description(self._ptr))
|
2014-12-03 03:39:34 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def width(self):
|
|
|
|
"""
|
|
|
|
Width (X axis) in pixels of the band.
|
|
|
|
"""
|
2015-03-14 02:49:02 +08:00
|
|
|
return capi.get_band_xsize(self._ptr)
|
2014-12-03 03:39:34 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def height(self):
|
|
|
|
"""
|
|
|
|
Height (Y axis) in pixels of the band.
|
|
|
|
"""
|
2015-03-14 02:49:02 +08:00
|
|
|
return capi.get_band_ysize(self._ptr)
|
2014-12-03 03:39:34 +08:00
|
|
|
|
2015-03-14 02:49:02 +08:00
|
|
|
@property
|
|
|
|
def pixel_count(self):
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the total number of pixels in this band.
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
2015-03-14 02:49:02 +08:00
|
|
|
return self.width * self.height
|
2014-12-03 03:39:34 +08:00
|
|
|
|
2015-11-24 22:48:00 +08:00
|
|
|
_stats_refresh = False
|
|
|
|
|
|
|
|
def statistics(self, refresh=False, approximate=False):
|
|
|
|
"""
|
|
|
|
Compute statistics on the pixel values of this band.
|
|
|
|
|
|
|
|
The return value is a tuple with the following structure:
|
|
|
|
(minimum, maximum, mean, standard deviation).
|
|
|
|
|
|
|
|
If approximate=True, the statistics may be computed based on overviews
|
|
|
|
or a subset of image tiles.
|
|
|
|
|
|
|
|
If refresh=True, the statistics will be computed from the data directly,
|
|
|
|
and the cache will be updated where applicable.
|
|
|
|
|
|
|
|
For empty bands (where all pixel values are nodata), all statistics
|
|
|
|
values are returned as None.
|
|
|
|
|
|
|
|
For raster formats using Persistent Auxiliary Metadata (PAM) services,
|
|
|
|
the statistics might be cached in an auxiliary file.
|
|
|
|
"""
|
|
|
|
# Prepare array with arguments for capi function
|
|
|
|
smin, smax, smean, sstd = c_double(), c_double(), c_double(), c_double()
|
|
|
|
stats_args = [
|
|
|
|
self._ptr, c_int(approximate), byref(smin), byref(smax),
|
|
|
|
byref(smean), byref(sstd), c_void_p(), c_void_p(),
|
|
|
|
]
|
|
|
|
|
|
|
|
if refresh or self._stats_refresh:
|
2016-05-17 00:43:04 +08:00
|
|
|
func = capi.compute_band_statistics
|
2015-11-24 22:48:00 +08:00
|
|
|
else:
|
|
|
|
# Add additional argument to force computation if there is no
|
|
|
|
# existing PAM file to take the values from.
|
|
|
|
force = True
|
|
|
|
stats_args.insert(2, c_int(force))
|
2016-05-17 00:43:04 +08:00
|
|
|
func = capi.get_band_statistics
|
2015-11-24 22:48:00 +08:00
|
|
|
|
2016-05-17 00:43:04 +08:00
|
|
|
# Computation of statistics fails for empty bands.
|
|
|
|
try:
|
|
|
|
func(*stats_args)
|
|
|
|
result = smin.value, smax.value, smean.value, sstd.value
|
|
|
|
except GDALException:
|
2015-11-24 22:48:00 +08:00
|
|
|
result = (None, None, None, None)
|
|
|
|
|
|
|
|
self._stats_refresh = False
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2014-12-03 03:39:34 +08:00
|
|
|
@property
|
|
|
|
def min(self):
|
|
|
|
"""
|
2015-11-24 22:48:00 +08:00
|
|
|
Return the minimum pixel value for this band.
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
2015-11-24 22:48:00 +08:00
|
|
|
return self.statistics()[0]
|
2014-12-03 03:39:34 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def max(self):
|
|
|
|
"""
|
2015-11-24 22:48:00 +08:00
|
|
|
Return the maximum pixel value for this band.
|
|
|
|
"""
|
|
|
|
return self.statistics()[1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def mean(self):
|
|
|
|
"""
|
|
|
|
Return the mean of all pixel values of this band.
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
2015-11-24 22:48:00 +08:00
|
|
|
return self.statistics()[2]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def std(self):
|
|
|
|
"""
|
|
|
|
Return the standard deviation of all pixel values of this band.
|
|
|
|
"""
|
|
|
|
return self.statistics()[3]
|
2014-12-03 03:39:34 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def nodata_value(self):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the nodata value for this band, or None if it isn't set.
|
2014-12-03 03:39:34 +08:00
|
|
|
"""
|
2015-06-19 23:46:03 +08:00
|
|
|
# Get value and nodata exists flag
|
2014-12-03 03:39:34 +08:00
|
|
|
nodata_exists = c_int()
|
2015-03-14 02:49:02 +08:00
|
|
|
value = capi.get_band_nodata_value(self._ptr, nodata_exists)
|
2015-06-19 23:46:03 +08:00
|
|
|
if not nodata_exists:
|
|
|
|
value = None
|
|
|
|
# If the pixeltype is an integer, convert to int
|
|
|
|
elif self.datatype() in GDAL_INTEGER_TYPES:
|
|
|
|
value = int(value)
|
|
|
|
return value
|
2015-03-14 02:49:02 +08:00
|
|
|
|
|
|
|
@nodata_value.setter
|
|
|
|
def nodata_value(self, value):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Set the nodata value for this band.
|
2015-03-14 02:49:02 +08:00
|
|
|
"""
|
2016-03-29 02:12:10 +08:00
|
|
|
if value is None:
|
|
|
|
if not capi.delete_band_nodata_value:
|
|
|
|
raise ValueError('GDAL >= 2.1 required to delete nodata values.')
|
|
|
|
capi.delete_band_nodata_value(self._ptr)
|
|
|
|
elif not isinstance(value, (int, float)):
|
|
|
|
raise ValueError('Nodata value must be numeric or None.')
|
|
|
|
else:
|
|
|
|
capi.set_band_nodata_value(self._ptr, value)
|
2015-11-24 22:48:00 +08:00
|
|
|
self._flush()
|
2015-03-14 02:49:02 +08:00
|
|
|
|
|
|
|
def datatype(self, as_string=False):
|
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Return the GDAL Pixel Datatype for this band.
|
2015-03-14 02:49:02 +08:00
|
|
|
"""
|
|
|
|
dtype = capi.get_band_datatype(self._ptr)
|
|
|
|
if as_string:
|
|
|
|
dtype = GDAL_PIXEL_TYPES[dtype]
|
|
|
|
return dtype
|
|
|
|
|
2017-09-19 08:39:36 +08:00
|
|
|
def color_interp(self, as_string=False):
|
|
|
|
"""Return the GDAL color interpretation for this band."""
|
|
|
|
color = capi.get_band_color_interp(self._ptr)
|
|
|
|
if as_string:
|
|
|
|
color = GDAL_COLOR_TYPES[color]
|
|
|
|
return color
|
|
|
|
|
2016-03-29 21:42:35 +08:00
|
|
|
def data(self, data=None, offset=None, size=None, shape=None, as_memoryview=False):
|
2015-03-14 02:49:02 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Read or writes pixel values for this band. Blocks of data can
|
2015-03-14 02:49:02 +08:00
|
|
|
be accessed by specifying the width, height and offset of the
|
|
|
|
desired block. The same specification can be used to update
|
|
|
|
parts of a raster by providing an array of values.
|
|
|
|
|
|
|
|
Allowed input data types are bytes, memoryview, list, tuple, and array.
|
|
|
|
"""
|
|
|
|
if not offset:
|
|
|
|
offset = (0, 0)
|
|
|
|
|
|
|
|
if not size:
|
|
|
|
size = (self.width - offset[0], self.height - offset[1])
|
|
|
|
|
2016-03-29 21:42:35 +08:00
|
|
|
if not shape:
|
|
|
|
shape = size
|
|
|
|
|
2015-03-14 02:49:02 +08:00
|
|
|
if any(x <= 0 for x in size):
|
|
|
|
raise ValueError('Offset too big for this raster.')
|
|
|
|
|
|
|
|
if size[0] > self.width or size[1] > self.height:
|
|
|
|
raise ValueError('Size is larger than raster.')
|
|
|
|
|
|
|
|
# Create ctypes type array generator
|
2016-03-29 21:42:35 +08:00
|
|
|
ctypes_array = GDAL_TO_CTYPES[self.datatype()] * (shape[0] * shape[1])
|
2015-03-14 02:49:02 +08:00
|
|
|
|
|
|
|
if data is None:
|
|
|
|
# Set read mode
|
|
|
|
access_flag = 0
|
|
|
|
# Prepare empty ctypes array
|
|
|
|
data_array = ctypes_array()
|
|
|
|
else:
|
|
|
|
# Set write mode
|
|
|
|
access_flag = 1
|
|
|
|
|
|
|
|
# Instantiate ctypes array holding the input data
|
2017-01-07 19:11:46 +08:00
|
|
|
if isinstance(data, (bytes, memoryview)) or (numpy and isinstance(data, numpy.ndarray)):
|
2015-03-14 02:49:02 +08:00
|
|
|
data_array = ctypes_array.from_buffer_copy(data)
|
|
|
|
else:
|
|
|
|
data_array = ctypes_array(*data)
|
|
|
|
|
|
|
|
# Access band
|
|
|
|
capi.band_io(self._ptr, access_flag, offset[0], offset[1],
|
2016-03-29 21:42:35 +08:00
|
|
|
size[0], size[1], byref(data_array), shape[0],
|
|
|
|
shape[1], self.datatype(), 0, 0)
|
2015-03-14 02:49:02 +08:00
|
|
|
|
|
|
|
# Return data as numpy array if possible, otherwise as list
|
|
|
|
if data is None:
|
|
|
|
if as_memoryview:
|
|
|
|
return memoryview(data_array)
|
|
|
|
elif numpy:
|
2016-03-31 16:43:14 +08:00
|
|
|
# reshape() needs a reshape parameter with the height first.
|
2015-03-14 02:49:02 +08:00
|
|
|
return numpy.frombuffer(
|
2016-03-31 16:43:14 +08:00
|
|
|
data_array, dtype=numpy.dtype(data_array)
|
|
|
|
).reshape(tuple(reversed(size)))
|
2015-03-14 02:49:02 +08:00
|
|
|
else:
|
|
|
|
return list(data_array)
|
|
|
|
else:
|
2015-11-24 22:48:00 +08:00
|
|
|
self._flush()
|
2015-07-09 16:52:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BandList(list):
|
|
|
|
def __init__(self, source):
|
|
|
|
self.source = source
|
2017-12-07 22:10:32 +08:00
|
|
|
super().__init__()
|
2015-07-09 16:52:11 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for idx in range(1, len(self) + 1):
|
|
|
|
yield GDALBand(self.source, idx)
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return capi.get_ds_raster_count(self.source._ptr)
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
try:
|
|
|
|
return GDALBand(self.source, index + 1)
|
|
|
|
except GDALException:
|
|
|
|
raise GDALException('Unable to get band index %d' % index)
|