GDAL Merge – Merging Rasters with gdal_merge.py

gdalgdalwarpgeotiff-tiffmergeraster

I'm trying to create a mosaic raster from two rasters. With rasterio works well but gdal_merge.py subprocess calling doesn`t work for me.

My Error is:

OSError: [WinError 193] %1 is not a valid Win32 application

import numpy as np
import matplotlib.pyplot as plt
import subprocess, glob
from osgeo import gdal

files_to_mosaic = glob.glob('C:/Users/DanielKovacs/Documents/GranCanaria/mosaic_L2A_T28RDS_A025431_20200505T115222_2020- 05-05_con/*_B04.tif')
files_to_mosaic

files_string = " ".join(files_to_mosaic)
print(files_string)

command = "gdal_merge.py -o mosaic2.tif -of gtiff " + files_string
output = subprocess.run(command)
output

Best Answer

You can probably do this with gdal.Warp in Python directly:

from osgeo import gdal

files_to_mosaic = ["a.tif", "b.tif"] # However many you want.
g = gdal.Warp("output.tif", files_to_mosaic, format="GTiff",
              options=["COMPRESS=LZW", "TILED=YES"]) # if you want
g = None # Close file and flush to disk

There's loads of other options you can add on to this.

Related Question