[GIS] mosaic rasters with gdal_merge.py

gdalgdal-mergepythonsubprocess

I want to mosaic 2 rasters "rasa" and "rasb" using gdal_merge.py . While running the code, it produces an error " No input files selected". Can somebody help me to find the error in the code?

    import subprocess
    filea = 'C:/Users/claudio/workspace/test/test1/rasa.tif'
    fileb = 'C:/Users/claudio/workspace/test/test1/rasb.tif'
    output = 'C:/Users/claudio/workspace/test/test1/output.tif'
    subprocess.call(['gdal_merge','-o',output,filea,fileb],shell=True)

when i run the above code, the output i am getting is:
enter image description here

Best Answer

The problem is you are passing a list to subprocess then specifying shell=True (which expects a string as input) so it is only taking the first item in the list, which is gdal_merge, and running this. Just running gdal_merge.py gives the same error.

The same code but without shell=True should work:

import subprocess
filea = 'C:/Users/claudio/workspace/test/test1/rasa.tif'
fileb = 'C:/Users/claudio/workspace/test/test1/rasb.tif'
output = 'C:/Users/claudio/workspace/test/test1/output.tif'
subprocess.call(['gdal_merge', '-o' , output, filea, fileb])