Generating a multivalue choice list

Prerequisite Reading
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.

Data
Download the data from here.

Understanding the model:

  1. Download an example script tool from here.
  2. Add the script tool to the model.
  3. Expose the input parameters (input feature class, field, value) of the script tool in the model.
  4. Make the input parameters model parameter.
  5. 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:

  1. From the output of the iterator to the Select tool.
  2. From the output of the Calculate tool to the Select tool.

Tips:

  • 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.

**************IMPORTANT NOTE**************

  • 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.

This entry was posted in Analysis & Geoprocessing and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

5 Comments

  1. zhouyaxin8 says:

    Hi shitijmehta,

    I was trying to run this model but run into an error as below. Would you please help me solve the problem?

    ” ERROR
    updateParameters Execution Error: Runtime error Traceback (most recent call last): File “C:\Documents and Settings\zhou_y\Desktop\Multipl\ChooseMultipleFieldValues.tbx#ChooseMultipleFieldValues.UpdateParameters.py”, line 4, in File “C:\Documents and Settings\zhou_y\Desktop\Multipl\ChooseMultipleFieldValues.tbx#ChooseMultipleFieldValues.UpdateParameters.py”, line 31, in updateParameters File “c:\program files\arcgis\desktop10.2\arcpy\arcpy\analysis.py”, line 1031, in Frequency raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000824: The tool is not licensed. Failed to execute (Frequency).”

    I was using ArcGIS 10.2 for desktop version 10.2.0.3348. And I have checked the expensions as listed: Spatial Analyst, Schematics, Publisher, Network Analyst, Geostatistical Analyst, and Arc Scan.

  2. hazelton_glenn says:

    this error has nothing to do with extensions. it is the level of arcmap that is the issue. it must be ‘Advanced’ and not ‘Basic’.

  3. landvest says:

    Is there any reason the model displayed in this blog post does not match the models contained in the script you are instructed to download?

    • landvest says:

      Ok, I see now, it requires modification. However, based on what i’m reading here, you need Advanced to get the validation to work properly, correct? I tried to remove the validation in a new script, but then the script isn’t able to derive the value list it seems.