I have two elevation rasters that I want to combine into an output like the one shown in the image - Expected result.

I tried that using Gdal: Merge, but it doesn't take into account my NoData cells as seen in Merge - Result.

From what I understand from this question, it is possible to do that using GRASS's tool r.mapcalculator. I tried the solution listed there, but it outputs only a few pixels where the two rasters overlap.

How can I adapt the code below to match my needs?

r.mapcalc "output = not(if(a))*b"

I am open to other suggestions as well.

I am using QGIS 2.14 with GRASS 7.0.4.

  • 2
    This doesn't answer your question, but gdal_merge can take into account NoData values with the -n option for the input and -a_nodata for the output (official doc).
    – ArMoraer
    Commented Sep 6, 2016 at 9:12
  • 1
    You're right, thank you. Actually I was using gdal: Merge from the QGIS GUI and it didn't have the option for NoData. But I've used gdal_merge and it worked perfectly. Now I don't know if I should leave the question here or just delete it.
    – Ralu Bur
    Commented Sep 6, 2016 at 9:29
  • 1
    I would suggest leaving the question open and @ArMoraer posting an answer :)
    – Joseph
    Commented Sep 6, 2016 at 10:30
  • A solution using r.mapcalculator is still welcome, because gdal_merge stops my automated script with a "Python.exe has stopped working" error, as seen here, here and here.
    – Ralu Bur
    Commented Sep 7, 2016 at 12:30

2 Answers 2


One option to achieve this is using gdal_merge.py. But unfortunately, this tool stops my automated script with a "Python.exe has stopped working" error, as others complained here, here and here.

Another option, which actually works for me, is using SAGA's tool Mosaic raster layers, which doesn't generate any errors:

#prepare QGIS environment
import sys
from qgis.core import *
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("E:\\QGIS\\apps\\qgis", True)

#prepare processing framework 
from processing.core.Processing import Processing
import processing
from processing.tools import *

#set variables
algorithm = "saga:mosaickrasterlayers"
inputMain = path + "Flood_main.tif;"
input2D = path + "Flood_cvasi2D.tif"
addInput = None
dataType = 7
interpolation = 0
overlapAreas = 1
blendDistance = 10
match = 0
fit = 0
extent = "612550.33,660022.33,679329.28,703773.28"
result = path + "Flood.tif"

#run algorithm
processing.runalg(algorithm,inputMain + input2D, addInput, dataType, interpolation,
                  overlapAreas, blendDistance, match, fit, extent, result)

#exit QGIS

QGIS version: 2.14.3-Essen

  • saga mosaickrasterlayers als oworks well! Commented Jan 15, 2019 at 6:06

Copy of this answer: https://gis.stackexchange.com/a/425197/92995 In QGIS/processing, use the GRASS tool "r.series". As "Aggregate operation" choose "average"/"median"/"minimum" or "maximum" to deal with overlaps

The tool also conveniently can be set to ignore values above/below set values (useful in eliminating a NULL value of e.g. -9999, so it does not affect the results).

I think raster have to be same CRS, but can be different extend and different resolution.

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.