The raster_calculator tool can be used to perform a complex mathematical operations on one or more input raster images on a cell-to-cell basis. The user inputs an expression
and a list of input rasters (input_rasters
), specified in the same order as the rasters contained within the statement. Rasters are treated like variables (that change value with each grid cell) and are specified within the statement as arbitrarily named variables contained within either double or single quotation marks (e.g. "DEM" > 500.0). The order of raster variables must match the order of rasters within the input_rasters
list.**Note, all input rasters must share the same number of rows and columns and spatial extent. Use the resample tool if this is not the case to convert the one raster's grid resolution to the others.
(band3, band4) = wbe.read_rasters('band3.tif', 'band4.tif') result = wbe.raster_calculator("('nir' - 'red') / ('nir' + 'red')", [band4, band3]) wbe.write_raster(result, 'result.tif', True)
The mathematical expression supports all of the standard algebraic unary and binary operators (+ - * / ^ %), as well as comparisons (< <= == != >= >) and logical operators (&& ||) with short-circuit support. The order of operations, from highest to lowest is as follows.
Listed in order of precedence:
Order | Symbol | Description |
(Highest Precedence) | ^ | Exponentiation |
% | Modulo | |
/ | Division | |
* | Multiplication | |
- | Subtraction | |
+ | Addition | |
== != < <= >= > | Comparisons (all have equal precedence) | |
&& and | Logical AND with short-circuit | |
(Lowest Precedence) | || or | Logical OR with short-circuit |
Several common mathematical functions are also available for use in the input statement. For example:
* log(base=10, val) -- Logarithm with optional 'base' as first argument. If not provided, 'base' defaults to '10'. Example: log(100) + log(e(), 100)
pi() -- π (3.141592653589793)
int(val)
round(modulus=1, val) -- Round with optional 'modulus' as first argument. Example: round(1.23456) == 1 && round(0.001, 1.23456) == 1.235
abs(val)
sign(val)
min(val, ...) -- Example: min(1, -2, 3, -4) == -4
max(val, ...) -- Example: max(1, -2, 3, -4) == 3
sin(radians) * asin(val)
pi()
and e()
. A number of global variables are also available to build conditional statements. These include the following:Special Variable Names For Use In Conditional Statements:
Name | Description |
`nodata` | An input raster's NoData value. |
`null` | Same as `nodata`. |
`minvalue` | An input raster's minimum value. |
`maxvalue` | An input raster's maximum value. |
`rows` | The input raster's number of rows. |
`columns` | The input raster's number of columns. |
`row` | The grid cell's row number. |
`column` | The grid cell's column number. |
`rowy` | The row's y-coordinate. |
`columnx` | The column's x-coordinate. |
`north` | The input raster's northern coordinate. |
`south` | The input raster's southern coordinate. |
`east` | The input raster's eastern coordinate. |
`west` | The input raster's western coordinate. |
`cellsizex` | The input raster's grid resolution in the x-direction. |
`cellsizey` | The input raster's grid resolution in the y-direction. |
`cellsize` | The input raster's average grid resolution. |
The special variable names are case-sensitive. If there are more than one raster inputs used in the statement, the functional forms of the nodata
, null
, minvalue
, and maxvalue
variables should be used, e.g. nodata("InputRaster")
, otherwise the value is assumed to specify the attribute of the first raster in the statement. The following are examples of valid statements:
"raster" != 300.0
"raster" >= (minvalue + 35.0)
("raster1" >= 25.0) && ("raster2" <= 75.0) -- Evaluates to 1 where both conditions are true.
tan("raster" * pi() / 180.0) > 1.0
"raster" == nodata Any grid cell in the input rasters containing the NoData value will be assigned NoData in the output raster, unless a NoData grid cell value allows the statement to evaluate to True (i.e. the mathematical expression includes the nodata
value).
ConditionalEvaluation
def raster_calculator(self, expression: str, input_rasters: List[Raster]) -> Raster: ...