raster_tools.stack_bands#

raster_tools.stack_bands(rasters, dst_crs=None, dst_grid=None, join='inner', resampling_method='nearest', resolution=None, dtype=None, null_value=None)[source]#

Stack input rasters into a multi-band raster.

All input bands are concatenated along the band axis onto a single destination grid. Inputs with differing grids, resolutions, or CRSs are reprojected onto the destination grid before stacking. The output has sum(r.nbands for r in rasters) bands.

Parameters
  • rasters (list of raster_tools.Raster) – A list-like object containing the rasters to be stacked. These can have differing grids, resolutions, and projections.

  • dst_crs (CRS-like, str, int, optional) – The destination CRS to use when building the destination grid. This can be anything that can be parsed by rasterio.CRS.from_user_input(). Only consulted when dst_grid is not provided. The default is to take the CRS from the first raster in rasters.

  • dst_grid (odc.geo.GeoBox, raster_tools.Raster, str, optional) – The definition for the destination grid to stack the rasters onto. This can be a odc.geo.GeoBox, raster_tools.Raster object, or a path str. If the input is a raster object or path, this function does NOT write to the given raster, it instead uses the raster as a reference for the grid. The default is to build a grid from the inputs according to join, using the resolution from the first raster in rasters. Passing dst_grid together with dst_crs or resolution is an error.

  • join (str, optional) –

    How to combine the input grids when building the destination grid. Only consulted when dst_grid is not provided. Valid options are:

    ’inner’

    Use the intersection of the input grid bounds. Only pixels covered by every input appear in the output. This is the default.

    ’outer’

    Use the union of the input grid bounds. Pixels not covered by a given input are filled with that band’s null value.

  • resampling_method (str, optional) –

    Resampling method to use when reprojecting input rasters to the destination grid. The default is nearest. Valid options are:

    ’nearest’

    Nearest neighbor resampling. This is the default.

    ’bilinear’

    Bilinear resampling.

    ’cubic’

    Cubic resampling.

    ’cubic_spline’

    Cubic spline resampling.

    ’lanczos’

    Lanczos windowed sinc resampling.

    ’average’

    Average resampling, computes the weighted average of all contributing pixels.

    ’mode’

    Mode resampling, selects the value which appears most often.

    ’max’

    Maximum resampling. (GDAL 2.0+)

    ’min’

    Minimum resampling. (GDAL 2.0+)

    ’med’

    Median resampling. (GDAL 2.0+)

    ’q1’

    Q1, first quartile resampling. (GDAL 2.0+)

    ’q3’

    Q3, third quartile resampling. (GDAL 2.0+)

    ’sum’

    Sum, compute the weighted sum. (GDAL 3.1+)

    ’rms’

    RMS, root mean square/quadratic mean. (GDAL 3.3+)

  • resolution (scalar, optional) – Pixel resolution of the output grid, in units of the output CRS. Only consulted when dst_grid is not provided. The default is to take the resolution from the first raster in rasters.

  • dtype (numpy.dtype, str, optional) – The dtype for the output raster. The default is to use numpy.result_type() on the dtypes from the input rasters.

  • null_value (scalar or "default", optional) – The nodata/null value to use for the output raster. The default is to take the null value from the first input raster that has one, falling back to a default based on the output dtype. Pass the string "default" to force the dtype-based default regardless of the inputs’ null values.

Returns

The resulting multi-band raster.

Return type

Raster

Notes

When dst_grid is not provided, the output CRS and resolution default to those of the first raster in rasters. Reordering the input list can therefore change the output grid; pass dst_crs, resolution, or dst_grid explicitly when that dependency is undesirable.