Generating a choice list from a field
This blog is an extension of the blog Generating a choice list from a field, explaining:
How to create a MULTIVALUE parameter choice list from an input feature class/table automatically. In this example, multiple input parameters from a choice list are then iterated through. The model can be easily extended to carry out numerous analysis that require user selected input parameters.
Download the data from here.
Understanding the model:
- 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 (e.g.Bird Locations) and a field (e.g. Type) 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. This list is a multivalue parameter i.e. you can select multiple values from the drop down list.
The output of the script tool is the multivalue with all the selected value (e.g. American Coot, Black Scoter, Canada Goose and Gadwall). In the model provided, this multivalue output is then connected to the Multivalue Iterator as input in the sub-model. Learn more about model within a model/sub-model.
The tool then iterates over each value (e.g. American Coot, Black Scoter, Canada Goose and Gadwall). The output of this iterator (Values variable) is then input to the Select tool as an inline variable, in the expression, to select features (“Type” = ‘%Value%’). Learn more about inline variable substitution.
The Calculate Value tool is used to remove spaces in the selected values (e.g. American Coot changes to AmericanCoot (no space)) using the .replace Python function.
The output of the Calculate Value tool is then used as inline variable in the output of the Select tool (e.g. C:ScratchScratch.gdb%Name%).
The Collect Value tool is used to collect the output from all the iterations. The output of the Collect Value tool is made a model parameter so that when the sub-model is added to the main model, the output of model parameter becomes a variable in the model.
To run the iterator and the Calculate Value tool before the Select tool two preconditions are created:
- From the output of the iterator to the Select tool.
- From the output of the Calculate tool to the Select tool.
- To iterate the values with other tools for your project, add your tools in the sub model either in place of the Select tool or connected to the output of Select.
- If you do not want to iterate put all your tools in the main model.
- If Choose Multiple Field Value is not the first tool in your model and is connected to other tools such as Add Field tool upstream, make sure to run the tools up to the Choose Field Values tool once and validate such that the tool sees the newly added field in the drop down list of its parameter. The tool is useful when the input field list is known and you can choose the values of the field before you execute the tool.
- If you change the field value after selecting values from the filter list the codes generates a new unique list of values for the new field, and checks if the old selected values exist in the new list. If true, it retains the selection else removes the selection. This behavior differs a little if the tool is used in ModelBuilder and you make the tool parameter – Value a variables in the model. In such a case, if the field value is changed after the selection and the value does not exist in the new list an empty check is left at the end of the new list. You must uncheck the last value or unselect all the values and then reselect the desired values. The designed behavior in ModelBuilder is not to erase the user modified values unless the user makes a conscious choice.
Understanding the tool
Prerequisite reading – Read more about the tool and the validation code for the tool from here.
To make this script tool parameters a multivalue parameter, the script tool property for the Value and Output value parameters have been changed as shown below:
The script tool validation code can be accessed by right clicking the script tool > selecting Properties > Validation tab > and clicking the Edit button. This opens the validation code in the Python editor for better viewing. The code has added comments explaining the code on each line.