更新 Python 工具箱中的方案

类型为要素类、表、栅格或工作空间的每个输出参数都具有 schema 对象。只有输出要素类、表、栅格和工作空间具有 schema;其他类型则没有。schema 对象通过地理处理进行创建。可以通过参数对象访问此方案,或者设置规则以便描述工具的输出。在设置方案规则之后,地理处理内部验证代码会检查设置的规则并更新输出的描述。

回顾一下,控制流程如下:

  1. 在首次打开工具对话框时,调用 getParameterInfo。设置静态规则(不会因用户输入而发生改变的规则)以描述输出。此时不会创建任何输出描述,因为用户尚未指定任何参数的值(除非已提供默认值)。
  2. 一旦用户与工具对话框通过任何方式发生交互,就会调用 updateParameters。
  3. updateParameters 可通过修改 schema 对象来解释无法根据参数依赖项确定的动态行为(例如像“添加字段”工具一样添加新字段)。
  4. 从 updateParameters 获得返回值后,调用内部验证例程并通过应用 schema 对象中的规则更新输出数据的描述。
  5. 然后调用 updateMessages。您可以检查内部验证可能已创建的警告消息和错误消息,然后修改它们或者添加您自己的自定义警告消息和错误消息。

除 type 为只读之外,其他所有 schema 属性均可读写。

属性名称值

type

字符串:"Feature"、"Table"、"Raster"、"Container"(对于工作空间和要素数据集)

clone

布尔

featureTypeRule

字符串:"AsSpecified"、"FirstDependency"

featureType

字符串:"Simple"、"Annotation"、"Dimension"

geometryTypeRule

字符串:"Unknown"、"FirstDependency"、"Min"、"Max"、"AsSpecified"

geometryType

字符串:"Point"、"Multipoint"、"Polyline"、"Polygon"

extentRule

字符串:"AsSpecified"、"FirstDependency"、"Intersection"、"Union"、"Environment"

extent

Extent 对象

fieldsRule

字符串:"None"、"FirstDependency"、"FirstDependencyFIDs"、"All"、"AllNoFIDs"、"AllFIDsOnly"

additionalFields

Field 对象列表

cellSizeRule

字符串:"AsSpecified"、"FirstDependency"、"Min"、"Max"、"Environment"

cellSize

双精度

rasterRule

字符串:"FirstDependency"、"Min"、"Max"、"Integer"、"Float"

rasterFormatRule

字符串:"Img"、"Grid"

additionalChildren

要添加到工作空间方案的数据集列表

schema 对象属性

使用 FirstDependency

许多规则都可设置为 “FirstDependency”,这表示在参数相关性数组集中找到的第一个参数的值将与 parameter.parameterDependencies 一起使用。 在以下代码示例中,参数 2 具有两个依存参数:参数 0 和参数 1,第一个依赖项是参数 0。


# Set the dependencies for the output and its schema properties
#
parameters[2].parameterDependencies = [parameters[0].name, parameters[1].name]

如果任一依存参数为多值(值列表),则会使用多值列表中的第一个值。

type

type 属性是只读的,并通过地理处理进行设置。

clone

如果为 true,则指示地理处理以精确复制(克隆)第一个依存参数中的描述。默认值为 false。通常,在 getParameterInfo 方法中将 clone 设置为 true。如果第一个依存参数为多值(值列表),则会克隆多值列表中的第一个值。

  • 如果 parameter.parameterType 为“Derived”,则会进行精确复制。 这是添加字段工具的行为。
  • 如果 parameter.parameterType 为 “Required”,仍会进行精确复制,但是会更改数据集的目录路径。 目录路径包括两部分:工作空间和基本名称,例如 E:/Data/TestData/netcity.gdb/infrastructure/roads。

    • 工作空间 = E:/Data/TestData/netcity.gdb/infrastructure
    • 基本名称 = roads
    用于构造新输出名称的规则如下:
    • 基本名称与含有数据集的第一个输入参数(不是第一个依赖项而是第一个参数)的基本名称相同,还会附加脚本工具名称(例如,roads_MyTool)。
    • 将工作空间设置为临时工作空间环境设置。 如果未设置临时工作空间环境,则使用当前工作空间环境设置。 如果当前工作空间环境也未设置,则使用包含数据集的第一个输入参数的工作空间。 如果该工作空间为只读,则将使用系统临时目录。

将 clone 设置为 true 后,所有基于规则的方法(例如 featureTypeRule、geometryTypeRule 和 extentRule)均设置为 "FirstDependency"。

下面的两个代码示例有着相同作用。 两个示例均基于使用裁剪工具创建输出方案的过程。

示例 1:显式设置所有规则

