2

In QGIS 3.24 I've got two rasters, one that is 1079x1079 and another that is 83x84, but they both have a very specific pixel size, 0.002083333333333333304,-0.002083333333333333304.

I'd like to add them together such that the value of the output pixel is A+B wherever A and B both have a value and A where B is no data. The raster calculator in QGIS 3.24 always trims to the extent of the smaller raster, unfortunately, and when I try to save the smaller raster to the same extent as the larger one, the pixel size distorts just a tiny bit which is not going to work for what I'm trying to accomplish. Is it possible to align both of the rasters again after I make the smaller one larger?

I need them to both ends at that exact pixel size that they were at before due to other layers on my project, so I'm not sure if the align raster tool will work or if it will tweak the size as well. I tried --extent=union in the gdal calculator, but it leaves no data values outside of the A+B section instead of values for A. A colleague also told me that gdalwarp might help, but as far as I can tell, that only reprojects to a different CRS, and it doesn't resample the data and project it onto the same cell size as another file. Are any other possibilities out there?

3
  • Which extent did you set when running the raster calculator?
    – Erik
    Commented Nov 15, 2022 at 8:15
  • You are wrong about gdalwarp. If you warp image B into an existing image A it will update the pixels of A as documented in gdal.org/programs/gdalwarp.html. However, it does not do raster algebra and calculate A+B for you. Please provide test data for A and B, perhaps there is a way to deal with the nodata in a right way with gdal_calc. And you could mention and add a link to your previous question gis.stackexchange.com/questions/444316/….
    – user30184
    Commented Nov 15, 2022 at 8:20
  • 1
    @Erik The extent of the smaller raster is contained inside that of the larger, so I tried to use the extent of the larger raster, but the raster calculator in QGIS trims to the extent of the smallest one by default. I would simply save the smaller raster with a larger extent of no value cells, but this distorts the size of the pixels and prevents them from lining up correctly with other files in the project. Commented Nov 15, 2022 at 20:44

1 Answer 1

1

Make sure both rasters are in the same CRS. The trick to get the whole extent is to create a virtual raster.

  1. In case the initial rasters contain nodata values, use Fill nodata cells on both rasters and use 0 as Fill value.

  2. Create a virtual raster out of your raster_A and raster_B - so the resulting virtual_raster has the extent of both rasters.

  3. Then use GDAL raster calculator to add raster1 and raster2. Set output nodata value to 0 and output extent to the extent of the virtual layer. Then use this expression for calculation: raster1 + raster2

    See screenshot: raster2 in grayscale, raster1 in color: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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