def _get_tile_data(self, tile: Tile, band: gdal.Band): ''' Loads the band data for the tile and converts it to the output data type. Returns a numpy array ''' band_data = np.array( band.ReadAsArray(tile.min_x, tile.min_y, tile.max_x - tile.min_x, tile.max_y - tile.min_y)) np_out_dt = gdal_array.GDALTypeCodeToNumericTypeCode( self.output_datatype) nodata = band.GetNoDataValue() if nodata is None: # convert the input data to the datatype we are going to use for # output band_data_converted = band_data.astype(np_out_dt) else: # conversion for data that includes nodata is done via a masked array # to prevent issues of converting a nodata value, losing some precision, # and having it not match the nodata value exactly (causing nodata to # not be nodata!) masked_band_data = np.ma.masked_where(band_data == nodata, band_data) masked_band_data_converted = masked_band_data.astype(np_out_dt) masked_band_data_converted.fill_value = self.output_nodata band_data_converted = masked_band_data_converted.filled() return band_data_converted
def read_band(band: gdal.Band, full=True, offset=None, chunk=None) -> List[int]: ''' Returns an array of a raster band Parameters: band: a gdal band full: to open the entirety of the raster offset: row offset chunk: chunk size of opening partially Returns: an array of pixels ''' logging.info(f'reading raster band: full={full}, offset={offset}') if full: arr = band.ReadAsArray() else: nrows = band.YSize ncols = band.XSize chunk = (nrows - offset) if (nrows - offset) < chunk else chunk arr = band.ReadAsArray(0, offset, ncols, chunk) return arr
def _get_block_size(self, density: gdal.Band, depth: gdal.Band, uncertainty: gdal.Band) -> Tuple[int, int]: ''' Gets the block size from the existing datasets. This is used to get optimium performance when reading data. ''' # For now it is assumed that all bands will share a common block # size. This is likely if they all share the same overall size, # and were all produced by the same piece of software. bs = depth.GetBlockSize() return (bs[0], bs[1])
def get_band_minimum(band: gdal.Band): ret = band.GetMinimum() if ret is None: band.ComputeStatistics(0) return band.GetMinimum()