Filter (arcpy)
摘要
The filter object allows you to specify the choices available for a parameter.
讨论
filter 对象允许为参数指定用户可用的选择。例如,可设置仅限选择文本字段的字段过滤器。过滤器执行三项工作:
- 过滤器仅提供在浏览数据时作出有效选择的用户。如果设置的过滤器是针对点要素类的,那么用户浏览数据时仅会显示点要素类。如果设置的过滤器是针对文本字段的,则字段的下拉列表仅显示文本字段。
- 如果由用户输入参数值(而不是从列表或文件浏览器中选择值),则会根据过滤器对值进行检查。如果用户输入无效值(例如不是文本字段,而是数值字段),则会自动给出警告或提示错误。
- 因为值是由内部验证根据过滤器进行检查,所以过滤器使您无需在 ToolValidator 类中编写自定义验证。
可通过两种方式指定过滤器:
- 在工具的属性对话框的参数选项卡上,单击参数,然后单击“过滤器”旁的像元,再从下拉列表中选择过滤器类型。选择过滤器类型之后,将打开一个对话框,用于指定过滤器的值。
- 可通过编程方式在 ToolValidator 类中设置值(示例如下所示)。地理处理将自动为字符串、长整型、双精度型、要素类、文件、字段和工作空间类型的参数创建过滤器。即使不在工具的属性对话框中选择过滤器,参数仍会关联一个过滤器 - 只不过它是空的。空过滤器等同于没有过滤器。通过向空过滤器添加值以激活过滤器,而用户的选择会受到过滤器内容的限制。
属性
属性 | 说明 | 数据类型 |
list (读写) |
If you do not want to filter values, set the list property to an empty list. The datatype specifiied depends on the filter type (ValueList, Range, FeatureClass, File, Field, and Workspace). | String |
type (读写) |
The type of filter (ValueList, Range, FeatureClass, File, Field, and Workspace). You can set the type of filter when dealing with Long and Double parameters. For other types of parameters, there is only one valid type of filter, so setting the type on these parameters is ignored. If you do not want to filter values, set the List property to an empty list. | Object |
代码实例
An example of dynamically updating a Value List Filter containing a choice list of keywords. If the user enters "OLD_FORMAT" in the second parameter, the third parameter contains "POINT, LINE, and POLYGON". If "NEW_FORMAT" is entered, the third parameter contains three additional choices.
class ToolValidator:
def __init__(self):
import arcpy
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
return
def updateParameters(self):
# Provide default values for "file format type" and
# "feature type in file"
#
if not self.params[1].altered:
self.params[1].value = "OLD_FORMAT"
if not self.params[2].altered:
self.params[2].value = "POINT"
# Update the value list filter of the "feature type in file" parameter
# depending on the type of file (old vs. new format) input
#
if self.params[1].value == "OLD_FORMAT":
self.params[2].filter.list = ["POINT", "LINE", "POLYGON"]
elif self.params[1].value == "NEW_FORMAT":
self.params[2].filter.list = ["POINT", "LINE", "POLYGON",
"POINT_WITH_ANNO",
"LINE_WITH_ANNO",
"POLYGON_WITH_ANNO"]
return
def updateMessages(self):
return
An example where the Value List Filter in the second parameter changes based on the shape type found in the first parameter, a feature class.
def updateParameters(self):
# Update the value list filter in the second parameter based on the
# shape type in the first parameter
#
stringFilter = self.params[1].filter
fc = self.params[0].value
if fc:
shapetype = arcpy.Describe(fc).shapeType.lower()
if shapetype == "point" or shapetype == "multipoint":
stringFilter.list = ["RED", "GREEN", "BLUE"]
elif shapetype == "polygon":
stringFilter.list = ["WHITE", "GRAY", "BLACK"]
else:
stringFilter.list = ["ORANGE", "INDIGO", "VIOLET"]
else:
stringFilter.list = ["RED", "GREEN", "BLUE"]
# If the user hasn't changed the keyword value, set it to the default value
# (first value in the value list filter).
#
if not self.params[1].altered:
self.params[1].value = stringFilter.list[0]
return