class ExampleClipTool1(object):
    def __init__(self):
        self.label = "Example Clip tool 1"
        self.description = "Explicitly setting all rules"

    def getParameterInfo(self):
        # Input feature class
        param0 = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        # Input table
        param1 = arcpy.Parameter(
            displayName="Clip Features",
            name="clip_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        # Input workspace
        param2 = arcpy.Parameter(
            displayName="Output Feature Class",
            name="out_feature_class",
            datatype="DEFeatureClass",
            parameterType="Required",
            direction="Output")

        # Set the dependencies for the output and its schema properties
        #  The two input parameters are feature classes.
        #
        param2.parameterDependencies = [param0.name, param1.name]

        # Feature type, geometry type, and fields all come from the first 
        #  dependency (parameter 0), the input features
        #
        param2.schema.featureTypeRule = "FirstDependency"
        param2.schema.geometryTypeRule = "FirstDependency"
        param2.schema.fieldsRule = "FirstDependency"

        # The extent of the output is the intersection of the input features 
        #  and the clip features (parameter 1)
        #
        param2.schema.extentRule = "Intersection"

        params = [param0, param1, param2]

        return params
示例 2:使用 clone 将规则设置为 FirstDependency 并且覆盖范围规则:

class ExampleClipTool2(object):
    def __init__(self):
        self.label       = "Example Clip tool 2"
        self.description = "Using clone to set rules to FirstDependency, then overriding the extent rule"

    def getParameterInfo(self):
        # Input feature class
        param0 = arcpy.Parameter(
            displayName="Input Features",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        # Input table
        param1 = arcpy.Parameter(
            displayName="Clip Features",
            name="clip_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        # Input workspace
        param2 = arcpy.Parameter(
            displayName="Output Feature Class",
            name="out_feature_class",
            datatype="DEFeatureClass",
            parameterType="Required",
            direction="Output")

        # Set the dependencies for the output and its schema properties
        #  The two input parameters are feature classes.
        #
        param2.parameterDependencies = [param0.name, param1.name]
        param2.schema.clone = True

        params = [param0, param1, param2]

        return params

    def updateParameters(self, parameters):
        # The only property of the clone that changes is that the extent 
        #  of the output is the intersection of the input features 
        #  and the clip features (parameter 1)
        #
        parameters[0].schema.extentRule = "Intersection"
        return

featureTypeRule

该设置用于确定输出要素类的要素类型。 此规则对输出栅格或输出表不起作用。

值描述

"AsSpecified"

要素类型将通过 featureType 属性确定。

"FirstDependency"

要素类型将与依赖项中的第一个参数相同。 如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。

featureTypeRule 值

featureType

当 featureTypeRule 为 "AsSpecified" 时,featureType 的值用于指定输出的要素类型。

值描述

"Simple"

输出将包含简单要素。 要素的几何类型通过 geometryTypeRule 指定。

"Annotation"

输出将包含注记要素。

"Dimension"

输出将包含尺寸要素。

featureType 值

geometryTypeRule

该设置用于确定输出要素类的几何类型(例如点或面)。

值描述

"Unknown"

这是默认设置。 通常,应该能够根据其他参数值确定 updateParameters 中的几何类型。 如果拥有的信息不足以确定几何类型,只需将规则设置为 "Unknown"。

"FirstDependency"

几何类型与第一个依存参数相同。 如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。

"Min","Max"

将检查所有相关参数的几何并且输出几何类型设置为找到的最小或最大类型。 "Min" 和 "Max" 定义如下:

  • 点和多点 = 0
  • 折线 = 1
  • 面 = 2
所以,如果相关参数是点和面要素类,则最小类型将为点,最大类型将为面。

"AsSpecified"

几何类型将通过 geometryType 属性的值确定。

geometryTypeRule 值

geometryType

将此项设置为当 geometryTypeRule 为 "AsSpecified" 时要使用的几何类型("Point"、"Multipoint"、"Polyline" 或 "Polygon")。

extentRule

值描述

"AsSpecified"

输出范围将在 extent 属性中指定。

"FirstDependency"

输出范围与第一个依存参数相同。 如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。

"Intersection"

输出范围将是所有依存参数的几何交集。 (这是裁剪工具使用的输出范围,如下所示。)

"Union"

输出范围将是所有依存参数的几何并集。

"Environment"

输出范围将基于范围环境设置进行计算。

extentRule 值

示例


# The extent of the output is the intersection of the input features 
#  and the clip features (the dependent parameters)
#
parameters[2].schema.extentRule = "Intersection"

范围

将此项设置为当 extentRule 为 "AsSpecified" 时要使用的范围。 可使用以空格分隔字符串或者含有四个值的列表设置范围。 顺序为 xmin、ymin、xmax、ymax。

示例
parameters[2].schema.extentRule = "AsSpecified"
parameters[2].schema.extent = "123.32 435.8 987.3 567.9"
# Alternatively use a list, as follows:
# parameters[2].schema.extent = [123.32, 435.8, 987.3, 567.9]

fieldsRule

fieldsRule 用于确定将在输出要素类或表中存在的字段。

在下表中,FID 代表“要素 ID”,但实际上指每个要素类或表中的 ObjectID 字段。

值描述

"None"

不会输出任何字段,ObjectID 除外。

"FirstDependency"

输出字段将与第一个依存参数相同。 如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。

"FirstDependencyFIDs"

只有第一个依存输入的 ObjectID 会写入到输出中。

"All"

将输出依存参数列表中的所有字段。

"AllNoFIDs"

除 ObjectID 以外的所有字段都将写入输出中。

"AllFIDsOnly"

所有 ObjectID 字段都将写入输出中,但是输入中的其他字段不会写入。

fieldsRule 值

additionalFields

除了通过应用 fieldsRule 添加的字段以外,还可向输出添加更多字段。 additionalFields 将采用字段对象列表。

cellSizeRule

这决定了输出栅格或输出格网的像元大小。

值描述

"AsSpecified"

输出像元大小在 cellSize 属性中指定。

"FirstDependency"

像元大小由第一个依存参数进行计算。 如果相关参数是栅格,则将使用其像元大小。 对于其他类型的依存参数,例如要素类或要素数据集,数据范围用于计算像元大小。 如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。

"Min","Max"

"Min" 表示输出像元大小为相关参数的最小像元大小。 "Max" 表示大小为相关参数的最大像元大小。

"Environment"

输出像元大小基于像元大小环境设置进行计算。

cellSizeRule 值

cellSize

将此项设置为 cellSizeRule 为 "AsSpecified" 时要使用的像元大小。

rasterRule

这用于确定输出栅格中包含的数据类型(整型或浮点型)。

值描述

"FirstDependency"

数据类型(整型或浮点型)与第一个依存参数相同。 如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。

"Min","Max"

整数被视为小于浮点数。 例如,如果有两个依存参数,一个包含整数,另一个包含浮点数,则 "Min" 创建整型输出,"Max" 创建浮点型输出。

"Integer"

输出栅格包含整型数(整数)。

"Float"

输出栅格包含浮点数(小数)。

rasterRule 值

rasterFormatRule

这决定了输出栅格格式,"Grid" 或 "Img"。 默认值为 "Img",它是 ERDAS IMAGINE 格式。 "Grid" 为 Esri 格式。

additionalChildren

工作空间是保存数据集(要素、表和栅格)的容器。 这些数据集相当于工作空间的子辈(如果将工作空间看作父辈的话)。 如果工具将数据集添加到新的或者现有的工作空间中,那么可通过添加对子辈的描述来更新工作空间的描述。 例如,工具可采用要素类列表(多值),以某种方式进行修改,然后将修改后的要素类写入现有工作空间。 在 模型构建器 中使用此工具时,工作空间就是派生的工具输出,可以将该工作空间用作选择数据工具的输入。 通过选择数据可以选择容器中的某个子数据集,并将它用作其他工具的输入。

additionalChildren 的输入是对子辈的一个或多个描述。 对子辈的描述分为两种形式:

形式描述

value 对象

value 属性返回的要素类、表、栅格、尺寸或注记值。

[type, name, fields, extent, spatial reference] 形式的列表对象

含有要添加的子辈描述的列表。 列表中只有前两个条目 type 和 name 是必需的。 其余参数均为可选。

additionalChildren 的成员列表

添加多个子项时,提供子项描述列表。 如果以列表形式添加子项,为 additionalChildren 创建一系列列表。

列表形式具有五个参数,如下表中所述。

参数类型描述

type

必需项

可用值包括:“Point”、“Multipoint”、“Polyline”、“Polygon”、“Table”、“Raster”、“Annotation”、“Dimension”

name

必需项

数据集的名称。 可以是数据集的基本名称 (streets) 或完整目录路径 (E:\mydata\test.gdb\infrastructure\streets)。 如果提供了完整的目录路径,将忽略基本名称 (streets) 以外的全部内容。

fields

可选

字段对象列表。 这包含子辈中出现的字段(如果已知)。

extent

可选

包含子辈空间范围的字符串或列表。

spatial reference

可选

空间参考对象。

子辈列表的内容

这些参数必须按照下列顺序提供。 要跳过可选参数,使用 None 或 '#'。

下面是设置工作空间方案的示例。 这些示例基于具有下列参数的脚本工具:

参数索引参数名称属性

0

输入要素类

要素类 - 输入。

1

输入表

表 - 输入。

2

输入工作空间

工作空间 - 输入(包含工具结果的现有工作空间)。

3

派生工作空间

工作空间 - 派生输出,获取自输入工作空间。 该工作空间的方案经修改可包含其他子辈。

示例工具参数

此工具将获取输入要素类和表,将二者复制到工作空间,向要素类添加新字段,然后在工作空间中创建面要素类。 (此工具的实际作用并不是很重要,因为它仅仅是用于举例说明工作空间方案的设置。)这些代码示例彼此关联,从 additionalChildren 的简单用法开始。

在 getParameterInfo 中,输出工作空间通过其依存参数 (param2) 进行克隆。

class ExampleTool(object):
    def __init__(self):
        self.label       = "Example tool"
        self.description = "Example of parameter dependencies"

    def getParameterInfo(self):
        #Define parameter definitions

        # Input feature class
        param0 = arcpy.Parameter(
            displayName="Input feature class",
            name="in_features",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input")

        # Input table
        param1 = arcpy.Parameter(
            displayName="Input table",
            name="in_table",
            datatype="GPTableView",
            parameterType="Required",
            direction="Input")

        # Input workspace
        param2 = arcpy.Parameter(
            displayName="Input workspace",
            name="in_workspace",
            datatype="DEWorkspace",
            parameterType="Required",
            direction="Input")

        # Derived workspaces
        param3 = arcpy.Parameter(
            displayName="Derived workspace",
            name="out_workspace",
            datatype="DEWorkspace",
            parameterType="Derived",
            direction="Output")

        # Set dependencies to the input workspace parameter
        param3.parameterDependencies = [param0.name]

        # Copy all existing contents to output
        param3.schema.clone = True

        params = [param0, param1, param2, param3]

        return params

Example: Copy the two inputs (no modifications) to the output workspace:


def updateParameters(self, parameters):
    inFC = parameters[0].value     # input feature class
    inTable = parameters[1].value  # input table
    inWS = parameters[2].value     # input workspace
    if inFC and inTable and inWS:
        parameters[3].schema.additionalChildren = [inFC, inTable]

    return

Example: The tool creates a new polygon feature class. The only properties known about this new feature class (when validating) are the name ("SummaryPolygon") and type ("polygon").


def updateParameters(self, parameters):
    children = []    # New empty list
    children.append(parameters[0].value)
    children.append(parameters[1].value)
    children.append(["polygon", "SummaryPolygon"])
    parameters[3].schema.additionalChildren = children

    return

Example: Add a field to the input feature class.


def updateParameters(self, parameters):
    
    # Create a field object with the name "Category" and type "Long"
    #
    newField = arcpy.Field()
    newField.name = "Category"
    newField.type = "Long"

    # Describe the input feature class in order to get its list of fields. The 9.3
    #  version of the geoprocessing object returns fields in a Python list, unlike
    #  previous versions, which returned fields in an enumerator.
    #
    desc = arcpy.Describe(parameters[0].value)
    fieldList = desc.fields

    # Add the new field to the list
    #
    fieldList.append(newField)

    # Create a new child based on the input feature class, but with the 
    #  additional field added
    #
    newChild = [desc.shapeType, desc.catalogPath, fieldList, 
               desc.extent, desc.spatialReference]

    # Now create our list of children and add to the schema
    #
    children = []
    children.append(newChild)
    children.append(inTable)
    children.append(["polygon", "SummaryPolygon"])
    parameters[3].schema.additionalChildren = children

    return

要为 SummaryPolygon(新的面要素类)创建字段,请创建与上面示例中所示模式相似的字段对象的列表。

Example: Multivalue input

In this example, the first parameter is a multivalue of feature classes. Each feature class in the multivalue is copied to the derived workspace. A new field, "ProjectID", is added to each of the copied feature classes.


# 0 - input features (multivalue)
# 1 - input workspace
# 2 - derived workspace

def updateParameters(self, parameters):
    inVT = parameters[0].value   # multivalue ValueTable
    inWS = parameters[1].value   # WorkSpace

    # Add each feature class to the output workspace. In addition,
    #  add a new field "ProjectID" to each feature class
    #
    if inVT and inWS:
        rowCount = inVT.rowCount  # Row count in MultiValue table
        children = []
        newField = arcpy.Field()
        newField.name = "ProjectID"
        newField.type = "Long"
        for row in range(0, rowCount):
            value = inVT.getValue(row, 0)
            if value:
                d = arcpy.Describe(value)
                fieldList = d.fields

                # Note -- not checking if field already exists
                #
                fieldList.append(newField)

                # Create new child with additional ProjectID 
                #  field and add child to list of children
                #
                child = [d.shapeType, d.catalogPath, fieldList]
                children.append(child)            
              
        parameters[2].schema.additionalChildren = children
    return

相关主题