The ST_MapAlgebra function generates a new raster object by computing pixel values from multiple source objects based on an algebraic expression.
Syntax
raster ST_MapAlgebra(raster[] rasters ,
cstringalgebraExpr default NULL,
cstringstorageoptiondefault '')Parameters
Parameter Name | Description |
rasters | An array of raster objects to use in the algebraic operation. |
algebraExpr | A JSON string that represents the algebraic expression. |
storageOption | A JSON string that represents the storage options for the result. |
The input raster objects must have the same height and width. The function does not check for spatial reference or resolution. If these properties do not match, you must first process the objects using ST_Transform, ST_Resize, and ST_Clip.
The algebraExpr parameter is a JSON array of objects. Each object specifies an algebraic expression. The parameters are as follows.
Parameter | Description | Type | Default | Notes |
algebraExpr | The algebraic expression. | string | - | - |
nodata | Specifies whether to use nodata. | boolean | false |
|
nodataValue | The nodata value. | float8 | 0 | - |
The algebraic expression for algebraExpr supports the following keywords.
[r, b]
r: The ID of the raster in the array. The value can be an integer from 0 to n-1.
b: The band number of the corresponding raster. The value can be an integer from 0 to n-1.
x
The column number of the pixel.
y
The row number of the pixel.
The expression supports the following operations:
Category | Operator/Function | Notes |
Operators |
| - |
Bitwise operations |
| - |
Logical operations |
| - |
Mathematical functions |
| Takes one parameter. |
Statistical functions |
| Takes at least two parameters. |
Example 1
This example generates a raster object with one band. The value of each pixel is calculated as 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
This example calculates the variance of three bands.
[ { "expr":"(std([0,0],[0,1],[0,2]))", "nodata": true, "nodataValue":999 } ]Example 3
This example generates a raster with three bands. A different expression is used to calculate each band.
[ { "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 } ]
The storageOption parameter is described as follows.
Parameter Name | Description | Type | Default | Notes |
chunking | Specifies whether to use chunk storage. | boolean | Same as the source raster | - |
chunkdim | The dimension information for the chunks. | string | Same as the source raster | This parameter is valid only when chunking is set to true. |
chunktable | The name of the chunk table. | string | '' | If you pass an empty string (''), a temporary chunk table with a random name is created to store the data. This temporary table is valid only for the current session. To keep an accessible raster object, specify a chunk table name. |
compression | The compression algorithm. | string | Same as the source raster | Supported algorithms: none, jpeg, zlib, png, lzo, and lz4. |
quality | The compression quality. | integer | Same as the source raster | This parameter applies only to the jpeg compression algorithm. |
interleaving | The interleaving method. | string | Same as the source raster | Must be one of the following:
|
endian | The byte order. | string | Same as the source raster | Must be one of the following:
|
Examples
-- A permanent table
CREATE TABLE rast_mapalgebra_result(id integer, rast raster);
-- Insert data into the 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"}'
);