I have a raster A (a heightmap created from a contour file). I also have created a vector layer with some polygons which I rasterized into raster B. Raster B is completely contained within the extents of raster A.

Raster A has values at each pixel. Raster B, on the other hand, has a bunch of empty area (which I believe is represented as 0 or nodata).

I'd like to produce raster C, such that C = A + B, where A and B intersect, and C = A where A and B don't intersect.

The problem I'm running into is that when I add the two rasters together using the raster calculator, the result only has values where the two rasters intersect, but show nothing where they do not intersect. I'm guessing this is due to how QGIS treats nodata values.

How can I achieve what I'm trying to do?


The problem, as you observed, is that the Rasterize program sets the values outside of the polygons to NODATA (or dummy), and the raster calculator outputs a dummy wherever either raster has a dummy. In QGIS 3.6.3 I've solved this problem by specifying the output carefully in the Rasterize program to give everything outside of the polygons a value of zero.

  1. Under the advanced parameters set the "Pre-initialize" value to zero, which will cause the raster to be all zeros wherever there is not a polygon.
  2. Set the NODATA value to something other than zero. Zero is the default value which is really a poor choice. I just chose a large negative number that will clearly not be in the data.
  3. Possibly not necessary, but I also found I had to set the extents and cell size to exactly match the raster I will be combining with, otherwise I got wonky results

rasterize dialog


Procedure with GRASS and SAGA modules and QGIS toolbox : 1. QGIS : extract raster A' by clipping raster A with shapefile B 2. SAGA>Raster tools>Reclassify values : change nodata cells of A' to zero giving A" 3. GRASS>Raster>r.patch : patch raster B with A" giving B' 4. raster calc : add A' to B' giving C

  • Wouldn't that yield 2 x A where B was nodata?
    – Vince
    Sep 5 '17 at 10:19
  • Yes you're right. you have to reclass the corresponding zones in A to zero.
    – Christine
    Sep 5 '17 at 13:35
  • Please edit the answer to reflect this procedure. Right now the answer is too brief, and doesn't answer the question correctly. Screen shots and links to documentation would not be inappropriate.
    – Vince
    Sep 6 '17 at 4:32

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.