You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

band.py 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. from ctypes import byref, c_double, c_int, c_void_p
  2. from django.contrib.gis.gdal.error import GDALException
  3. from django.contrib.gis.gdal.prototypes import raster as capi
  4. from django.contrib.gis.gdal.raster.base import GDALRasterBase
  5. from django.contrib.gis.shortcuts import numpy
  6. from django.utils.encoding import force_text
  7. from .const import (
  8. GDAL_COLOR_TYPES, GDAL_INTEGER_TYPES, GDAL_PIXEL_TYPES, GDAL_TO_CTYPES,
  9. )
  10. class GDALBand(GDALRasterBase):
  11. """
  12. Wrap a GDAL raster band, needs to be obtained from a GDALRaster object.
  13. """
  14. def __init__(self, source, index):
  15. self.source = source
  16. self._ptr = capi.get_ds_raster_band(source._ptr, index)
  17. def _flush(self):
  18. """
  19. Call the flush method on the Band's parent raster and force a refresh
  20. of the statistics attribute when requested the next time.
  21. """
  22. self.source._flush()
  23. self._stats_refresh = True
  24. @property
  25. def description(self):
  26. """
  27. Return the description string of the band.
  28. """
  29. return force_text(capi.get_band_description(self._ptr))
  30. @property
  31. def width(self):
  32. """
  33. Width (X axis) in pixels of the band.
  34. """
  35. return capi.get_band_xsize(self._ptr)
  36. @property
  37. def height(self):
  38. """
  39. Height (Y axis) in pixels of the band.
  40. """
  41. return capi.get_band_ysize(self._ptr)
  42. @property
  43. def pixel_count(self):
  44. """
  45. Return the total number of pixels in this band.
  46. """
  47. return self.width * self.height
  48. _stats_refresh = False
  49. def statistics(self, refresh=False, approximate=False):
  50. """
  51. Compute statistics on the pixel values of this band.
  52. The return value is a tuple with the following structure:
  53. (minimum, maximum, mean, standard deviation).
  54. If approximate=True, the statistics may be computed based on overviews
  55. or a subset of image tiles.
  56. If refresh=True, the statistics will be computed from the data directly,
  57. and the cache will be updated where applicable.
  58. For empty bands (where all pixel values are nodata), all statistics
  59. values are returned as None.
  60. For raster formats using Persistent Auxiliary Metadata (PAM) services,
  61. the statistics might be cached in an auxiliary file.
  62. """
  63. # Prepare array with arguments for capi function
  64. smin, smax, smean, sstd = c_double(), c_double(), c_double(), c_double()
  65. stats_args = [
  66. self._ptr, c_int(approximate), byref(smin), byref(smax),
  67. byref(smean), byref(sstd), c_void_p(), c_void_p(),
  68. ]
  69. if refresh or self._stats_refresh:
  70. func = capi.compute_band_statistics
  71. else:
  72. # Add additional argument to force computation if there is no
  73. # existing PAM file to take the values from.
  74. force = True
  75. stats_args.insert(2, c_int(force))
  76. func = capi.get_band_statistics
  77. # Computation of statistics fails for empty bands.
  78. try:
  79. func(*stats_args)
  80. result = smin.value, smax.value, smean.value, sstd.value
  81. except GDALException:
  82. result = (None, None, None, None)
  83. self._stats_refresh = False
  84. return result
  85. @property
  86. def min(self):
  87. """
  88. Return the minimum pixel value for this band.
  89. """
  90. return self.statistics()[0]
  91. @property
  92. def max(self):
  93. """
  94. Return the maximum pixel value for this band.
  95. """
  96. return self.statistics()[1]
  97. @property
  98. def mean(self):
  99. """
  100. Return the mean of all pixel values of this band.
  101. """
  102. return self.statistics()[2]
  103. @property
  104. def std(self):
  105. """
  106. Return the standard deviation of all pixel values of this band.
  107. """
  108. return self.statistics()[3]
  109. @property
  110. def nodata_value(self):
  111. """
  112. Return the nodata value for this band, or None if it isn't set.
  113. """
  114. # Get value and nodata exists flag
  115. nodata_exists = c_int()
  116. value = capi.get_band_nodata_value(self._ptr, nodata_exists)
  117. if not nodata_exists:
  118. value = None
  119. # If the pixeltype is an integer, convert to int
  120. elif self.datatype() in GDAL_INTEGER_TYPES:
  121. value = int(value)
  122. return value
  123. @nodata_value.setter
  124. def nodata_value(self, value):
  125. """
  126. Set the nodata value for this band.
  127. """
  128. if value is None:
  129. if not capi.delete_band_nodata_value:
  130. raise ValueError('GDAL >= 2.1 required to delete nodata values.')
  131. capi.delete_band_nodata_value(self._ptr)
  132. elif not isinstance(value, (int, float)):
  133. raise ValueError('Nodata value must be numeric or None.')
  134. else:
  135. capi.set_band_nodata_value(self._ptr, value)
  136. self._flush()
  137. def datatype(self, as_string=False):
  138. """
  139. Return the GDAL Pixel Datatype for this band.
  140. """
  141. dtype = capi.get_band_datatype(self._ptr)
  142. if as_string:
  143. dtype = GDAL_PIXEL_TYPES[dtype]
  144. return dtype
  145. def color_interp(self, as_string=False):
  146. """Return the GDAL color interpretation for this band."""
  147. color = capi.get_band_color_interp(self._ptr)
  148. if as_string:
  149. color = GDAL_COLOR_TYPES[color]
  150. return color
  151. def data(self, data=None, offset=None, size=None, shape=None, as_memoryview=False):
  152. """
  153. Read or writes pixel values for this band. Blocks of data can
  154. be accessed by specifying the width, height and offset of the
  155. desired block. The same specification can be used to update
  156. parts of a raster by providing an array of values.
  157. Allowed input data types are bytes, memoryview, list, tuple, and array.
  158. """
  159. offset = offset or (0, 0)
  160. size = size or (self.width - offset[0], self.height - offset[1])
  161. shape = shape or size
  162. if any(x <= 0 for x in size):
  163. raise ValueError('Offset too big for this raster.')
  164. if size[0] > self.width or size[1] > self.height:
  165. raise ValueError('Size is larger than raster.')
  166. # Create ctypes type array generator
  167. ctypes_array = GDAL_TO_CTYPES[self.datatype()] * (shape[0] * shape[1])
  168. if data is None:
  169. # Set read mode
  170. access_flag = 0
  171. # Prepare empty ctypes array
  172. data_array = ctypes_array()
  173. else:
  174. # Set write mode
  175. access_flag = 1
  176. # Instantiate ctypes array holding the input data
  177. if isinstance(data, (bytes, memoryview)) or (numpy and isinstance(data, numpy.ndarray)):
  178. data_array = ctypes_array.from_buffer_copy(data)
  179. else:
  180. data_array = ctypes_array(*data)
  181. # Access band
  182. capi.band_io(self._ptr, access_flag, offset[0], offset[1],
  183. size[0], size[1], byref(data_array), shape[0],
  184. shape[1], self.datatype(), 0, 0)
  185. # Return data as numpy array if possible, otherwise as list
  186. if data is None:
  187. if as_memoryview:
  188. return memoryview(data_array)
  189. elif numpy:
  190. # reshape() needs a reshape parameter with the height first.
  191. return numpy.frombuffer(
  192. data_array, dtype=numpy.dtype(data_array)
  193. ).reshape(tuple(reversed(size)))
  194. else:
  195. return list(data_array)
  196. else:
  197. self._flush()
  198. class BandList(list):
  199. def __init__(self, source):
  200. self.source = source
  201. super().__init__()
  202. def __iter__(self):
  203. for idx in range(1, len(self) + 1):
  204. yield GDALBand(self.source, idx)
  205. def __len__(self):
  206. return capi.get_ds_raster_count(self.source._ptr)
  207. def __getitem__(self, index):
  208. try:
  209. return GDALBand(self.source, index + 1)
  210. except GDALException:
  211. raise GDALException('Unable to get band index %d' % index)