Zonal Statistics (Spatial Analyst)

License Level:BasicStandardAdvanced

Summary

Calculates statistics on values of a raster within the zones of another dataset.

Learn more about how Zonal Statistics works

Illustration

Zonal Statistics illustration
OutRas = ZonalStatistics(ZoneRas, "VALUE", ValRas, "MINIMUM")

Usage

Syntax

ZonalStatistics (in_zone_data, zone_field, in_value_raster, {statistics_type}, {ignore_nodata})
ParameterExplanationData Type
in_zone_data

Dataset that defines the zones.

The zones can be defined by an integer raster or a feature layer.

Raster Layer | Feature Layer
zone_field

Field that holds the values that define each zone.

It can be an integer or a string field of the zone dataset.

Field
in_value_raster

Raster that contains the values on which to calculate a statistic.

Raster Layer
statistics_type
(Optional)

Statistic type to be calculated.

  • MEAN Calculates the average of all cells in the value raster that belong to the same zone as the output cell.
  • MAJORITY Determines the value that occurs most often of all cells in the value raster that belong to the same zone as the output cell.
  • MAXIMUM Determines the largest value of all cells in the value raster that belong to the same zone as the output cell.
  • MEDIAN Determines the median value of all cells in the value raster that belong to the same zone as the output cell.
  • MINIMUM Determines the smallest value of all cells in the value raster that belong to the same zone as the output cell.
  • MINORITY Determines the value that occurs least often of all cells in the value raster that belong to the same zone as the output cell.
  • RANGE Calculates the difference between the largest and smallest value of all cells in the value raster that belong to the same zone as the output cell.
  • STD Calculates the standard deviation of all cells in the value raster that belong to the same zone as the output cell.
  • SUM Calculates the total value of all cells in the value raster that belong to the same zone as the output cell.
  • VARIETY Calculates the number of unique values for all cells in the value raster that belong to the same zone as the output cell.
String
ignore_nodata
(Optional)

Denotes whether NoData values in the Value input will influence the results of the zone that they fall within.

  • DATA Within any particular zone, only cells that have a value in the input Value raster will be used in determining the output value for that zone. NoData cells in the Value raster will be ignored in the statistic calculation.
  • NODATA Within any particular zone, if any NoData cells exist in the Value raster, it is deemed that there is insufficient information to perform statistical calculations for all the cells in that zone; therefore, the entire zone will receive the NoData value on the output raster.
Boolean

Return Value

NameExplanationData Type
out_raster

The output zonal statistics raster.

Raster

Code Sample

ZonalStatistics example 1 (Python window)

This example determines for each zone the range of cell values in the Value input raster.

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/sapyexamples/data"
outZonalStats = ZonalStatistics("zone", "value", "valueraster", "RANGE",
                                "NODATA")
outZonalStats.save("C:/sapyexamples/output/zonestatout")
ZonalStatistics example 2 (stand-alone script)

This example determines for each zone the range of cell values in the Value input raster.

# Name: ZonalStatistics_Ex_02.py
# Description: Calculates statistics on values of a raster 
#    within the zones of another dataset.
# Requirements: Spatial Analyst Extension

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *

# Set environment settings
env.workspace = "C:/sapyexamples/data"

# Set local variables
inZoneData = "zone"
zoneField = "value"
inValueRaster = "valueraster" 

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute ZonalStatistics
outZonalStatistics = ZonalStatistics(inZoneData, zoneField, inValueRaster,
                                     "RANGE", "NODATA")

# Save the output 
outZonalStatistics.save("C:/sapyexamples/output/zonestatout2")

Environments

Related Topics

Licensing Information

ArcGIS for Desktop Basic: Requires Spatial Analyst
ArcGIS for Desktop Standard: Requires Spatial Analyst
ArcGIS for Desktop Advanced: Requires Spatial Analyst
4/10/2014