Select to view content in your preferred language

Filter feature layer paramter based on attributes

167
3
Jump to solution
3 weeks ago
MortenBackNielsen
Emerging Contributor

Is it possible to filter the available feature layers for a parameter based on the attribute table the same way we can filter based on the shape type:

 

# Define the sample paramter
Samples = arcpy.Parameter(
    displayName="SamplePoints",
    name="SamplePoints",
    datatype="GPFeatureLayer",
    parameterType="Required",
    direction="Input")
# Require the sample to be Points
Samples.filter.list = ["Point"]

# Reqiore the feature class of the layer to have the Float 'value' and Date 'SampleDate'
# ???  

 

Here i want only to be able to select a feature layer for a feature class that have the attribute "value" and "SampleData"

0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

I don't think there's a way to do it as an in-built filter, but you use tool validation to check.

Try something like this? (Not super-tested but it should work)

def updateMessages(self, params):
    Samples = params[0]
    if Samples.value:
        field = {f.name:f.type for f in arcpy.ListFields(Samples.value)}
    valueFieldType = fields.get("value", None)
    SampDateFieldType = fields.get("SampleDate", None)
    if not (valueFieldType and SampDateFieldType):
        Samples.setErrorMessage('Feature Class must have a field named "value"'
                                'and a field named "SampleDate')
    if valueFieldType != "Single":
        Samples.setErrorMessage('"value" field must be a Float field')
    if SampDateFieldType != "Date":
        Samples.setErrorMessage('"SampleDate" field must be a Date field')

 

View solution in original post

3 Replies
AlfredBaldenweck
MVP Regular Contributor

I don't think there's a way to do it as an in-built filter, but you use tool validation to check.

Try something like this? (Not super-tested but it should work)

def updateMessages(self, params):
    Samples = params[0]
    if Samples.value:
        field = {f.name:f.type for f in arcpy.ListFields(Samples.value)}
    valueFieldType = fields.get("value", None)
    SampDateFieldType = fields.get("SampleDate", None)
    if not (valueFieldType and SampDateFieldType):
        Samples.setErrorMessage('Feature Class must have a field named "value"'
                                'and a field named "SampleDate')
    if valueFieldType != "Single":
        Samples.setErrorMessage('"value" field must be a Float field')
    if SampDateFieldType != "Date":
        Samples.setErrorMessage('"SampleDate" field must be a Date field')

 

MortenBackNielsen
Emerging Contributor

Hi @AlfredBaldenweck 
Thank you for you solution, I learn a lot from it. 

I was hoping for a way to filter the options in the drop down, but this works as well Thanks again.

 

 

 

 

def updateMessages(self, params):
    samples = params[0]
    if samples.value:
        fields = {f.name:f.type for f in arcpy.ListFields(samples.value)}
    
        valueFieldType = fields.get("value")
        SampDateFieldType = fields.get("SampleDate")

        if valueFieldType is None or SampDateFieldType is None:
            samples.setErrorMessage('Feature Class must have a field named "value"' 'and a field named "SampleDate')

        if valueFieldType != "Double":
            samples.setErrorMessage(f'"value" field is of type {valueFieldType} must be a Float field')

        if SampDateFieldType != "Date":
            samples.setErrorMessage(f'"SampleDate" field is of type {SampDateFieldType} must be a Date field')

 

 

 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Glad it worked. One thing to watch out for is that you check to see if the field type is "Double", but you your error message says the field type must be "Float", which is actually "Single". You should either change what you're checking for or change your error message.

0 Kudos