3

I have built a VRT mosaic and overviews with these lines of code:

out_vrt = os.path.join('/out/dir/', 'out.vrt')
ds = gdal.BuildVRT(out_vrt, my_raster_list)
ds = None
ds = gdal.Open(out_vrt)
factors = [128, 256, 512]
gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
ds.BuildOverviews("AVERAGE", factors)

This creates the out.vrt raster referencing 187 GeoTIFF rasters in my_raster_list. These rasters already have overviews for levels [2, 4, 8, 16, 32, 64].

The code then creates the output.vrt.ovr file containing overviews for levels [128, 256, 512].

As there is no SUM algorithm for building overviews, and I need to build overviews which represent the sum of pixel values in the resampled pixels, I successfully used the AVERAGE algorithm to build overviews for the GeoTIFF rasters and then used .ReadAsArray() and .WriteArray() methods to manipulate the overview pixels to calculate the sum from the average values and the number of original pixels within the resampled pixels.

Basically, this is done with these lines:

band = ds.GetRasterBand(1)
ovr_num = band.GetOverviewCount()
factors = [128, 256, 512]

for level in range(ovr_num):
    b_ovr = band.GetOverview(level)
    arr = b_ovr.ReadAsArray()
    arr *= (factors[level])**2
    b_ovr.WriteArray(arr)

    b_ovr = None
    del b_ovr

However, I cannot do this with the VRT overviews as

Writing through VRTSourcedRasterBand is not supported

The question is: how can I overcome this problem?

I know I can write the overviews directly in the GeoTIFF rasters, but I am not quite sure if this is the right way to go, because:

  1. I am afraid overviews for large factors will look "nasty" in the final mosaic when created for single rasters instead of considering the whole 187
  2. If one of the 187 rasters needs to be updated, I would have to build overviews up to 512 level, and this would take a lot of time. Rather, I would just update it until level 64 and for lower scale I would bet I won't notice much difference if I don't update the VRT overview levels.
3

If you find a way to create GeoTIFF files which cover the whole VRT area with pixel sizes matching the overview levels 128, 256, and 512 you can point the VRT file to use them as overviews https://gdal.org/drivers/raster/vrt.html

Overview: This optional element describes one overview level for the band. It should have a child SourceFilename and SourceBand element. The SourceFilename may have a relativeToVRT boolean attribute. Multiple elements may be used to describe multiple overviews.

<Overview>
 <SourceFilename relativeToVRT="1">sum_level_128.tif</SourceFilename>  
 <SourceBand>1</SourceBand>
</Overview>
| improve this answer | |
  • Great solution! Not that this is a difficult task, but do you think I am supposed to modify the VRT XML myself to point to the overview rasters or is there an option to do that with GDAL? Thanks! – umbe1987 Mar 2 at 10:21
  • You must edit VRT XML manually. – user30184 Mar 2 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.