1

(QGIS3) I'm trying to create a raster layer that is the sum of 2 layers, but I do not want to clip to the smallest extent. The raster calculator in QGIS automatically trims the new file to the smallest extent, and thus far, I have not found how to prevent this from happening, though I suspect there is a small script that I could add to do so.

The two layers in question are both raster layers with very small and specific pixel sizes, 0.002083333301204805168,-0.002083333297619031996 specifically, and the extents are 83x84 and 1079x1079. As such, trying to save the smaller layer to a larger extent does not work because the pixel size warps due to QGIS not saving enough decimal points in the export tool. Perhaps another solution to my problem would be increasing the extent this way and then resampling, but I am not familiar with how the resampling tools work on QGIS or which would be the most applicable for my scenario. I've heard of gdalwarp which I think might contain an answer somewhere and someone has offered me baseline script using the software R which I'm not very comfortable with. I'm hoping someone just has a small script add on that I can enter into the raster calculator tool which can omit the clipping function, but any other solutions or advice would be helpful. I would like to repeat this process with the one smaller layer and several larger raster layers to produce a number of different summed layers if a particular solution might make it quicker for me to do so.

1 Answer 1

2

The QGIS raster calculator is a front end to the gdal_calc Python utility https://gdal.org/programs/gdal_calc.html. The utility has an option for controlling the behavior with extent:

--extent=<option>

New in version 3.3.

This option determines how to handle rasters with different extents. This option is mutually exclusive with the projwin option, which is used for providing a custom extent.

For all the options below the pixel size (resolution) and SRS (Spatial Reference System) of all the input rasters must be the same.

ignore (default) - only the dimensions of the rasters are compared. if the dimensions do not agree the operation will fail.

fail - the dimensions and the extent (bounds) of the rasters must agree, otherwise the operation will fail.

union - the extent (bounds) of the output will be the minimal rectangle that contains all the input extents.

intersect - the extent (bounds) of the output will be the maximal rectangle that is contained in all the input extents.

I think that you would like to use the option "union". Give --extent=union as an extra option and have a try.

enter image description here

6
  • I'm not quite sure I know how to use this program. I went into QGIS, processing tools, GDAL raster calculator, and tried filling out the tool and using --extent=union in the additional command line parameters, but it said that it did not recognize --extent as a command. I'm on QGIS version 3.18.1-Zurich if that makes a difference and I need to update. Or perhaps there is a way to update the GDAL calculator within QGIS? I did notice that the script in that tool says gdal_calc.bat instead of gdal_calc.py, I'm not sure if there is a different tool I should be using, but any advice would be nice. Nov 11, 2022 at 17:17
  • Copy the whole gdal_calc script from the window. It should now contain the added --extent=union part. The gdal_calc.bat is executing gdal_calc.py so for you they mean the same.
    – user30184
    Nov 11, 2022 at 19:45
  • gdal_calc.bat --calc "A+B" --format GTiff --type Float32 -A C:/Users/cwdil/OneDrive/Documents/Final_Landslides_4326_01.tif --A_band 1 -B C:/Users/cwdil/Downloads/MoPo330_jessee_2017_model.tif --B_band 1 --extent=union --outfile C:/Users/cwdil/AppData/Local/Temp/processing_RyYTVZ/8d38ce641ea14526aa87c85e0e4d87ba/OUTPUT.tif This is what the script looks like. But I get an error saying: Usage: gdal_calc.py --calc=expression --outfile=out_filename [-A filename] [--A_band=n] [-B...-Z filename] [other_options] gdal_calc.py: error: no such option: --extent Process returned error code 2 Nov 11, 2022 at 20:58
  • Maybe your QGIS version comes with too old GDAL version. See gdal.org/programs/gdal_calc.html New in version 3.3. You can check the GDAL version from the QGIS menu Help - About.
    – user30184
    Nov 11, 2022 at 21:39
  • I updated to QGIS 3.24 and got --extent=union to produce an output with the right extent, but outside of the extent of the smaller raster, it is all just no data pixels. Is there any way for those pixels to contain the value of the larger raster? Like calculate a sum where the value equals A+B where A and B both have a value, but just A where B is no data. Nov 12, 2022 at 19:27

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.