gdal
gdal copied to clipboard
Calculate average for overlapping parts when merging rasters
I tried using the Merge tool in QGIS 3.16.10 (based on GDAL 3.1.4) to merge a few raster files. The files overlap with a few hundred meters and it would be very useful if GDAL could calculate the average value for the raster files (2 or more files) where they overlap. See for example how FME allows this kind of manipulation, http://docs.safe.com/fme/html/FME_Desktop_Documentation/FME_Transformers/Transformers/rastermosaicker.htm.
QGIS issue: https://github.com/qgis/QGIS/issues/46990
You can probably achieve that with gdalwarp -r average. gdal_merge.py is a simple/simplistic tool
I think that gdalwarp does not do it or I just do not know how. Simple test with overlapping red (1) and green (2) images
gdal_create -outsize 100 100 -a_ullr 0 100 100 0 -bands 3 -burn 255 -burn 0 -burn 0 img1.tif
gdal_create -outsize 100 100 -a_ullr 50 100 150 0 -bands 3 -burn 0 -burn 255 -burn 0 img2.tif
gdalwarp -r average img2.tif img1.tif warped.tif

The latter image, red, covers the first one. There was an initiative that should be perfect for this kind of tasks https://gdal.org/development/rfc/rfc62_raster_algebra.html, if implemented.
you're right. The resampling method in gdalwarp is about how to resample source pixels from a single source. For the purpose you want to accmoplish you'll need to use several steps:
- compute the overlapping area
- use gdal_translate/gdalwarp to extract from each source the overlapping area
- use gdal_calc to do the average
- and then composite the result with gdalwarp
is it not possible to do the same using gdalvrt?