Select to view content in your preferred language

Using script tool parameters to update a multiple choice list of values to select from based on a fields attributes?

3258
4
Jump to solution
09-07-2021 01:03 PM
RPGIS
by
Frequent Contributor

Hi,

I was wondering if there was a way to set up the script tool parameters to update a multiple choice selection list based on a fields attribute values. I think I set it up correctly ( by all means correct me if it is wrong ) but it doesn't seem to be working accordingly. I might be missing something, but since I generally never mess with this, it is a bit foreign to me. Any help on this would be greatly appreciated.

 

 

class ToolValidator:
    # Class to add custom behavior and properties to the tool and tool parameters.

    def __init__(self):
        # set self.params for use in other function
        self.params = arcpy.GetParameterInfo()

    def initializeParameters(self):
        # Customize parameter properties. 
        # This gets called when the tool is opened.
        return

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before 
        # standard validation.

        if params[3].value:
            fcfields = [field.name for field in arcpy.ListFields(params[3].value) if field.name == params[4].valueAsText]
            attributevalues = [row for row in arcpy.da.SearchCursor(featureclass, fcfields)]
            self.params[5].filter.list = set(attributevalues)
            
        return

    def updateMessages(self):
        # Customize messages for the parameters.
        # This gets called after standard validation.
        return

    # def isLicensed(self):
    #     # set tool isLicensed.
    #     return True

 

 

Thanks

0 Kudos
3 Solutions

Accepted Solutions
by Anonymous User
Not applicable

Try indexing your row in your list comp:

attributevalues = [row[0] for row in arcpy.da.SearchCursor(featureclass, fcfields)]

 

View solution in original post

nzjs
by
Occasional Contributor

Looks like you might need to change params[] to self.params[] in a couple of places.

View solution in original post

RPGIS
by
Frequent Contributor

I figured out the issue. I wasn't sure why I didn't notice it before but the tool is working with the exception of the indexing error that I need to troubleshoot.

class ToolValidator:
    # Class to add custom behavior and properties to the tool and tool parameters.

    def __init__(self):
        # set self.params for use in other function
        self.params = arcpy.GetParameterInfo()

    def initializeParameters(self):
        # Customize parameter properties. 
        # This gets called when the tool is opened.
        return

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before 
        # standard validation.
        featureclass = self.params[2].value
        fieldname = self.params[3].value
        SubdivisionLayers = self.params[4].filter
        if featureclass:
            fcfields = [field.name for field in arcpy.ListFields(featureclass) if field.name == fieldname]
            attributevalues = [row[0] for row in arcpy.da.SearchCursor(featureclass, fcfields)]
            UniqueAttributes = set(attributevalues)
            SubdivisionLayers.list = [str(value) for value in UniqueAttributes]
            
        return

    def updateMessages(self):
        # Customize messages for the parameters.
        # This gets called after standard validation.
        return

    # def isLicensed(self):
    #     # set tool isLicensed.
    #     return True

The other issue is that it is passing the values with a ';' in between. I think I know a workaround but I will keep trying or create another post for any assistance.

View solution in original post

0 Kudos
4 Replies
by Anonymous User
Not applicable

Try indexing your row in your list comp:

attributevalues = [row[0] for row in arcpy.da.SearchCursor(featureclass, fcfields)]

 

nzjs
by
Occasional Contributor

Looks like you might need to change params[] to self.params[] in a couple of places.

RPGIS
by
Frequent Contributor

So I tried both of your suggestions, but I am still not getting the multiple selection that I am looking for. Here is the updated script but I can't seem to figure out what I am missing.

Do I need to initialize a parameter first before using the update parameter? If this is all correct then any ideas on how to accomplish this. I have never tinkered with the tool validations before so this is all new territory for me.

class ToolValidator:
    # Class to add custom behavior and properties to the tool and tool parameters.

    def __init__(self):
        # set self.params for use in other function
        self.params = arcpy.GetParameterInfo()

    def initializeParameters(self):
        # Customize parameter properties. 
        # This gets called when the tool is opened.
        return

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before 
        # standard validation.

        if self.params[3].value:
            featureclass = self.params[3].value
            fcfields = [field.name for field in arcpy.ListFields(featureclass) if field.name == self.params[4].valueAsText]
            attributevalues = [row[0] for row in arcpy.da.SearchCursor(featureclass, fcfields)]
            self.params[5].filter.list = set(attributevalues)
            
        return

    def updateMessages(self):
        # Customize messages for the parameters.
        # This gets called after standard validation.
        return

    # def isLicensed(self):
    #     # set tool isLicensed.
    #     return True
0 Kudos
RPGIS
by
Frequent Contributor

I figured out the issue. I wasn't sure why I didn't notice it before but the tool is working with the exception of the indexing error that I need to troubleshoot.

class ToolValidator:
    # Class to add custom behavior and properties to the tool and tool parameters.

    def __init__(self):
        # set self.params for use in other function
        self.params = arcpy.GetParameterInfo()

    def initializeParameters(self):
        # Customize parameter properties. 
        # This gets called when the tool is opened.
        return

    def updateParameters(self):
        # Modify parameter values and properties.
        # This gets called each time a parameter is modified, before 
        # standard validation.
        featureclass = self.params[2].value
        fieldname = self.params[3].value
        SubdivisionLayers = self.params[4].filter
        if featureclass:
            fcfields = [field.name for field in arcpy.ListFields(featureclass) if field.name == fieldname]
            attributevalues = [row[0] for row in arcpy.da.SearchCursor(featureclass, fcfields)]
            UniqueAttributes = set(attributevalues)
            SubdivisionLayers.list = [str(value) for value in UniqueAttributes]
            
        return

    def updateMessages(self):
        # Customize messages for the parameters.
        # This gets called after standard validation.
        return

    # def isLicensed(self):
    #     # set tool isLicensed.
    #     return True

The other issue is that it is passing the values with a ';' in between. I think I know a workaround but I will keep trying or create another post for any assistance.

0 Kudos