> ## 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_ZonalStats

This returns a statistic value specified by `statType` over the region of interest defined by `zone`. It computes the statistic from the pixel values within the ROI geometry and returns the result. If the `excludeNoData` parameter is not specified, it will default to `true`. This excludes NoData values from the statistic calculation. Additionally, if the `band` parameter is not provided, band 1 will be used by default for the statistic computation. The valid options for `statType` are:

The `allTouched` parameter (Since `v1.7.1`) determines how pixels are selected:

* When true, any pixel touched by the geometry will be included.
* When false (default), only pixels whose centroid intersects with the geometry will be included.

<img src="https://mintcdn.com/wherobots/AayJA2u8CknIeTgt/images/sql-functions/RS_ZonalStats_allTouched/RS_ZonalStats_allTouched.svg?fit=max&auto=format&n=AayJA2u8CknIeTgt&q=85&s=f71dcc246d3e9e7d30b6de84861bd4dc" alt="RS_ZonalStats_allTouched" width="820" height="410" data-path="images/sql-functions/RS_ZonalStats_allTouched/RS_ZonalStats_allTouched.svg" />

* `count`: Number of pixels in the region.
* `sum`: Sum of pixel values.
* `mean|average|avg`: Arithmetic mean.
* `median`: Middle value in the region.
* `mode`: Most occurring value, if there are multiple values with same occurrence then will return the largest number.
* `stddev|sd`: Standard deviation.
* `variance`: Variance.
* `min`: Minimum value in the region.
* `max`: Maximum value in the region.

<Note>
  If the coordinate reference system (CRS) of the input `zone` geometry differs from that of the `raster`, then `zone` will be transformed to match the CRS of the `raster` before computation.

  The following conditions will throw an `IllegalArgumentException` if they are not met:

  * The provided `raster` and `zone` geometry should intersect when `lenient` parameter is set to `false`.
  * The option provided to `statType` should be valid.

  `lenient` parameter is set to `true` by default. The function will return `null` if the `raster` and `zone` geometry do not intersect.
</Note>

<img src="https://mintcdn.com/wherobots/AayJA2u8CknIeTgt/images/sql-functions/RS_ZonalStats/RS_ZonalStats.svg?fit=max&auto=format&n=AayJA2u8CknIeTgt&q=85&s=3ca60274e72a6c3d7a2d04d396c21ae6" alt="RS_ZonalStats" width="700" height="390" data-path="images/sql-functions/RS_ZonalStats/RS_ZonalStats.svg" />

## Signatures

```sql theme={"system"}
RS_ZonalStats(raster: Raster, zone: Geometry, band: Integer, statType: String, allTouched: Boolean, excludeNoData: Boolean, lenient: Boolean)
```

```sql theme={"system"}
RS_ZonalStats(raster: Raster, zone: Geometry, band: Integer, statType: String, allTouched: Boolean, excludeNoData: Boolean)
```

```sql theme={"system"}
RS_ZonalStats(raster: Raster, zone: Geometry, band: Integer, statType: String, allTouched: Boolean)
```

```sql theme={"system"}
RS_ZonalStats(raster: Raster, zone: Geometry, band: Integer, statType: String)
```

```sql theme={"system"}
RS_ZonalStats(raster: Raster, zone: Geometry, statType: String)
```

## Parameters

<ParamField body="raster" type="Raster" required>
  The input raster.
</ParamField>

<ParamField body="zone" type="Geometry" required>
  The zone value.
</ParamField>

<ParamField body="band" type="Integer">
  The band index.
</ParamField>

<ParamField body="statType" type="String" required>
  The stat type value.
</ParamField>

<ParamField body="allTouched" type="Boolean">
  The all touched value.
</ParamField>

<ParamField body="excludeNoData" type="Boolean">
  The exclude no data value.
</ParamField>

<ParamField body="lenient" type="Boolean">
  The lenient value.
</ParamField>

## Return type

<ResponseField type="Double">
  A numeric value.
</ResponseField>

## Examples

```sql theme={"system"}
RS_ZonalStats(rast1, geom1, 1, 'sum', true, false)
```

```
10690406
```

```sql theme={"system"}
RS_ZonalStats(rast2, geom2, 1, 'mean', false, true)
```

```
226.55992667794473
```
