ST_MapAlgebra merges multiple raster objects into a single raster object by applying algebraic expressions written in Algebra Computing Language.
Syntax
raster ST_MapAlgebra(raster[] rasters,
cstring algebraExpr default NULL,
cstring storageoption default '')Parameters
| Parameter | Description |
|---|---|
rasters | The input raster objects to transform. |
algebraExpr | A JSON string that defines the algebraic expressions to apply. See algebraExpr parameter. |
storageOption | A JSON string that controls how the output raster is stored. See storageOption parameter. |
ST_MapAlgebra only requires that the input rasters share the same unit of measurement for length and width. It does not check spatial reference systems or resolutions. If your rasters use different units, use ST_Transform, ST_Resize, and ST_Clip to align them first.
algebraExpr parameter
The algebraExpr parameter takes a JSON array. Each object in the array defines one output band using the following fields.
Fields
| Field | Type | Default | Description |
|---|---|---|---|
algebraExpr | String | — | The algebraic expression for this band. |
nodata | Boolean | false | Controls how nodata pixels are handled. true: nodata pixels are treated as valid and are not transformed. false: nodata pixels are treated as invalid and are transformed. |
nodataValue | float8 | 0 | The nodata value written to the output raster. |
Expression keywords
Use the following keywords in algebraic expressions to reference pixel values:
[r, b]— The pixel value at raster indexrand band indexb, both zero-based (0 to n−1).x— The zero-based column index of the current pixel.y— The zero-based row index of the current pixel.
Supported operations
| Type | Operators and functions | Notes |
|---|---|---|
| Arithmetic | +, -, *, /, % (remainder), ** (power) | — |
| Bitwise | <<, >>, &, |, ^ | — |
| Logical | <, >, ==, !=, <=, >=, &&, ||, ! | — |
| Math functions | abs, sqrt, exp, log, ln, sin, cos, tan, sinh, cosh, tanh, arcsin, arccos, arctan, ceil, floor, round | Only one math function per expression. |
| Statistical functions | min, max, sum, mean, majority, minority, std, median, range, variety | Requires two or more arguments. |
algebraExpr examples
Example 1 — Single-band output using an arithmetic expression
Produces a one-band raster using raster[0]band[0] + raster[1]band[0] * raster[1]band[1]:
[
{
"expr": "([0,0] + [1,0] * [1,1]) ",
"nodata": true,
"nodataValue": 999
}
]Example 2 — Single-band output using a statistical function
Computes the standard deviation across three bands of the first raster:
[
{
"expr": "(std([0,0],[0,1],[0,2]))",
"nodata": true,
"nodataValue": 999
}
]Example 3 — Multi-band output with a unique expression per band
Produces a three-band raster, applying min, max, and mean across three bands:
[
{
"expr": "(min([0,0],[0,1],[0,2]))",
"nodata": true,
"nodataValue": 999
},
{
"expr": "(max([0,0],[0,1],[0,2]))",
"nodata": true,
"nodataValue": 999
},
{
"expr": "(mean([0,0],[0,1],[0,2]))",
"nodata": true,
"nodataValue": 999
}
]storageOption parameter
| Field | Type | Default | Description |
|---|---|---|---|
chunking | Boolean | Same as input raster | Whether to store the output raster as chunks. |
chunkdim | String | Same as input raster | The chunk dimensions. Takes effect only when chunking is true. |
chunktable | String | '' (empty string) | The name of the chunk table. By default, a temporary table with a random name is created and is only valid for the current session. Specify a name to store the output permanently. |
compression | String | Same as input raster | The compression format. Valid values: None, JPEG, Zlib, PNG, LZO, LZ4. |
quality | Integer | Same as input raster | The image quality. Takes effect only in JPEG format. |
interleaving | String | Same as input raster | The interleaving type. Valid values: bip (band interleaved by pixel, BIP), bil (band interleaved by line, BIL), bsq (band sequential, BSQ). |
endian | String | Same as input raster | The byte order. Valid values: NDR (little endian), XDR (big endian). |
Examples
The following example creates a permanent chunk table, then inserts the result of ST_MapAlgebra into it. Both rasters are collected into an array, and a single arithmetic expression combines band 0 from the first raster with band 0 and band 1 from the second raster.
-- Create a permanent chunk table.
CREATE TABLE rast_mapalgebra_result(id integer, rast raster);
-- Insert data into a chunk table.
WITH foo AS (
SELECT 1 AS rid, rast AS rast from t1 WHERE id = 1
UNION ALL
SELECT 2 AS rid, rast AS rast from t2 WHERE id = 2
)
INSERT INTO rast_mapalgebra_result
SELECT 1, ST_MapAlgebra(
ARRAY(SELECT rast FROM foo ORDER BY rid),
'[{"expr":"([0,0] + 0.5 * [1,0] - ([1,1])","nodata": true, "nodataValue":999}]',
'{"chunktable":"algebra_rbt"}'
);