GDAL_CALC(1) | GDAL | GDAL_CALC(1) |
gdal_calc - Command line raster calculator with numpy syntax.
gdal_calc.py --calc=expression --outfile=out_filename [-A filename]
[--A_band=n] [-B...-Z filename] [other_options]
DESCRIPTION
Command line raster calculator with numpy syntax. Use any basic arithmetic supported by numpy arrays such as +, -, *, and / along with logical operators such as >. Note that all files must have the same dimensions (unless extent option is used), but no projection checking is performed (unless projectionCheck option is used).
A letter may be repeated, or several values (separated by space) can be provided (GDAL >= 3.3). Since GDAL 3.5, wildcard exceptions (using ?, *) are supported for all shells/platforms. The effect will be to create a 3-dim numpy array. In such a case, the calculation formula must use this input as a 3-dim array and must return a 2D array (see examples below). In case the calculation does not return a 2D array an error would be generated.
NOTE:
Ignores the input bands NoDataValue. By default, the input bands NoDataValue are not participating in the calculation. By setting this setting - no special treatment will be performed on the input NoDataValue. and they will be participating in the calculation as any other value. The output will not have a set NoDataValue, unless you explicitly specified a specific value by setting --NoDataValue=<value>.
NOTE:
NOTE:
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.
This option provides a custom extent for the output, it is mutually exclusive with the extent option.
By default, no projection checking will be performed. By setting this option, if the projection is not the same for all bands then the operation will fail.
New in version 3.3.
The following options are available by using function the python interface of gdal_calc. They are not available using the command prompt.
Add two files together:
gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"
Average of two layers:
gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="(A+B)/2"
NOTE:
gdal_calc.py -A input.tif -B input2.tif --outfile=result.tif --calc="(A.astype(numpy.float64) + B) / 2"
Add three files together (two options with the same result):
gdal_calc.py -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="A+B+C"
New in version 3.3.
gdal_calc.py -A input1.tif -A input2.tif -A input3.tif --outfile=result.tif --calc="numpy.sum(A,axis=0)".
Average of three layers (two options with the same result):
gdal_calc.py -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="(A+B+C)/3"
New in version 3.3.
gdal_calc.py -A input1.tif input2.tif input3.tif --outfile=result.tif --calc="numpy.average(a,axis=0)".
Maximum of three layers (two options with the same result):
gdal_calc.py -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="numpy.max((A,B,C),axis=0)"
New in version 3.3.
gdal_calc.py -A input1.tif input2.tif input3.tif --outfile=result.tif --calc="numpy.max(A,axis=0)"
Set values of zero and below to null:
gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0
Using logical operator to keep a range of values from input:
gdal_calc.py -A input.tif --outfile=result.tif --calc="A*logical_and(A>100,A<150)"
Work with multiple bands:
gdal_calc.py -A input.tif --A_band=1 -B input.tif --B_band=2 --outfile=result.tif --calc="(A+B)/2" --calc="B*logical_and(A>100,A<150)"
Chris Yesson <chris dot yesson at ioz dot ac dot uk>, Etienne Tourigny <etourigny dot dev at gmail dot com>
1998-2023
January 2, 2023 |