I have two rasters. One is larger and binary called mask (all values are either 0 or 1). The smaller raster fits inside this raster but has a much smaller extent smallerMask. All its values are 1. Where it sits above the larger raster, the larger raster is populated with 0s. I want to replace that area of 0s with the 1s from the smallerMask. Thought this would be easy.

(("mask" + "smallerMask") = 2 OR ("mask" + "smallerMask") = 1) = 1

However, I just get output along the smaller raster extent. I made sure that the larger one was selected as the extent. Why is this happening?

I've also tried merge but it keeps the original values.

EDIT: I figured out a workaround by using both layers to build a virtual raster but I would really like to know how to do this with raster calculator

1 Answer 1


Okay, I figured this out.

  1. Ensure the smaller raster actually has the same extent as the larger raster, but with a bunch of nodata values outside the AOI.

  2. use r.null to convert the nodata to 0s in the smaller raster.

  3. Use the following expression in raster calculator:

(("mask1" + "mask2") = 2 OR ("mask1" + "mask2") = 1) = 1

And that's it.

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.