[GIS] Merge raster layers with NoData cells

mergemosaicoverlapping-featuresqgisraster-calculator

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.

Best Answer

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)
QgsApplication.initQgis()

#prepare processing framework 
sys.path.append('E:\\QGIS\\apps\\qgis\\python\\plugins')
from processing.core.Processing import Processing
Processing.initialize()
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
QgsApplication.exitQgis()
QApplication.exit()

QGIS version: 2.14.3-Essen

Related Question