1

I am trying to build overviews in a VRT dataset with python GDAL.

I try this code:

out_vrt = os.path.join(out_dir, 'mosaic.vrt')
ds = gdal.BuildVRT(out_vrt, [raster1.tif, raster2.tif])
factors = [128, 256, 512]
gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
ds.BuildOverviews("AVERAGE", factors)

, but I get this error:

ERROR 6: /home/umberto/Documents/jrc/FB_Data4Good/mosaic/test/vrt/mosaic.vrt: BuildOverviews() not supported for this dataset.

The VRT mosaic is created, but the overviews are not.

As far as I know, gdaladdo should work with VRTs, but I am not sure if gdal.BuildVRT() is actually using it.

Any idea?

umbe1987
  • 3,785
  • 2
  • 23
  • 56

1 Answers1

1

Solved by looking at this answer:

out_vrt = os.path.join(out_dir, 'mosaic.vrt')
ds = gdal.BuildVRT(out_vrt, [raster1.tif, raster2.tif])
ds = None # this did the trick!
ds = gdal.Open(out_vrt, 0)  # 0 = read-only, 1 = read-write. 
factors = [128, 256, 512]
gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
ds.BuildOverviews("AVERAGE", factors)

Explanation (quoting one of the comments in the answer I mentioned):

"Note that the CreateCopy() method returns a writable dataset, and that it must be closed properly to complete writing and flushing the dataset to disk. In the Python case this occurs automatically when "dst_ds" goes out of scope." SInce there is no closing for python, you have to bring your vrt out of scope, by assign it to None.

BUG FIXED

This was a bug that has been fixed (see
[gdal-dev] [Python bindings] BuildOverviews() not supported for VRT dataset
)

umbe1987
  • 3,785
  • 2
  • 23
  • 56