gdal icon indicating copy to clipboard operation
gdal copied to clipboard

Calculate average for overlapping parts when merging rasters

Open magnusnil opened this issue 2 years ago • 4 comments

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

magnusnil avatar Jan 26 '22 09:01 magnusnil

You can probably achieve that with gdalwarp -r average. gdal_merge.py is a simple/simplistic tool

rouault avatar Jan 26 '22 10:01 rouault

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
image

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.

jratike80 avatar Jan 26 '22 11:01 jratike80

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

rouault avatar Jan 26 '22 12:01 rouault

is it not possible to do the same using gdalvrt?

rimds avatar Mar 18 '24 12:03 rimds