Example of using an iterator in a model
To explain the use of an iterator, the model example below uses the Iterate Feature Classes iterator to project every feature class in a workspace, name each Project tool output dynamically based on the input name using inline variable substitution, and add a new field to each feature class.
- An input workspace is specified for Iterate Feature Classes to iterate through every feature class in that workspace.
- Feature classes to iterate through are restricted by specifying a wildcard and feature type (only polygon feature classes with a name starting with I will be iterated through).
- Iterate Feature Classes produces two outputs: the feature class iterated through and the name of the feature class. The feature class variable is connected as the input to the Project tool, and the feature class name variable is used for inline variable substitution to dynamically name each output from the Project tool with the same name of the input feature class.
- A new field is added to every feature class by connecting the Add Field tool.
Note:
- 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.
Related Topics
3/3/2014