Is it possible to merge raster bands from several folders using GDAL?

Viewed 57

I have two folders containing about 15 000 .tif files. Each file in the first folder is a raster with 5 bands, named AA_"number" meaning it looks like

AA_1.tif, AA_2.tif, ..., AA_15000.tif.

Each file in the second folder is a raster with 2 bands named BB_"number" and looks like

BB_1.tif, BB_2.tif, ..., BB_15000.tif.

My goal is to add bands 1-3 from first file from folder AA with band 1 from the first file in folder BB to create a 4 band raster, and make 15000 4 band rasters. After doing some research and testing things out in QGIS I believe the tool Merge from GDAL could solve this task, but I have not been able make it find the right files in different folders. And as I have 2x 15 000 files, it is not possible to do this selection manually. Is there anyone who know a smart solution to this, preferably using GDAL or QGIS?

1 Answers

There are many ways to do this, and it really depends on what the exact use case is. Like the type of analysis/visualization that needs to be done on the result.

With this many files, it could for example be nice to merge them using a VRT. That will avoid creating redundant data, but whether that's actually the best solution depends. Just stacking them in a new tiff-file would of course also work.

Unfortunately, creating a VRT using gdalbuildvrt / gdal.BuildVRT is not possible with multi-band inputs.

If your inputs are homogeneous in terms of properties, it should be fairly simple to set up a template where you fill in the file locations and write the VRT to disk. For more inputs with heterogeneous properties it might still be possible, but you'll have to be careful to take it all into account.

Conceptually such a VRT would look something like:

<VRTDataset rasterXSize="..." rasterYSize="...">
  <SRS>...</SRS>
  <GeoTransform>....</GeoTransform>

  <VRTRasterBand dataType="..." band="1">
    <ComplexSource>
      <SourceFilename relativeToVRT="0">//some_drive/aa_folder/aa_file1.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      ...
    </ComplexSource>
  </VRTRasterBand>

  <VRTRasterBand dataType="..." band="2">
    <ComplexSource>
      <SourceFilename relativeToVRT="0">//some_drive/aa_folder/aa_file1.tif</SourceFilename>
      <SourceBand>2</SourceBand>
      ...
    </ComplexSource>
  </VRTRasterBand>

  <VRTRasterBand dataType="..." band="3">
    <ComplexSource>
      <SourceFilename relativeToVRT="0">//some_drive/aa_folder/aa_file1.tif</SourceFilename>
      <SourceBand>3</SourceBand>
      ...
    </ComplexSource>
  </VRTRasterBand>


  <VRTRasterBand dataType="..." band="4">
    <ComplexSource>
      <SourceFilename relativeToVRT="0">//some_drive/bb_folder/bb_file1.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      ...
    </ComplexSource>
  </VRTRasterBand>

</VRTDataset>

You can first use gdalbuildvrt on some of your files to find all the properties that need to be filled in, like projection, pixel dimensions etc. That will work, but gdalbuildvrt will only be able to take the first band from the inputs. If all bands have homogeneous properties (like nodata value etc), that should be fine as a reference.

Related