The Iterate Feature Selection tool iterates over features in a feature class.
In this workflow, you'll create a model to iterate over features grouped by regions in a feature class (USA states) and create eight feature classes, one for each group or region. The field value (region) used to split the feature class is also used as the name of the output feature class. The region names includes spaces. For example, Pacific West Region, Mid Atlantic Region, and so on. Space is not accepted as a valid file name character in a file geodatabase. To remove unacceptable spaces, the iterator’s output parameter Value (region name) is altered by using it as an inline variable in the Calculate Value tool (%Value%). The output of the Calculate Value tool is finally used as an inline variable in the output name of the Copy Features tool (%Name%). The output of the Copy Features tool is connected to the Collect Values tool, which collects the output of each iteration and outputs a single multivalue output.
If you are new to ModelBuilder, review the following topics:
Open the project
Download the project to use in this tutorial. The project includes an input geodatabase, an output geodatabase and a completed model.
- Open the Work with Iterate Feature Selection overview page.
- Click Download on the right side of the page.
- Right-click the downloaded IterateFeatureSelection.zip file and extract it to a convenient location, such as C:\Temp
- In the extracted folder, IterateFeatureSelection, double-click the ArcGIS Pro project file, IterateFeatureSelection.aprx.
- Explore the data in the Catalog pane, and click the Project tab if necessary. Browse to Folders > IterateFeatureSelection.
The ToolData.gdb geodatabase contains the States feature class, which will be used as the input for the model.
The outputs of the model will be written to the ScratchStates.gdb geodatabase.
You'll create a model in the MyProjectTools toolbox. To add tools to the model, complete the following steps:
- In the Catalog pane, click the Project tab if necessary. Browse to Folders > IterateFeatureSelection > MyProjectTools.
- Right-click MyProjectTools and click New. Select Model to create and open a model.
- Add the Iterate Feature Selection tool.
- Click the ModelBuilder tab.
- In the Insert group, click Iterators .
- In the Iterators list, click Iterate Feature Selection.
The Iterate Feature Selection iterator is added to the model. The iterator is unavailable since no parameter values have been provided.
- Add the Calculate Value tool.
- Click the ModelBuilder tab.
- In the Insert group, click Utilities .
- In the Utilities list, click Calculate Value.
The Calculate Value tool is added to the model.
- Add the Collect Values tool in the model, as described in the previous step.
- Add the Copy Features tool.
- Click in the model, and type Copy Features to open the Add Tools To Model dialog box.
- Search for the Copy Features tool.
- Double-click the Copy Features tool and add it to the model.
Specify the tool parameters and connect the tools
After adding tools to the model, you need to specify the parameters for each tool.
- Double-click the Iterate Feature Selection iterator to open the iterator's dialog box.
- For the In Features parameter, browse to <Your Project Location>\IterateFeatureSelection\ToolData.gdb and select the States feature class.
- Leave the rest of the parameters empty and click OK.
The Input Feature Class is added as a model variable in the model (dark blue oval) and changes the color of the iterator, indicating that the required parameters have been supplied and it is ready to run.
- Right-click the Feature Selection iterator and click Create Variable > From Parameter > Group By Fields to expose the Group By Field parameter as a variable in the model.
- Double-click the Group By Fields model variable and from the drop-down list, select Region.
- Click OK to close the dialog box.
The Region field value will be used to select features for each iteration.
- Double-click the Calculate Value tool to open the tool's dialog box.
For the Expression parameter, type "%Value%".replace(" ","") and change Data type to String.
The expression has the following parts:
- The inline variable substitution %Value%
- The Python replace method to replace the space from the name
At run time, %Value%, which is the region name of the iterator output parameter (light blue oval), is replaced by the value of the variable, for example, Pacific West Region, Mid Atlantic Region, and so on.
- Click OK to close the dialog box.
- In ModelBuilder, tools honor the hierarchy and run only if the connected upstream tool runs first. The Calculate Value tool uses the output Value parameter of the iterator in its expression, but is not connected to it and will fail if it does not find the region name. You want the iterator to run before the Calculate Value tool so that the region name is available. For this, you'll set the Value variable as a precondition on the Calculate Value tool.
Hover over the Iterate Feature Selection tool output variable Value (light blue oval). Click and drag to draw a connector line to the Calculate Value tool. Release the mouse and click Precondition on the pop-up menu.
- Right-click the States variable (dark blue oval) and click Rename. Type Input Feature Class for a new name. In a similar way, rename the output of the iterator I_States (green oval) to Selected Features and the output of the Calculate Value tool to Name.
- Hover over the Calculate Value tool output variable Name (green oval). Click and drag to draw a connector line to the Copy Features tool. Release the mouse and click Precondition on the pop-up menu.
- Double-click the Copy Features tool to open the tool's dialog box.
- For the Input Features parameter, choose Selected Features from the drop-down list.
- For the Output Feature Class parameter, browse to <Your Project Location>\ScratchStates.gdb and type %Name%.
Leave the rest of the parameter default settings and click OK to close the Copy Features tool dialog box.
- Hover over the Copy Features tool output variable (green oval). Click and drag to draw a connector line to the Collect Values tool. Release the mouse and click Input Value on the pop-up menu.
- Rename all the model variables as shown in the finished model.
- To arrange the model elements, click the Auto Layout button on the ModelBuilder tab, and click the Fit To Window button to maximize the layout to the size of your model window.
Run the model
- Right-click the output of the Collect Values tool and, click Add To Display.
The output is added to the map.
- Click the Save button on the ModelBuilder tab to save the model.
- Click the Run button on the ModelBuilder tab to run the model.
The model iterates over eight groups of features and creates eight feature classes in the ScratchStates.gdb geodatabase.
- Close the model and ArcGIS Pro.