rasterio.merge module

Copy valid pixels from input files to an output file.

rasterio.merge.copy_first(merged_data, new_data, merged_mask, new_mask, **kwargs)
rasterio.merge.copy_last(merged_data, new_data, merged_mask, new_mask, **kwargs)
rasterio.merge.copy_max(merged_data, new_data, merged_mask, new_mask, **kwargs)
rasterio.merge.copy_min(merged_data, new_data, merged_mask, new_mask, **kwargs)
rasterio.merge.merge(datasets, bounds=None, res=None, nodata=None, dtype=None, precision=None, indexes=None, output_count=None, resampling=Resampling.nearest, method='first', target_aligned_pixels=False, dst_path=None, dst_kwds=None)

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.

Parameters
  • datasets (list of dataset objects opened in 'r' mode, filenames or PathLike objects) – 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:
    merged_dataarray_like

    array to update with new_data

    new_dataarray_like

    data to merge same shape as merged_data

    merged_mask, new_maskarray_like

    boolean masks where merged/new data pixels are invalid same shape as merged_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

  • target_aligned_pixels (bool, optional) – Whether to adjust output image bounds so that pixel coordinates are integer multiples of pixel size, matching the -tap options of GDAL utilities. Default: False.

  • dst_path (str or PathLike, optional) – Path of output dataset

  • dst_kwds (dict, optional) – Dictionary of creation options and other paramters that will be overlaid on the profile of the output dataset.

Returns

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

tuple