Very often we are asked
- “How to generate a choice list for a parameter?” and
- “How to generate a choice list for a parameter from an input feature class/table automatically?” i.e. how to create a new choice list of unique field values each time the input feature class/table changes.
Generating a choice list for a parameter
For the first question, all you have to do is to update the value list property of a tool parameter.
A value list is a predefined set of input choices for a parameter. Only values contained in the value list or commonly called the drop down list are allowed as inputs. Values not in the list raise an error and the tool does not execute. In a model a value list filter can be used for a String, Long, Double and Boolean data types. For Long and Double types, you enter the allowable numeric values. For Boolean data types, the value list contains two values: the true value and the false value. The true value is always the first value in the list.
Generating a choice list for a parameter from an input feature class/table automatically
For the second question, life is simple again. All you need to do is:
- Download an example script tool from here.
- Add the script tool to the model.
- Expose the input parameters (input feature class, field, value) of the script tool in the model.
- Make the input parameters model parameter.
- Use as described in the example below:
In the following example the user can define an input feature class (Bird Locations in this example) and a field (Type in this example) to generate a value list, from the model tool dialog. The value list parameter is then populated with a choice list of all the unique
values in the field. The output of the script tool is the selected value (Canada Goose in this example). The Select tool uses the output of the script tool (the derived output parameter as shown in illustration 1) as inline variable in the expression to select features (“Type” = ‘%Output Value%’)
Illustration 1 – Shows a model example of using the dynamically generated value list
Understanding the script and script tool
- A Python script is created and set as a source file for the script tool.
- Three required parameters are created for the script tool: input dataset, input field and the input value. After the input data and input field values are received, the inputs pass through a custom validation code in the script tool and automatically populates the third input value parameter with a choice list of unique values from the input field. User of the tool can then choose any value from the populated list (see illustration 2 below).
- A derived output parameter is created, and set to be equal to the value selected by the user from the automatically generated choice list (see illustration 2 below). This is important to chain the script tool to other tools inside a model.Illustration 2 – Shows how the script tool parameter properties and the code in the script are connected
- Illustration 3 shows the script tool validation tab. The code can be edited by clicking the Edit button at the bottom of the validation tab.Illustration 3 – Shows the validation code block part under the script tool validation tab
The workflow used in the validation code is as shown below:
Illustration 4 – Validation code workflow
To understand the validation code in details see illustration below:
Illustration 5 – Line by line explanation of the validation code
The script tool was contributed by Jason Scheirer, a developer on the Analysis and Geoprocessing team.