gdal_calc.py

具有numpy语法的命令行栅格计算器。

简介

gdal_calc.py --calc=expression --outfile=out_filename [-A filename]
             [--A_band=n] [-B...-Z filename] [other_options]

DESCRIPTION

具有numpy语法的命令行栅格计算器。使用numpy数组支持的任何基本算法,如 +-*\ 以及逻辑运算符,如 > . 请注意,所有文件必须具有相同的尺寸(除非使用了“范围”选项),但不执行投影检查(除非使用了“投影检查”选项)。

--help

显示此帮助消息并退出

-h

一样 --help .

--calc=expression

Calculation in numpy syntax using +, -, /, *, or any numpy array functions (i.e. log10()). Multiple --calc options can be listed to produce a multiband file (GDAL >= 3.2).

-A <filename>

输入gdal栅格文件,可以使用任意字母(a-z,a-z)。(从GDAL 3.3开始支持小写)

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.

--A_band=<n>

文件A的栅格标注栏数(默认为1)。

--outfile=<filename>

要生成或填充的输出文件。

--NoDataValue=<value>

输出NoDataValue(默认数据类型特定值)。要指示未设置NoDataValue,请使用--NoDataValue=none(GDAL>=3.3)

备注

使用Python API: None 值将指示特定于数据类型的默认值。 'none' 值将指示未设置NoDataValue。

--hideNoData

3.3 新版功能.

忽略输入值。默认情况下,输入值不参与计算。通过设置此设置-不会对输入nodata值执行特殊处理。它们将作为任何其他值参与计算。除非您通过设置--NoDataValue=<value>显式指定了特定的值,否则输出将没有设置NoDataValue。

--type=<datatype>

输出数据类型,必须是 [Int32, Int16, Float64, UInt16, Byte, UInt32, Float32] .

备注

尽管数据类型集使用 --type ,当使用相同类型的操作数执行中间算术运算时,运算结果将遵循原始数据类型。最终可能导致意想不到的结果。

--format=<gdal_format>

输出文件的GDAL格式。

--color-table=<filename>

Allows specifying a filename of a color table (or a ColorTable object) (with Palette Index interpretation) to be used for the output raster. Supported formats: txt (i.e. like gdaldem, but color names are not supported), qlr, qml (i.e. exported from QGIS)

--extent=<option>

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.

--projwin <ulx> <uly> <lrx> <lry>

3.3 新版功能.

This option provides a custom extent for the output, it is mutually exclusive with the extent option.

--projectionCheck

3.3 新版功能.

默认情况下,不执行投影检查。通过设置此选项,如果所有波段的投影不相同,则操作将失败。

--creation-option=<option>

将创建选项传递给输出格式驱动程序。可能会列出多个选项。有关每个格式的合法创建选项,请参见特定于格式的文档。

--co=<option>

一样 creation-option.

--allBands=[a-z, A-Z]

处理给定栅格(a-z,a-z)的所有波段。所有标注栏都需要一个计算。

--overwrite

Overwrite output file if it already exists. Overwriting must be understood here as deleting and recreating the file from scratch. Note that if this option is not specified and the output file already exists, it will be updated in place.

--debug

打印调试信息。

--quiet

禁止显示进度消息。

Python选项

3.3 新版功能.

The following options are available by using function the python interface of gdal_calc. They are not available using the command prompt.

user_namespace

在计算表达式中使用的自定义函数或其他名称的字典。

return_ds

如果启用,输出数据集将从函数返回而不是关闭。

color_table

Allows specifying a ColorTable object (with Palette Index interpretation) to be used for the output raster.

例子

将两个文件添加到一起:

gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"

平均两层:

gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="(A+B)/2"

备注

在前面的示例中,请注意,如果A和B输入是相同的数据类型(例如整数),则可能需要在除法运算之前强制转换其中一个操作数。

gdal_calc.py -A input.tif -B input2.tif --outfile=result.tif --calc="(A.astype(numpy.float64) + B) / 2"

将三个文件添加到一起(具有相同结果的两个选项):

gdal_calc.py -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="A+B+C"

3.3 新版功能.

gdal_calc.py -A input1.tif -A input2.tif -A input3.tif --outfile=result.tif --calc="numpy.sum(A,axis=0)".

三个选项的平均结果:

gdal_calc.py -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="(A+B+C)/3"

3.3 新版功能.

gdal_calc.py -A input1.tif input2.tif input3.tif --outfile=result.tif --calc="numpy.average(a,axis=0)".

最多三层(具有相同结果的两个选项):

gdal_calc.py -A input1.tif -B input2.tif -C input3.tif --outfile=result.tif --calc="numpy.max((A,B,C),axis=0)"

3.3 新版功能.

gdal_calc.py -A input1.tif input2.tif input3.tif --outfile=result.tif --calc="numpy.max(A,axis=0)"

将0和以下的值设置为空:

gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0

使用逻辑运算符保留输入值的范围:

gdal_calc.py -A input.tif --outfile=result.tif --calc="A*logical_and(A>100,A<150)"

使用多个频段:

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)"