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).
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
.
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.
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.
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
.
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:

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

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):
