In this post we are going to learn how to merge massive amounts of raster files automatically using PyQGIS. This way we will avoid all the crashes and troubles associated with the processing script found in the toolbox. And of course it saves us all the trouble of having to manually select the files.
TL; DR, the solution
We will arrive to the solution after taking two simple steps. The first step will be creating a VRT file. A VRT file is a Virtual Dataset that contains all of the images merged. Second and final step will be just to have that file translated to the desired extension.
vrt_path = os.path.join(BASE_PATH, 'prov_vrt.vrt') vrt = gdal.BuildVRT(vrt_path, list) result = os.path.join(BASE_PATH, 'merged.tif') gdal.Translate(result, vrt, format='GTiff')
Here the variable list is a list composed of the path of each and every file we want to merge. This is important, don’t rush with the copypasting or you will get an error.
Complete solution
If you are starting from scratch you will have to import the libraries os and pathlib. They will help us explore folders and files with ease.
In this code glob will help us filter the files in the folder to only those with the extension TIF.
from pathlib import Path from osgeo import gdal import os folder_path = r"C:\Users\Usuario\Desktop\input" BASE_PATH = os.path.dirname(os.path.abspath(folder_path)) folder = Path(folder_path) l = [] for f in folder.glob('**/*.tif'): f_path = f.as_posix() l.append(f_path) vrt_path = os.path.join(BASE_PATH, 'prov_vrt.vrt') vrt = gdal.BuildVRT(vrt_path, l) result = os.path.join(BASE_PATH, 'merged.tif') gdal.Translate(result, vrt, format='GTiff')
The processing script found in the toolbox as well as the use of gdal_merge imply that every image to be merged has to be loaded previously in memory. The code above bypasses this step, thus avoiding all the problems related to lack of memory.
Hello,
Maybe you can help me with this error
NameError: name ‘gdal’ is not defined
Should i define gdal or is it some complement i should call first?
Thanks
Hi Claudio,
thanks for your comment. In fact, you should import gdal at the beginning of your script. There was a mistake in the code and that part was missing, we will correct it.