def test_has_mask(): """Should return True.""" with rasterio.open(S3_MASK_PATH) as src_dst: assert utils.has_mask_band(src_dst) with rasterio.open(COG_DST) as src_dst: assert not utils.has_mask_band(src_dst)
def test_has_alpha(): """Check if rasters have alpha bands.""" with rasterio.open(S3_ALPHA_PATH) as src_dst: assert utils.has_alpha_band(src_dst) assert not utils.has_mask_band(src_dst) with rasterio.open(COG_DST) as src_dst: assert not utils.has_alpha_band(src_dst)
def _get_info(asset: str) -> Tuple: with rasterio.open(asset) as src_dst: description = [ src_dst.descriptions[b - 1] for i, b in enumerate(src_dst.indexes) ] indexes = non_alpha_indexes(src_dst) mask = has_mask_band(src_dst) return ( src_dst.count, src_dst.dtypes[0], description, src_dst.tags(), src_dst.nodata, mask, indexes, )
def info(address: str) -> Dict: """ Return simple metadata about the file. Attributes ---------- address : str or PathLike object A dataset path or URL. Will be opened in "r" mode. Returns ------- out : dict. """ with rasterio.open(address) as src_dst: minzoom, maxzoom = get_zooms(src_dst) bounds = transform_bounds(src_dst.crs, constants.WGS84_CRS, *src_dst.bounds, densify_pts=21) center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2, minzoom] def _get_descr(ix): """Return band description.""" name = src_dst.descriptions[ix - 1] if not name: name = "band{}".format(ix) return name band_descriptions = [(ix, _get_descr(ix)) for ix in src_dst.indexes] tags = [(ix, src_dst.tags(ix)) for ix in src_dst.indexes] other_meta = dict() if src_dst.scales[0] and src_dst.offsets[0]: other_meta.update(dict(scale=src_dst.scales[0])) other_meta.update(dict(offset=src_dst.offsets[0])) if has_alpha_band(src_dst): nodata_type = "Alpha" elif has_mask_band(src_dst): nodata_type = "Mask" elif src_dst.nodata is not None: nodata_type = "Nodata" else: nodata_type = "None" try: cmap = src_dst.colormap(1) other_meta.update(dict(colormap=cmap)) except ValueError: pass return dict( address=address, bounds=bounds, center=center, minzoom=minzoom, maxzoom=maxzoom, band_metadata=tags, band_descriptions=band_descriptions, dtype=src_dst.meta["dtype"], colorinterp=[ src_dst.colorinterp[ix - 1].name for ix in src_dst.indexes ], nodata_type=nodata_type, **other_meta, )
def metadata( src_dst: Union[DatasetReader, DatasetWriter, WarpedVRT], bounds: Optional[Tuple[float, float, float, float]] = None, indexes: Optional[Union[Sequence[int], int]] = None, max_size: int = 1024, bounds_crs: CRS = constants.WGS84_CRS, percentiles: Tuple[float, float] = (2.0, 98.0), hist_options: Dict = {}, **kwargs: Any, ) -> Dict: """ Retrieve metadata and statistics from an image. Attributes ---------- src_dst : rasterio.io.DatasetReader rasterio.io.DatasetReader object bounds : tuple, optional Bounding box coordinates from which to calculate image statistics. max_size : int `max_size` of the longest dimension, respecting bounds X/Y aspect ratio. indexes : list of ints or a single int, optional Band indexes. bounds_crs: CRS or str, optional Specify bounds coordinate reference system, default WGS84/EPSG4326. percentiles: tuple, optional Tuple of Min/Max percentiles to compute. Default is (2, 98). hist_options : dict, optional Options to forward to numpy.histogram function. kwargs : Any, optional Additional options to forward to part or preview Returns ------- dict """ if isinstance(indexes, int): indexes = (indexes, ) if indexes is None: indexes = non_alpha_indexes(src_dst) if indexes != src_dst.indexes: warnings.warn("Alpha band was removed from the output data array", AlphaBandWarning) if bounds: data, mask = part( src_dst, bounds, max_size=max_size, indexes=indexes, bounds_crs=bounds_crs, **kwargs, ) bounds = transform_bounds(bounds_crs, constants.WGS84_CRS, *bounds, densify_pts=21) else: data, mask = preview(src_dst, max_size=max_size, indexes=indexes, **kwargs) bounds = transform_bounds(src_dst.crs, constants.WGS84_CRS, *src_dst.bounds, densify_pts=21) data = numpy.ma.array(data) data.mask = mask == 0 statistics = { indexes[b]: raster_stats(data[b], percentiles=percentiles, **hist_options) for b in range(data.shape[0]) } def _get_descr(ix): """Return band description.""" name = src_dst.descriptions[ix - 1] if not name: name = "band{}".format(ix) return name band_descriptions = [(ix, _get_descr(ix)) for ix in indexes] tags = [(ix, src_dst.tags(ix)) for ix in indexes] other_meta = dict() if src_dst.scales[0] and src_dst.offsets[0]: other_meta.update(dict(scale=src_dst.scales[0])) other_meta.update(dict(offset=src_dst.offsets[0])) if has_alpha_band(src_dst): nodata_type = "Alpha" elif has_mask_band(src_dst): nodata_type = "Mask" elif src_dst.nodata is not None: nodata_type = "Nodata" else: nodata_type = "None" try: cmap = src_dst.colormap(1) other_meta.update(dict(colormap=cmap)) except ValueError: pass return dict( bounds=bounds, statistics=statistics, band_metadata=tags, band_descriptions=band_descriptions, dtype=src_dst.meta["dtype"], colorinterp=[src_dst.colorinterp[ix - 1].name for ix in indexes], nodata_type=nodata_type, **other_meta, )