2

I have two raster files A and B which overlap in certain areas. These rasters should be merged to one while taking the exact values of raster A, and hence overriding/replacing the values from raster B in those areas.

I tried the tools Mosaic raster layers (with "first" and "last" as options for dealing with the overlapping areas) as well as r.patch so far, but all the outputs do not contain the exact values of raster A, but instead often show some values between raster A and B.

Is there a proper way to do it? If not in QGIS, I would also try in ArcGIS.

1
  • 1
    It should be possible to do this in a two step operation: First remove the areas in raster B in the areas raster A is defined, then combine the rasters. Commented Sep 10, 2021 at 12:18

1 Answer 1

3

You can use a combination of Virtual raster and Raster calculator to clip those pixels of raster B that overlap raster A, than combine both rasters. What is a bit tricky is to get a common extent (like a bounding box around both rasters) and to avoid nodata values outside the raster: if you just add both layers with raster calculator, only where pixels of both rasters overlap you'll have values. Everything else will be set to nodata. This can be solved by combining both rasters with Virtual raster (or Merge, alternatively).

  1. Create a Virtual raster, select both layers (be sure that in the layer panel, rasterA is above rasterB) and check the box next to Place each input file into a separate band to keep the values of both layers separate (we use it later) - the output should be named virtual.

  2. Use raster calculator to create a mask covering rasterA. Use this expression: "rasterA@1"/"rasterA@1". Output raster has value 1 where rasterA has pixels and nodata everywhere else.

  3. Convert nodata to 0 on the output of layer 2 with Fill NoData cells. We can't use nodata values for calculation in the next step. Rename the result to filled: it has value 1 for rasterA, 0 for everything else.

  4. You now can clip those pixels from rasterB that overlap with rasterA using raster calculator with this expression: "virtual@2" * (1- "filled@1"). Be sure to select the correct band of the virtual layer (the one containing the pixels from rasterB, here band @2). You get a raster with original pixel values from layer B where it does not overlap and 0 everywhere else. Rename it only2.

  5. Add the band representing rasterA from the virtual raster with the output from step 4: "only2@1"+"virtual@1" and here you are with your result.

RasterB with values 0 to 10: enter image description here

RasterA with values from 0 to 33 (in the background: part of RasterB visbile) - so two layers: enter image description here

The result: combined raster as one layer, containing the values of rasterA and only those pixels of rasterB that are not covered by rasterA. The dark area in the upper left are nodata values (where none of the two rasters A or B have pixels): enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.