> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wherobots.com/llms.txt
> Use this file to discover all available pages before exploring further.

# RS_MapAlgebra

Apply a map algebra script on a raster. The expression can be a simple arithmetic operation or a complex combination of multiple operations. The expression can be applied to a single raster band or multiple raster bands. The result of the expression is a new raster.

`RS_MapAlgebra` is backed by [Jiffle](https://github.com/geosolutions-it/jai-ext/wiki/Jiffle) and can be compiled to Java bytecode for execution. For guidance on scripting for map algebra, refer to the [Jiffle language summary](https://github.com/geosolutions-it/jai-ext/wiki/Jiffle---language-summary#operators).

<img src="https://mintcdn.com/wherobots/AayJA2u8CknIeTgt/images/sql-functions/RS_MapAlgebra/RS_MapAlgebra.svg?fit=max&auto=format&n=AayJA2u8CknIeTgt&q=85&s=80ea41d0e11e52915cdbbbb3e1ea0dd1" alt="RS_MapAlgebra" width="900" height="420" data-path="images/sql-functions/RS_MapAlgebra/RS_MapAlgebra.svg" />

<Note>
  The `RS_MapAlgebra` function can cast the output raster to a different data type specified by `pixelType`:

  * If `pixelType` is smaller than the input raster data type, narrowing casts will be performed, which may result in loss of data.

  * If `pixelType` is larger, widening casts will retain data accuracy.

  * If `pixelType` matches the input raster data type, no casting occurs.

  This allows controlling the output pixel data type. Users should consider potential precision impacts when coercing to a smaller type.
</Note>

## Signatures

### Single raster input

```sql theme={"system"}
RS_MapAlgebra(rast: Raster, pixelType: String, script: String)
```

```sql theme={"system"}
RS_MapAlgebra(rast: Raster, pixelType: String, script: String, noDataValue: Double)
```

```sql theme={"system"}
RS_MapAlgebra(rast: Raster, pixelType: String, script: String, noDataValue: Double, numBands: Int)
```

### Two raster inputs

```sql theme={"system"}
RS_MapAlgebra(rast0: Raster, rast1: Raster, pixelType: String, script: String, noDataValue: Double)
```

```sql theme={"system"}
RS_MapAlgebra(rast0: Raster, rast1: Raster, pixelType: String, script: String, noDataValue: Double, numBands: Int)
```

## Parameters

<ParamField body="rast" type="Raster" required>
  The input raster to apply the map algebra expression to. In the script, the `rast` variable is bound to this raster.
</ParamField>

<ParamField body="rast0" type="Raster" required>
  The first raster to apply the map algebra expression to (for two raster input signatures).
</ParamField>

<ParamField body="rast1" type="Raster" required>
  The second raster to apply the map algebra expression to (for two raster input signatures).
</ParamField>

<ParamField body="pixelType" type="String" required>
  The data type of the output raster. Accepts one of: `D` (double), `F` (float), `I` (integer), `S` (short), `US` (unsigned short) or `B` (byte). If specified `NULL`, the output raster will have the same data type as the input raster.
</ParamField>

<ParamField body="script" type="String" required>
  The map algebra script written in Jiffle. The `rast` variable (or `rast0`/`rast1` for two raster inputs) is bound to the input raster(s), and the `out` variable is bound to the output raster. Use bracket notation to access bands, e.g. `rast[0]` for the first band.
</ParamField>

<ParamField body="noDataValue" type="Double">
  The nodata value of the output raster. `null` is allowed.
</ParamField>

<ParamField body="numBands" type="Int">
  The number of bands in the output raster. When specified, the output bands can be referenced in the script using `out[0]`, `out[1]`, etc.
</ParamField>

## Return type

<ResponseField type="Raster">
  The resulting raster.
</ResponseField>

## Examples

### NDVI calculation

Calculate the NDVI of a raster with 4 bands (R, G, B, NIR):

```sql theme={"system"}
SELECT RS_MapAlgebra(rast, 'D', 'out = (rast[3] - rast[0]) / (rast[3] + rast[0]);') AS ndvi FROM raster_table
```

```
+--------------------+
|              raster|
+--------------------+
|GridCoverage2D["g...|
+--------------------+
```

### Two raster inputs

```sql theme={"system"}
SELECT RS_MapAlgebra(rast0, rast1, 'D', 'out = rast0[0] * 0.5 + rast1[0] * 0.5;', null) FROM raster_table
```

### Multi-band output

```sql theme={"system"}
SELECT RS_MapAlgebra(rast, 'D', 'out[0] = rast[0] - rast[1]; out[1] = rast[0] + rast[1];', null, 2) FROM raster_table
```
