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()

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.

1 estrella2 estrellas3 estrellas4 estrellas5 estrellas (2 votes, average: 5.00 de 5)


Quality training taught by professionals