HOW TO

Use Iterate operations in Model Builder to generate expression strings in ArcGIS Pro

Last Published: September 4, 2024

Summary

Iterate operations in Model Builder in ArcGIS Pro allow users to perform repetitive tasks on multiple datasets or features. However, there is a limitation when it comes to using the values obtained from iterate operations in expressions, such as in the "Select Layer by Attributes" or "Make Feature Layer" tools. This article presents a solution to overcome this limitation by creating a script tool that accepts two strings: the field name and the field values. The script tool then returns an expression string that can be used in various tools within Model Builder.

Procedure

  1. Create a new script tool in ArcGIS Pro by right-clicking on the toolbox where you want to add the tool and selecting New > Script.
  2. In the Script Properties dialog, provide a name for the script tool and click the Parameters tab.
  3. In the Parameter dialog, specify the input parameters for the script tool. In this case, we need two input parameters: the field name and the field values. Set the data type of both parameters to String. and one output parameter as String and its Type as Derived.
  4. Click the Execution tab to enter the script, for now we are generating an expression which validates "Equals to" condition. Below is the script for the same
  import arcpy
    
    # Get the input parameters
    field_name = arcpy.GetParameterAsText(0)
    field_value = arcpy.GetParameterAsText(1)
    
    # Create the expression
    expression = f"{field_name} = '{field_value}'"
    
    # Set the output parameter
    arcpy.SetParameterAsText(2, expression)

Below is the UI of the script tool:
UI Script Tool.png 

  1. Use the script tool in Model Builder. Add the script tool to the model and connect it to the iterate operation.
  2. Assign a precondition to the script tool to ensure it runs only after the iterate operation has completed successfully.
  3. In the subsequent tools, where you need to use the expression, use the output of the script tool as the input for the expression parameter.

By following these steps, you can effectively use the values obtained from iterate operations in expressions within Model Builder in ArcGIS Pro. This solution provides a flexible and efficient way to automate repetitive tasks and perform complex spatial and non-spatial analyses. 

As a use case, let us assume we need to export KML from a layer based on the unique values of a field. Hence we Iterated Field Values and connected the output to the script tool as a field value and defined the field name in the script tool manually as a one time declaration. Assign a precondition to the script tool to ensure it runs only after the iterate operation has completed successfully. We are now able to use the output of the script tool as an expression on the Select Layer By Attributes tool and further use Layer to KML with the Iterator inline variable in the output name to generate unique KML files by the field value.

Model_Iterate_Values.png

Article ID: 000033380

Software:
  • ArcGIS Pro

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options