Tuesday 21 January 2020

parameters - Wildcard for field in arcpy.GetParameterAsText


I have this parameters for my python script:


import arcpy

Layer1 = arcpy.GetParameterAsText(0)
Tabela1 = arcpy.GetParameterAsText(1)
Estado = arcpy.GetParameterAsText(2)
Ano = arcpy.GetParameterAsText(3).split(';')
mxd = arcpy.mapping.MapDocument("CURRENT")

Saida = arcpy.GetParameterAsText(4)
stats = []

Parameter 'Ano' is a multivalue field selection and this parameter obtained the value list from the feature class 'Tabela1'. There is many fields in this FC and all of them are listed in 'Ano' but I only want to show the fields with a prefix ACH*. The script works well, But don`t want to get the user confusing with too many fields in the 'Ano' field.



Answer



Unfortunately, the only filter you can set on the parameter of Field data type is the field type, not its name.


In order to achieve the name filtering, you would need to use a custom validator.


  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:
self.params[2].filter.list = [f.name for f in arcpy.ListFields(self.params[0].value,
wild_card='POP*')]
return

This code would populate the third script tool parameter (of String type, Multivalue) with the names that start with POP. The second parameter shows all the fields (Field type, Multivalue), the third one only the filtered:


enter image description here


If you would like to keep your business logic code (think source code of the script tool) and the validation code (what happens to parameters and messages when user interacts with it) in one single file, then look at Python toolboxes. One single text .pyt file which can contain one or more script tools.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...