Raster Expression
The Expression operator performs a pixel-wise mathematical expression on one or more bands of a raster source.
The expression is specified as a user-defined script in a very simple language.
The output is a raster time series with the result of the expression and with time intervals that are the same as for the inputs.
Users can specify an output data type.
Internally, the expression is evaluated using floating-point numbers.
An example usage scenario is to calculate NDVI for a red and a near-infrared raster channel.
The expression uses a raster source with the two bands red and nir and calculates the formula (nir - red) / (nir + red).
When the temporal resolution is months, our output NDVI will also be a monthly time series.
The following describes the types used in the parameters.
Expression
Section titled “Expression”Expressions are simple scripts to perform pixel-wise computations.
One can refer to the raster bands by their names, e.g., red and nir.
Furthermore, expressions can check with red IS NODATA, nir IS NODATA, etc. for NO DATA values.
This is important if mapNoData is set to true.
Otherwise, NO DATA values are mapped automatically to the output NO DATA value.
Finally, the value NODATA can be used to output NO DATA.
Users can think of this implicit function signature for, e.g., the two bands red and nir:
fn (red: f64, nir: f64) -> f64As a start, expressions contain algebraic operations and mathematical functions.
(red + nir) / 2In addition, branches can be used to check for conditions.
if red IS NODATA { nir} else { red}Function calls can be used to access utility functions.
max(red, 0)Currently, the following functions are available:
abs(a): absolute valuemin(a, b),min(a, b, c): minimum valuemax(a, b),max(a, b, c): maximum valuesqrt(a): square rootln(a): natural logarithmlog10(a): base 10 logarithmcos(a),sin(a),tan(a),acos(a),asin(a),atan(a): trigonometric functionspi(),e(): mathematical constantsround(a),ceil(a),floor(a): rounding functionsmod(a, b): division remainderto_degrees(a),to_radians(a): conversion to degrees or radians
To generate more complex expressions, it is possible to have variable assignments.
let mean = (red + nir) / 2;let coefficient = 0.357;mean * coefficientNote, that all assignments are separated by semicolons. However, the last expression must be without a semicolon.
Band names as variables
Section titled “Band names as variables”Band names are adapted to be valid variable names:
every character that is not a letter or a digit is replaced by an underscore, e.g., nir 1 becomes nir_1.
A band can only be referred to by its name if the name starts with an ASCII letter,
consists of ASCII letters, digits and underscores only, and is not a keyword of the expression language
(if, else, let, nodata, is, true, false, as well as the reserved words
and, or, not, in, for, while, loop, break, continue, return, fn and match).
Moreover, the adapted names of all bands must be different.
To use other names, rename the bands with the RasterStacker operator (parameter renameBands).
Deprecated: Bands can also be referred to as A for the first band, B for the second, and so on.
This will be removed in the future.
If a band is named like the letter of another band, e.g., the first band is named B,
the band name is used if the expression also refers to another band by its name.
Otherwise, the letter is used.
Parameters
Section titled “Parameters”| Name | Type | Description | Examples |
|---|---|---|---|
| expression | string | Expression script Example: "(nir - red) / (nir + red)" |
"(nir - red) / (nir + red)" |
| outputType | RasterDataType | A raster data type for the output | "F32" |
| outputBand | RasterBandDescriptor | Description about the output | {"name":"NDVI","measurement":{"type":"unitless"}} |
| mapNoData | boolean | Should NO DATA values be mapped with the expression? Otherwise, they are mapped automatically to NO DATA. |
true |
Sources
Section titled “Sources”| Name | Type | Description |
|---|---|---|
| raster | RasterOperator | An operator that produces raster data. |
Examples
Section titled “Examples”{ "type": "Expression", "params": { "expression": "(nir - red) / (nir + red)", "outputType": "F32", "outputBand": { "name": "NDVI", "measurement": { "type": "unitless" } }, "mapNoData": true }, "sources": { "raster": { "type": "GdalSource", "params": { "data": "ndvi" } } }}