Python equivalent of gdalbuildvrt

by Brian   Last Updated October 17, 2018 12:22 PM

Is there a way to perform the same task as the gdalbuildvrt utility using GDAL Python bindings? So far I have not found any way to do this other than creating a vrt of a single dataset and manually editing the xml. I would like to create a vrt from multiple rasters (essentially performing a mosaic). Is this possible using pure Python? My other option is to use subprocess to simply call gdalbuildvrt.



Answers 2


Honestly it's easier to do this by using gdalbuildvrt in a subprocess or os.system.

Should you wish to do this through Python it can be done. Using the standard dataset creation methods within GDAL Python we can easily create the base dataset VRT.

from osgeo import gdal

drv = gdal.GetDriverByName("VRT")
vrt = drv.Create("test.vrt", x_size, y_size, 0)

Note that we are creating the dataset with no bands initially. From the documentation on VRTs that VRT datasets are one of the few dataset types that can accept AddBand arguments.

vrt.AddBand(gdal.GDT_Float32)
band = vrt.GetRasterBand(1)

Now for each band we have to set the metadata items manually:

simple_source = '<SourceFilename relativeToVRT="1">%s</SourceFilename>' % source_path + \
    '<SourceBand>%i</SourceBand>' % source_band + \
    '<SourceProperties RasterXSize="%i" RasterYSize="%i" DataType="Real" BlockXSize="%i" BlockYSize="%i"/>' % (x_size, y_size, x_block, y_block) + \
    '<SrcRect xOff="%i" yOff="%i" xSize="%i" ySize="%i"/>' % (x_offset, y_offset, x_source_size, y_source_size) + \
    '<DstRect xOff="%i" yOff="%i" xSize="%i" ySize="%i"/>' % (dest_x_offset, dest_y_offset, x_dest_size, y_dest_size)
band.SetMetadataItem("SimpleSource", simple_source)
band.SetMetadataItem("NoDataValue", -9999)

SetMetadatItem takes two arguments, the first a string of the metadata item, the second the item itself. This means that you can't subset a metadata item, so for data sources you have to set the entire contents as a string.

Note that we can use this method to create complex sources (ComplexSource) that contain look-up-tables of values, Kernel filter sources (KernelFilteredSource) of arbitrary sizes and shapes, and Mask Bands (MaskBand).

om_henners
om_henners
December 15, 2012 12:48 PM

Since GDAL 2.1 the CLI tools are available as library functions, and in fact that's what the CLI tools now call internally.

For example:

gdalbuildvrt -r cubic -addalpha my.vrt one.tif two.tif

Is the equivalent of:

from osgeo import gdal

vrt_options = gdal.BuildVRTOptions(resampleAlg='cubic', addAlpha=True)
gdal.BuildVRT('my.vrt', ['one.tif', 'two.tif'], options=vrt_options)

The available CLI options directly map to the parameters of BuildVRTOptions, plus there's some extras like progress callbacks available.

rcoup
rcoup
October 17, 2018 12:03 PM

Related Questions


Why result of merge of multiple raster is so big?

Updated July 23, 2018 22:22 PM



Some tiff not included on a vrt index

Updated October 13, 2017 20:22 PM

Virtual Raster Mosaic - gdalbuildvrt - data gaps

Updated May 15, 2018 15:22 PM