Skip To Content

Foreach

Available with Image Analyst license.

Available with Spatial Analyst license.

Summary

Creates a raster object by applying a raster function or a customized function template to every slice in the input multidimensional raster.

Discussion

Use the Foreach function to apply a raster function to every slice in a multidimensional raster dataset. To apply a raster function to a single slice, use the Subset function before using Foreach.

This function creates a raster object that contains the variables and dimensions of the input multidimensional raster, processed using a raster function. The referenced raster dataset for the raster object is temporary. To make it permanent, you can call the raster object's save method.

Syntax

Foreach (in_raster, raster_function, {raster_function_arguments})
ParameterExplanationData Type
in_raster

The input multidimensional raster dataset.

Raster
raster_function

The name of a raster function or the path to a custom raster function (.rft.xml file) to apply to every slice in the input multidimensional raster.

String
raster_function_arguments

The parameters and values associated with the raster function or function chain. If not specified, default values will be used.

For example, the Tasseled Cap raster function does not require any arguments; the input is a single raster, so there is no need to specify arguments.

The Arithmetic raster function, however, requires 5 input parameters: Raster, Raster2, Operation, Cellsize Type and Extent Type. To use Arithmetic with the Apply function, Raster and Raster2 are defined in the in_raster parameter, and the remaining parameters have default values, so if nothing is specified for those parameters, the default values will be used.

For information about the function arguments for each raster function, see Raster function objects.

Dictionary
Return Value
Data TypeExplanation
Raster

The output multidimensional raster.

Code sample

Foreach example

Calculate NDVI for each slice in a time series of Landsat 7 scenes.

import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

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

# Create multidimensional raster object from 
# Landsat7 time series data in a mosaic dataset
in_raster = Raster("Landsat7", True)

# Calculate NDVI for each scene in the time series
out_NDVI_raster = Foreach(
	in_raster, "NDVI", {'VisibleBandID':3,'InfraredBandID':4})