[GIS] Producing raster A + B where A and B intersect, and A where they do not, using QGIS

qgisrasterraster-calculator

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?

Best Answer

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

Related Question