2

I have created a multi-value choice list following this blogpost:

https://blogs.esri.com/esri/arcgis/2012/07/16/generating-a-multivalue-choice-list/

I changed the validation code to my needs but now I am stuck. Basically I want the script to use the selected values of the choice list and 1) change them all to a another field value (optional) 2) apply a select analysis on it.

My validation code so far looks like this:

import arcpy
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""

def __init__(self):
"""Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

def initializeParameters(self):
"""Refine the properties of a tool's parameters.  This method is
called when the tool is opened."""
    return

def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed.  This method is called whenever a parameter
has been changed."""
    if self.params[0].value and self.params[1].value:
        self.params[2].filter.list = sorted({row[0] for row in arcpy.da.SearchCursor(self.params[0].value, self.params[1].value.value) if row[0]})

def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter.  This method is called after internal validation."""
    return

My user interface looks like this: enter image description here

How can I do this?

Update: The part of the script, where I don't know what to do:

inputFC= arcpy.GetParameterAsText(0) #input Feature Class, type: feature layer
inputField =arcpy.GetParameterAsText(1) #input Field, type: Field
Values = arcpy.GetParameterAsText(2) # field values, type: String
OutputFC = arcpy.GetParameterAsText(3) #output, type: feature layer

where = "\"inputField"\" = Values
arcpy.Select_analysis(inputFC, OutputFC, where)

The problem lies in the parameters of the select_analysis part. I don't know how to access the values the user selected to use them in an SQL statement.

1

Try this

import arcpy

inputFC= arcpy.GetParameterAsText(0) #input Feature Class, type: feature layer
inputField =arcpy.GetParameterAsText(1) #input Field, type: Field
Values = arcpy.GetParameterAsText(2) # field values, type: String
OutputFC = arcpy.GetParameterAsText(3) #output, type: feature layer

valueList = [x.strip() for x in Values.split(";")]

dateVal = 0

fieldInfo = arcpy.ListFields(inputFC, inputField)[0]
fieldType = fieldInfo.type
if fieldType == 'String':
    where = "{} IN ('{}')".format(arcpy.AddFieldDelimiters(inputFC, inputField), "', '".join(valueList))
elif fieldType == 'Date':
    arcpy.AddError("Doesn't work nice with dates!")
    dateVal = 1
else:
    where = "{} IN ({})".format(arcpy.AddFieldDelimiters(inputFC, inputField), ", ".join(valueList))

if not dateVal:
    arcpy.Select_analysis(inputFC, OutputFC, where)

You have to convert your multi-value string of field values into a list of values and then concatenate them into your where clause. The where clause is constructed slightly differently for String fields from other types.

This doesn't work nicely with dates at this stage - I'm still looking into how to deal to the dates here and will post an edit if I find a solution.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.