Iteration, often referred to as looping or batch processing, 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 a single tool or process can be executed repeatedly.
- Only one iterator can be used per model. The button 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 another model and add only the iterator and processes that you want to run multiple times, save it, and add it as a model tool into the main model.
- To give a unique name to the output of any tool connected to an iterator, use inline variable substitution.
There are twelve iterators in ModelBuilder that you can use for iterating over different values, datasets, and workspaces.
Iterates a specific number of times.
Iterates until a particular variable or condition is True or False.
|Iterate Feature Selection|
Iterates through each feature in a feature class or for a group of features with common attributes.
|Iterate Row Selection|
Iterates through each record in a table or for a group of records with common attributes
|Iterate Field Values|
Iterates through each value in a field.
Iterates through each value in a list of input values.
Iterates through each dataset in a workspace.
|Iterate Feature Classes|
Iterates through each feature class in a workspace.
Iterates through each file in a folder.
Iterates through each raster in a workspace.
Iterates through each table in a workspace.
Iterates through each workspace, like a geodatabase, in a folder.
Add and use an iterator
To add an iterator to your model, click ModelBuilder > Insert > Iterator and choose from the list of iterators. Only one iterator can be used per model. The options to add another iterator will be disabled if one iterator exists in the model.
Double-click the iterator element to view and change its settings. Each of the twelve iterators are similar, though each iterator may have settings that differ from the other iterators. A commonly used iterator, Iterate Feature Classes, is explained below.
Input workspace (such as a geodatabase) or a feature dataset.
Combination of * and characters that help to limit the results. For example, A* means that only feature classes starting with A are included. This is optional.
A feature type filter that helps limit the results. The most common are Point, Line, and Polygon. This is optional.
Determines if the iterator will go into subfolders in the workspace. This is optional.
|Output Feature Class|
Output variable that contains the path of the feature class. This variable can be used as input to all geoprocessing tools that accept feature layers or feature classes.
Output variable that contains the name of the feature class. This is a string.
Iterate Feature Classes requires an Input Workspace (the variable named Input Feature Classes in the graphic above) where all the feature classes to iterate through are stored. Three additional settings, Wildcard, Feature Type, and Recursive, are used to restrict which feature classes in the workspace are iterated and control the iteration in subfolders in the workspace.
Iterate Feature Classes has two output variables: a feature class and the name of the feature class. The feature class variable can be connected to the next tool for processing, and the Name variable can be used for inline variable substitution.
The example below uses the Iterate Feature Classes iterator to project every feature class in a workspace to a new map projection. The name each new feature class created by the Project tool is named based on the input Name using inline variable substitution. In addition, a new field is added to each output feature class. After adding the Iterate Feature Classes iterator to ModelBuilder, follow these general steps:
- Specify an input workspace, such as a geodatabase, to iterate through every feature class in that workspace.
- Specify a wildcard and feature type (in the example, only polygon feature classes with a name starting with the letter I will be included).
- Iterate Feature Classes produces two outputs: the feature class (Output Feature Class) and the name of the feature class (Name). Connect the Output Feature Class variable to the Project tool to use it as input, and use the Name variable and inline variable substitution to name each output from the Project tool with the same name as the input feature class. The Project tool output is written into another geodatabase.
- Connect the output of the Project tool to the Add Field tool to add a new field.