Python Toolbox - user select from domain values

09-20-2018 06:45 PM
New Contributor

I have a feature class. One of the fields is a Short Integer e.g. STATE. There is a domain (domain codes) that maps Short Integer to String values e.g. STATE_VALUES: 1 = New, 2 = Validated, 3 = Published. The STATE field in the feature class is linked to the domain. It operates as expected e.g. set state to integer values and via the Attribute Table view in ArcMap, see the associated state value.

I have a Python Toolbox. When the user opens one of the functions, the user specifies various parameters and then proceeds to execute the function. One of the parameters is to provide a state. At the moment, the user needs to type in the integer value (e.g. 1,2,3), which requires them to know the integer values but it would be better if they are presented with a list of values to choose from (e.g. New, Validated, Published).

Reviewing various documentation and other online resources, it isn't obvious if and how it could be done. GPValueTable seemed like a good option for the parameter type but it didn't give what was required.

Any suggestions or resources?

Greatly appreciate the help.

0 Kudos
1 Reply
MVP Alum

I would put the domain in a dictionary and use something like (note lines 8, 13-24 and 31):

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

        self.state = { 'New': 1, 'Validated': 2, 'Published': 3}  # domain description: domain code

    def getParameterInfo(self):
        """Define parameter definitions"""
        stateValue = arcpy.Parameter(
            displayName = "Select State",
            name = "stateValue",
            datatype = "GPString",
            parameterType = "Required",
            direction = "Input")

        stateValue.filter.type = "ValueList"
        stateValue.filter.list = list(self.state.keys()) # dictionary keys
        stateValue.value = stateValue.filter.list[0] # default first item in list

        return [stateValue]  

    # other defs

    def execute(self, parameters, messages):
        """The source code of the tool."""
        messages.addMessage('Selected: {}, Code: {}'.format(parameters[0].value, self.state[parameters[0].value])) 


If should also be possible to use arcpy.da.ListDomains in some fashion to populate the dictionary if needed.

0 Kudos