I'm trying to merge several files within a QGIS plugin via gdal_merge.py. In Anaconda distribution exactly the same code works as it is like that. I suppose Python in QGIS can't find gdal_merge.py. But maybe I'm also wrong. How can I fix this issue?

wd = C:/Users/Jonas/Documents/Bachelorarbeit/MODIS/Data/oneday
# Define directories
Path(wd+"/mergedTiles").mkdir(parents=True, exist_ok=True)
out_dir = wd + '/mergedTiles/'

# Get tif files in wd
tile_list = glob.glob(wd+'/unmergedTiles' + '/*LST_*.tiff')

#get sublsists and zip
str_start = [s.split('\\')[1][:17] for s in tile_list]
str_end = [s.split('\\')[1].split('_')[1] for s in tile_list]
res = np.unique([i + j for i, j in zip(str_start, str_end)])

for label in res: 
    files = [wd+'/unmergedTiles/' + s.split('\\')[1] for s in tile_list if s.endswith(label[17:]) and s.split('\\')[1][:17] == label[:17]]
    out_path = out_dir + label
    #print('Files To Merge')
    cmd = 'python ' + "gdal_merge.py" +' -co COMPRESSED=YES'+ ' -o' + ' ' + out_path + ' ' + '-of gtiff ' + " ".join(files)
    output = subprocess.check_output(cmd)
  • Did you try just gdal_merge.py instead of python gdal_merge.py? Normally, if it's in the path, you don't need to explicitly add python. If no choice, you may import os and os.chdir(path_to_dir_containing_gdal_merge)
    – ThomasG77
    Feb 19, 2021 at 20:35
  • @ThomasG77 if I delete "python in my string I get a new error. OSError: [WinError 193] is not a valid Win32 application. Feb 19, 2021 at 20:50
  • in addition, I have differnt gdal_merge.py in QGIS and anaconda. I actually don't know to which I shoud refer. Feb 19, 2021 at 20:56
  • Use the 2nd recipe in my comment and don't remove python as it's breaking. About choosing one or the other gdal_merge.py. If your QGIS is not executed from a conda env, use the QGIS gdal_merge.py
    – ThomasG77
    Feb 19, 2021 at 22:05
  • it has worked! Adter trying some directories. Thanks for help Feb 20, 2021 at 11:50


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.