rasterio.merge module

Copy valid pixels from input files to an output file.

rasterio.merge.merge(datasets, bounds=None, res=None, nodata=None, dtype=None, precision=10, indexes=None, output_count=None, resampling=<Resampling.nearest: 0>, method='first')

Copy valid pixels from input files to an output file.

All files must have the same number of bands, data type, and coordinate reference system.

Input files are merged in their listed order using the reverse painter’s algorithm (default) or another method. If the output file exists, its values will be overwritten by input values.

Geospatial bounds and resolution of a new output file in the units of the input file coordinate reference system may be provided and are otherwise taken from the first input file.

  • datasets (list of dataset objects opened in 'r' mode) – source datasets to be merged.

  • bounds (tuple, optional) – Bounds of the output image (left, bottom, right, top). If not set, bounds are determined from bounds of input rasters.

  • res (tuple, optional) – Output resolution in units of coordinate reference system. If not set, the resolution of the first raster is used. If a single value is passed, output pixels will be square.

  • nodata (float, optional) – nodata value to use in output file. If not set, uses the nodata value in the first input raster.

  • dtype (numpy dtype or string) – dtype to use in outputfile. If not set, uses the dtype value in the first input raster.

  • precision (float, optional) – Number of decimal points of precision when computing inverse transform.

  • indexes (list of ints or a single int, optional) – bands to read and merge

  • output_count (int, optional) – If using callable it may be useful to have additional bands in the output in addition to the indexes specified for read

  • resampling (Resampling, optional) – Resampling algorithm used when reading input files. Default: Resampling.nearest.

  • method (str or callable) –

    pre-defined method:

    first: reverse painting last: paint valid new on top of existing min: pixel-wise min of existing and new max: pixel-wise max of existing and new

    or custom callable with signature:

    def function(old_data, new_data, old_nodata, new_nodata, index=None, roff=None, coff=None):


    array to update with new_data


    data to merge same shape as old_data

    old_nodata, new_dataarray_like

    boolean masks where old/new data is nodata same shape as old_data

    index: int

    index of the current dataset within the merged dataset collection

    roff: int

    row offset in base array

    coff: int

    column offset in base array


Two elements:

dest: numpy ndarray

Contents of all input rasters in single array

out_transform: affine.Affine()

Information for mapping pixel coordinates in dest to another coordinate system

Return type