I have a raster representing rivers/drainage basins within a terrain: enter image description here

I have a raster DEM of the terrain: enter image description here

In the basin raster, everything in black is 9999. White is nodata.

The DEM is obviously a range of values, from 1-500.

I want to combine these two rasters, in either of two ways: The drainage raster has its values added to those of the DEM. So if the course of the drainage basin in the DEM is originally a range of values between 2-300, the new range would be between 10199-10299.

Or: The drainage raster has its values replace those of the DEM. So if the course of the drainage basin in the DEM is originally a range of values between 2-300, it would now be a static 9999.

This seems like it should be straightforward, but I can't come up with a method in raster calc to give me the desired output. The closest I've come is an output raster which adds the two values together, but all of the surrounding DEM data is missing. i.e., the output looks identical to the basin raster, but with the combined value of DEM + basin raster.

I found the following solution from about a year ago: Updating/replacing raster values in one raster values from another in QGIS?

However, I can't replicate this behaviour in QGIS 3.4. When attempting to point the vectorization output at the existing DEM, it does not update, my ultimate output is the same DEM i began with.

My goal is to be able to run r.drain on the resulting map and get paths that do not follow the drainage basins, but instead find the next most efficient path through the terrain.

If someone knows of a better method to achieve this result, I'd welcome that as well. My DEM is based on vector contours/breaklines/point data, and I considered trying to re-interpolate the DEM based on the original vector drainage map, but wasn't able to come up with a way to do that either.

2 Answers 2


You can do it in raster calculator, but instead of conditional use the result of relational operator e.g. river@1 = 9999 gives 0 (false) or 1 (true).

Try this:

((river@1 = 9999) * river@1) + ((river@1 != 9999) * dem@1)

The first part or the second part will be zero, so the value of river raster or the value of the dem raster will be the result.

  • Using your suggested command outputs a raster identical to my initial rivers raster, unfortunately.
    – BogBody
    Dec 11, 2018 at 4:07
  • Try only the second part of the expression it should give your the dem except at the rivers where it should give 0.
    – Zoltan
    Dec 12, 2018 at 10:05
  • Is NoDATA value set to zero for your river raster? In Any operation where one of the operands is NoDATA cause the result willbe NoData too.
    – Zoltan
    Dec 12, 2018 at 10:43

Ah ha, I got it! Like most things, the answer makes me feel dumb in hindsight:


This allows you to fill in nodata areas in one raster, with data from a second raster. Since my drainage raster is surrounded by nodata, with r.patch I was able to patch the surrounding areas with the existing DEM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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