def get_area( address, bbox, max_img_size=512, bbox_crs="epsg:4326", out_crs="epsg:3857", nodata=0 ): """Read image part.""" bounds = transform_bounds(bbox_crs, out_crs, *bbox, densify_pts=21) vrt_params = dict(add_alpha=True, crs=out_crs, resampling=Resampling.bilinear) if nodata is not None: vrt_params.update( dict( nodata=nodata, add_alpha=False, src_nodata=nodata, init_dest_nodata=False, ) ) with rasterio.open(address) as src: vrt_transform, vrt_width, vrt_height = get_vrt_transform(src, bounds) vrt_width = round(vrt_width) if vrt_width < max_img_size else max_img_size vrt_height = round(vrt_height) if vrt_height < max_img_size else max_img_size vrt_params.update( dict(transform=vrt_transform, width=vrt_width, height=vrt_height) ) with WarpedVRT(src, **vrt_params) as vrt: data = vrt.read( out_shape=(1, vrt_height, vrt_width), resampling=Resampling.bilinear, indexes=[1], ) return data
def get_area_stats( src, bounds, max_img_size=512, indexes=None, nodata=None, resampling_method="bilinear", bbox_crs="epsg:4326", histogram_bins=20, histogram_range=None, ): """ Read data and mask. Attributes ---------- srd_dst : rasterio.io.DatasetReader rasterio.io.DatasetReader object bounds : list bounds (left, bottom, right, top) tilesize : int Output image size indexes : list of ints or a single int, optional, (defaults: None) If `indexes` is a list, the result is a 3D array, but is a 2D array if it is a band index number. nodata: int or float, optional (defaults: None) resampling_method : str, optional (default: "bilinear") Resampling algorithm histogram_bins: int, optional Defines the number of equal-width histogram bins (default: 10). histogram_range: str, optional The lower and upper range of the bins. If not provided, range is simply the min and max of the array. Returns ------- out : array, int returns pixel value. """ if isinstance(indexes, int): indexes = [indexes] elif isinstance(indexes, tuple): indexes = list(indexes) with rasterio.open(src) as src_dst: bounds = transform_bounds(bbox_crs, src_dst.crs, *bounds, densify_pts=21) vrt_params = dict(add_alpha=True, resampling=Resampling[resampling_method]) indexes = indexes if indexes is not None else src_dst.indexes nodata = nodata if nodata is not None else src_dst.nodata 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] vrt_transform, vrt_width, vrt_height = get_vrt_transform( src_dst, bounds, bounds_crs=src_dst.crs) vrt_params.update( dict(transform=vrt_transform, width=vrt_width, height=vrt_height)) width = round(vrt_width) if vrt_width < max_img_size else max_img_size height = round( vrt_height) if vrt_height < max_img_size else max_img_size out_shape = (len(indexes), width, height) if nodata is not None: vrt_params.update( dict(nodata=nodata, add_alpha=False, src_nodata=nodata)) if has_alpha_band(src_dst): vrt_params.update(dict(add_alpha=False)) with WarpedVRT(src_dst, **vrt_params) as vrt: arr = vrt.read(out_shape=out_shape, indexes=indexes, masked=True) if not arr.any(): return None, band_descriptions params = {} if histogram_bins: params.update(dict(bins=histogram_bins)) if histogram_range: params.update(dict(range=histogram_range)) stats = { indexes[b]: _stats(arr[b], **params) for b in range(arr.shape[0]) if vrt.colorinterp[b] != ColorInterp.alpha } return stats, band_descriptions
def read_cog_tile(src, bounds, tile_size, indexes=None, nodata=None, resampling_method="bilinear", tile_edge_padding=2): """ Read cloud-optimized geotiff tile. Notes ----- Modified from `rio-tiler <https://github.com/cogeotiff/rio-tiler>`_. License included below per terms of use. BSD 3-Clause License (c) 2017 Mapbox All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Arguments --------- src : rasterio.io.DatasetReader rasterio.io.DatasetReader object bounds : list Tile bounds (left, bottom, right, top) tile_size : list Output image size indexes : list of ints or a single int, optional, (defaults: None) If `indexes` is a list, the result is a 3D array, but is a 2D array if it is a band index number. nodata: int or float, optional (defaults: None) resampling_method : str, optional (default: "bilinear") Resampling algorithm tile_edge_padding : int, optional (default: 2) Padding to apply to each edge of the tile when retrieving data to assist in reducing resampling artefacts along edges. Returns ------- out : array, int returns pixel value. """ if isinstance(indexes, int): indexes = [indexes] elif isinstance(indexes, tuple): indexes = list(indexes) vrt_params = dict( add_alpha=True, crs='epsg:' + str(src.crs.to_epsg()), resampling=Resampling[resampling_method] ) vrt_transform, vrt_width, vrt_height = get_vrt_transform( src, bounds, bounds_crs='epsg:' + str(src.crs.to_epsg())) out_window = Window(col_off=0, row_off=0, width=vrt_width, height=vrt_height) if tile_edge_padding > 0 and not \ _requested_tile_aligned_with_internal_tile(src, bounds, tile_size): vrt_transform = vrt_transform * Affine.translation( -tile_edge_padding, -tile_edge_padding ) orig__vrt_height = vrt_height orig_vrt_width = vrt_width vrt_height = vrt_height + 2 * tile_edge_padding vrt_width = vrt_width + 2 * tile_edge_padding out_window = Window( col_off=tile_edge_padding, row_off=tile_edge_padding, width=orig_vrt_width, height=orig__vrt_height, ) vrt_params.update(dict(transform=vrt_transform, width=vrt_width, height=vrt_height)) indexes = indexes if indexes is not None else src.indexes out_shape = (len(indexes), tile_size[1], tile_size[0]) nodata = nodata if nodata is not None else src.nodata if nodata is not None: vrt_params.update(dict(nodata=nodata, add_alpha=False, src_nodata=nodata)) if has_alpha_band(src): vrt_params.update(dict(add_alpha=False)) with WarpedVRT(src, **vrt_params) as vrt: data = vrt.read( out_shape=out_shape, indexes=indexes, window=out_window, resampling=Resampling[resampling_method], ) mask = vrt.dataset_mask(out_shape=(tile_size[1], tile_size[0]), window=out_window) return data, mask, out_window, vrt_transform