Source code for geographer.raster_bands_getter_mixin
"""Mix-in that provides methods to get raster bands."""from__future__importannotationsfrompathlibimportPathimportrasterioasrio
[docs]classRasterBandsGetterMixIn:"""Mix-in that provides methods to get raster bands."""def_get_bands_for_raster(self,bands:dict[str,list[int]|None]|None,source_raster_path:Path,)->list[int]:"""Return bands indices to be used in the target raster. Args: source_raster_path: path to source raster. bands: dict of band indices Raises: ValueError: If the optional bands dict is not None and the raster type (source_raster_path.parent.name) is not in the bands dict. Returns: band indices """raster_type=source_raster_path.parent.nameifbandsisNone:returnself._get_all_band_indices(source_raster_path)elifraster_typeinbandsandbands[raster_type]isNone:returnself._get_all_band_indices(source_raster_path)elifraster_typeinbandsandbands[raster_type]isnotNone:returnbands[raster_type]# type: ignoreelse:raiseValueError(f"Missing bands key: {raster_type}")def_get_all_band_indices(self,source_raster_path:Path)->list[int]:"""Return list of all band indices of source GeoTiff. Args: source_raster_path: path to source raster (or label etc) Returns: indices of all bands in GeoTiff """withrio.open(source_raster_path)assrc:bands=list(range(1,src.count+1))returnbands