![]() |
![]()
| ![]() |
![]()
NAMEgdal_calc - Command line raster calculator with numpy syntax. SYNOPSISgdal_calc [--help] [--help-general] 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 the --extent option is used), but no projection checking is performed (unless the --projectionCheck option is used). NOTE: gdal_calc is a Python utility, and is only available if
GDAL Python bindings are available.
Using the Python API: None value will indicate
default datatype specific value. 'none' value will indicate not setting
a NoDataValue.
Despite the datatype set using --type, when doing
intermediate arithmetic operations using operands of the same type, the
operation result will honor the original datatype. This may lead into
unexpected results in the final result.
NOTE: UInt64, Int64, CInt16, CInt32, CFloat32, CFloat64 have
been added in GDAL 3.5.3 Int8 has been added in GDAL 3.7
PYTHON OPTIONSAdded in version 3.3. The following options are available by using the Python interface of gdal_calc. They are not available using the command prompt.
EXAMPLESExample 1: Average of two filesgdal_calc -A input1.tif -B input2.tif --outfile=result.tif --calc="(A+B)/2" CAUTION: If A and B inputs both have integer data types, integer
division will be performed. To avoid this, you can convert of one of the
operands to a floating point type before the division operation.
gdal_calc -A input.tif -B input2.tif --outfile=result.tif --calc="(A.astype(numpy.float64) + B) / 2" Example 2: Summing three filesgdal_calc -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="A+B+C" Example 3: Combining three files into a 3D array and summinggdal_calc -A input1.tif -A input2.tif -A input3.tif --outfile=result.tif --calc="numpy.sum(A,axis=0)". Example 4: Average of three filesgdal_calc -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="(A+B+C)/3" Example 5: Average of three files, using 3D arraygdal_calc -A input1.tif input2.tif input3.tif --outfile=result.tif --calc="numpy.average(a,axis=0)". Example 6: Maximum of three filesgdal_calc -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="numpy.max((A,B,C),axis=0)" Example 7: Maximum of three files, using a 3D arraygdal_calc -A input1.tif input2.tif input3.tif --outfile=result.tif --calc="numpy.max(A,axis=0)" Example 8: Setting values of zero and below to NODATAgdal_calc -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0 Example 9: Using logical operator to keep a range of values from inputgdal_calc -A input.tif --outfile=result.tif --calc="A*logical_and(A>100,A<150)" Example 10: Performing two calculations and storing results in separate bandsgdal_calc -A input.tif --A_band=1 -B input.tif --B_band=2 \ Example 11: Add a raster to each band in a 3-band rastergdal_calc -A 3band.tif -B 1band.tif --outfile result.tif --calc "A+B" --allBands A The result will have three bands, where each band contains the values of 1band.tif added to the corresponding band in 3band.tif. Example 12: Add two three-band rastersgdal_calc -A 3band_a.tif -B 3band_b.tif --outfile result.tif --calc "A+B" --allBands A --allBands B The result will have three bands, where each band contains the values of the corresponding band of 3band_a.tif added to the corresponding band of 3band_b.tif. AUTHORChris Yesson <chris dot yesson at ioz dot ac dot uk>, Etienne Tourigny <etourigny dot dev at gmail dot com> COPYRIGHT1998-2025
|