I am trying to create a single raster file that divides the region of Antwerp, Belgium into 5 categories: high green, low green, agriculture, water, and build-up area. For this, I will have to combine two raster files I already have:

  1. I have a raster that shows at a certain resolution different categories of vegetation. The legend is as follows: 1 = High green (dark green); 2 = low green (light green); 3 = agriculture (yellow); and 4 = other (grey) (see figure below).enter image description here

  2. Another raster shows where there is water in the same area (Legend: 6 = water (blue)) (see figure below where this raster layer is shown on top of the first).enter image description here

What I want to do is to keep the values 1, 2, and 3, and then overwrite the values '4' (other) with '6' (water), but only there where there is water present. The other values '4' should remain the same.

I believe I will have to run some kind of raster calculator line, but I am lost as to which one that would be.

Here is a link to a part of both the vegetation and water datasets: https://we.tl/t-dfXUKHhDxS


2 Answers 2


Use the Raster Calculator to add selected values from one image to another using the formula:

("Raster 2@1" = 127) * 127 + ("Raster 2@1" != 127) * "Raster 1@1" 

"Raster 1@1" is the image into which the selected values from the "Raster 2@1" image will be inserted and 127 is the value you want to transfer (in my case 127 is grey color in shape of the heart).

___________ Raster1 __________________ Raster2____________________Result

enter image description here__ enter image description here__ enter image description here

** UPDATE 1**

Your raster file with water contains nodata. If you try to check raster values for expression x = integer value and x is Null (nodata) this will always lead to False result (because Null is not a value).

  1. Fill nodata with zeros with the Fill NoData cells tool.
  2. Don't change the expression too much (it makes no sense), use:
("Water@1" = 6) * 6 + ("Water@1" != 6) * "Vegetation@1"
  1. If that doesn't work - clip data to the same extent, make sure that both images have the same data type and pixel size.

enter image description here __ enter image description here __ enter image description here

  • I used following syntax in the raster caclulation tool: ("Water@1" = 6)*6 + ("Water@1" != 6)*(("Vegetation@1"=1)*1 + ("Vegetation@1"=2)*2 + ("Vegetation@1"=3)*3 + ("Vegetation@1"=4)*4). There however seems to be a problem with it, as the tool cannot run. I eddited the question to include a link to a part of the datasets.
    – Amber
    Commented Oct 2, 2021 at 9:41
  • @Amber See update. Commented Oct 2, 2021 at 12:41
  • @ComradeChe It would be great if you can add another condition 'Replaced by Water (6) only when Vegetation is Other (4)'. Your current expression puts water in the green/light green area, too.
    – Kazuhito
    Commented Oct 2, 2021 at 23:53
  • @Amber Try this: ("Water@1" = 6 and "Vegetation@1" = 4) * 6 + ("Water@1" != 6 and "Vegetation@1" != 4) * "Vegetation@1" Commented Oct 3, 2021 at 9:03

Here's a solution I found to work on a very similar issue, replacing a portion of a DEM with another DEM.

Using the Merge tool, select both raster layers as your inputs, then check the "Place each input file into a separate band". Export the file, for the purpose of the example call name it "Merged_2band" and be sure to add it to your project.

Select the raster file with the larger extent (if that applies) in the layers panel, then open the raster calculator. You will see your newly created file listed in the "raster bands" display area with @1 and @2 signifying the different bands. Hit "Use selected Layer Extent" to set your Spatial Extent. Enter the following into the Raster Calculator Expression

if ( "Merged_2band@2=6,"Merged_2band@2","Merged_2band@1")

The resulting file should overwrite any other values with 6 if they share the same space, but leave everything else alone.

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.