A quick tour of using iterators
Iteration, often referred to as looping, means to repeat a process over and over with some degree of automation. Iteration is very important because automating repetitive tasks reduces the time and effort required to perform the tasks. With iteration in ModelBuilder, a process can be executed over and over using different settings or data in each iteration. ModelBuilder also provides flexibility in iteration, as an entire model or simply a single tool or process can be executed repeatedly.
Iterator |
Description |
---|---|
Iterates over a starting and ending value by a given value. It works exactly like For in any scripting/programming language, executing through a set number of items. |
|
Works exactly like 'while' in any scripting/programming language, executing "while" a condition is true or false for the input or set of inputs. |
|
Iterates over features in a feature class. |
|
Iterates over rows in a table. |
|
Iterates over each value in a field. |
|
Iterates over a list of values. |
|
Iterates over datasets in a Workspace or Feature Dataset. |
|
Iterates over feature classes in a Workspace or Feature Dataset. |
|
Iterates over files in a folder. |
|
Iterates over rasters in a Workspace or a Raster Catalog. |
|
Iterates over tables in a workspace. |
|
Iterates over workspaces in a folder. |
Understanding an iterator
Each iterator has a set of parameters that may differ from the other iterators, but the overall structure of all iterator tools is very similar. A commonly used iterator, Iterate Feature Classes, is explained below.
Iterate Feature Classes requires an Input Workspace where all the feature classes to iterate through are stored. Two additional parameters, Wildcard and Feature Type, are used to restrict what feature classes in the workspace are iterated:
- Wildcard limits the feature classes by their names.
- Feature Type limits the feature classes by their feature types: annotation, arc, dimension, edge, junction, label, line, node, point, polygon, region, route, or TIC.
The Recursive parameter is used to control the iteration over feature classes within subfolders in the workspace.
Iterate Feature Classes has two output variables: the output feature class and the name of the feature class. The output feature class can be connected to the next tool for processing, and the Name variable can be used for inline variable substitution. For example, if the Buffer tool was added to the model, and the Feature Class variable was connected to the tool, every feature class in the workspace would be buffered.
Iterator input and output
Below is a list of iterators and what their input and outputs are. A number of iterators have a Value or Name as second output, which can be used for inline variable substitution.
Iterator |
Input |
Output 1 |
Output 2 |
---|---|---|---|
Values |
Value |
- |
|
Values |
Boolean—True or False Value |
- |
|
Features |
Feature |
Value |
|
Table |
Record |
Value |
|
Table |
Field Value |
- |
|
Values |
Value |
- |
|
Workspace or Feature Dataset |
Dataset |
Name |
|
Workspace or Feature Dataset |
Feature Class |
Name |
|
Folder |
File |
Name |
|
Workspace or Raster Catalog |
Raster Dataset |
Name |
|
Workspace |
Table |
Name |
|
Folder |
Workspace |
Name |
- Only one iterator can be used per model. The options to add another iterator will be disabled if one iterator exists in the model.
- If an iterator is added to a model, all tools in the model iterate for each value in the iterator. If you do not want to run each tool in the model for each iterated value, create a submodel/model within a model/nested model that contains only the iterator and add it as a model tool into the main model.
- If a model containing an iterator is exported to a Python script, the script will not include the iteration logic. Python listing logic can be added to the script to achieve a similar effect.
- Using an iterator will set a default value of -1 in Iteration options in Model Properties, which simply means that the model will run for an unlimited number of times, or based on the number of inputs in an iterator, and not on a set number.
- The output of any tool connected to the iterator can have (if required) a unique name for each iteration to avoid being overwritten by
- Using the system variable %n%, for example, C:\Scratch\scratch.gdb\output_%n%.
- Using the Name or Value output of the iterator used in the model as an inline variable, for example, C:\Scratch\scratch.gdb\output_%Name%, C:\Scratch\scratch.gdb\output_%Value%.
- Using any other variable in the model as an inline variable; for example, if there is a variable XYZ, the name of the output can be C:\Scratch\scratch.gdb\output_%XYZ%. This variable should not contain a constant value, but a value that changes with each iteration; otherwise, the file will be overwritten with each iteration.
- Tool outputs with the Add To Display option checked are added to the display in ArcMap using the variable name. If you want to add the output of each iteration to display in ArcMap with the actual unique output name instead of the name of the variable:
- Connect the output to be displayed to the Collect Values tool.
- Right-click the output of Collect Values and check the Add To Display.
- If the model will be run from the model tool dialog box, then make the output of Collect Values a model parameter, as only the output model parameters are added to display.
Iterators replace the series option in Model Properties available prior to ArcGIS 10.