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
.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:
- 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
- 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.