类型为要素类、表、栅格或工作空间的每个输出参数都具有 schema 对象。只有输出要素类、表、栅格和工作空间具有 schema,其他类型则没有。schema 对象通过地理处理进行创建。可以通过参数对象访问此方案,或者设置规则以便描述工具的输出。在设置方案规则之后,地理处理内部验证代码会检查设置的规则并更新输出的描述。
回顾一下,控制流程如下:
- 在首次打开工具对话框时,调用 getParameterInfo。设置静态规则(不会因用户输入而发生改变的规则)以描述输出。此时不会创建任何输出描述,因为用户尚未指定任何参数的值(除非已提供默认值)。
- 一旦用户与工具对话框通过任何方式发生交互,就会调用 updateParameters。
- updateParameters 可通过修改 schema 对象来解释无法根据参数依赖项确定的动态行为(例如像“添加字段”工具一样添加新字段)。
- 从 updateParameters 获得返回值后,调用内部验证例程并通过应用 schema 对象中的规则更新输出数据的描述。
- 然后调用 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 | 字段对象的 Python 列表 | 
| cellSizeRule | 字符串:“AsSpecified”、“FirstDependency”、“Min”、“Max”、“Environment” | 
| cellsize | 双精度 | 
| rasterRule | 字符串:“FirstDependency”、“Min”、“Max”、“Integer”、“Float” | 
| rasterFormatRule | 字符串:“Img”、“Grid” | 
| additionalChildren | 要添加到工作空间方案的数据集的 Python 列表 | 
使用 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 属性是只读的,并通过地理处理进行设置。
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” | 要素类型将与依赖项中的第一个参数相同。如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。 | 
featureType
当 featureTypeRule 为“AsSpecified”时,featureType 中的值用于指定输出的要素类型。
| 值 | 描述 | 
|---|---|
| “Simple” | 输出将包含简单要素。要素的几何类型通过 geometryTypeRule 指定。 | 
| “Annotation” | 输出将包含注记要素。 | 
| “Dimension” | 输出将包含尺寸要素。 | 
geometryTypeRule
该设置用于确定输出要素类的几何类型(例如点或面)。
| 值 | 描述 | 
|---|---|
| “Unknown” | 这是默认设置。通常,应该能够根据其他参数值确定 updateParameters() 中的几何类型。如果没有足够的信息用于确定几何类型,只需将规则设置为“Unknown”即可。 | 
| “FirstDependency” | 几何类型与第一个依存参数相同。如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。 | 
| “Min”、“Max” | 检查所有依存参数的几何,并将输出几何类型设置为找到的最小类型或最大类型。“Min”和“Max”定义如下: 
 | 
| “AsSpecified” | 几何类型将通过 geometryType 属性的值确定。 | 
geometryType
当 geometryTypeRule 为“AsSpecified”时,将该属性设置为要使用的几何类型(“Point”、“Multipoint”、“Polyline”或“Polygon”)。
extentRule
| 值 | 描述 | 
|---|---|
| “AsSpecified” | 输出范围将在 extent 属性中指定。 | 
| “FirstDependency” | 输出范围与第一个依存参数相同。如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。 | 
| “Intersection” | 输出范围将是所有依存参数的几何交集。(这是裁剪工具使用的输出范围,如下所示。) | 
| “Union” | 输出范围将是所有依存参数的几何并集。 | 
| “Environment” | 输出范围将基于输出范围环境设置进行计算。 | 
示例
# 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”时,将该属性设置为要使用的范围。可使用以空格分隔的字符串或者使用含有四个值的 Python 列表对象来设置范围。顺序为 xmin、ymin、xmax、ymax。
示例
parameters[2].schema.extentRule = "AsSpecified"
parameters[2].schema.extent = "123.32 435.8 987.3 567.9"
或者使用 Python 列表
xmin = 123.32
ymin = 435.8
xmax = 987.3
ext = [xmin, ymin, xmax, 567.9]
parameters[2].schema.extent = ext
fieldsRule
fieldsRule 用于确定将在输出要素类或输出表上存在的字段。
在下表中,FID 代表“要素 ID”,但实际上是指每个要素类或表中的 ObjectID 字段。
| 值 | 描述 | 
|---|---|
| “None” | 除对象 ID 以外,将不输出任何字段。 | 
| “FirstDependency” | 输出字段将与第一个依存参数相同。如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。 | 
| “FirstDependencyFIDs” | 只有第一个依存输入的 ObjectID 会写入到输出中。 | 
| “All” | 将输出依存参数列表中的所有字段。 | 
| “AllNoFIDs” | 除 ObjectID 以外的所有字段都将写入到输出中。 | 
| “AllFIDsOnly” | 所有 ObjectID 字段都将写入到输出中,但是输入中的其他字段不会写入。 | 
additionalFields
除了通过应用 fieldsRule 添加的字段以外,还可向输出添加其他字段。additionalFields 将采用字段对象的 Python 列表。
cellSizeRule
此属性用于确定输出栅格或输出格网的像元大小。
| 值 | 描述 | 
|---|---|
| “AsSpecified” | 输出像元大小在 cellSize 属性中指定。 | 
| “FirstDependency” | 像元大小由第一个依存参数进行计算。如果依存参数是栅格,就会使用它的像元大小。对于其他类型的依存参数,例如要素类或要素数据集,数据范围用于计算像元大小。如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。 | 
| “Min”、“Max” | “Min”表示输出像元大小是依存参数的最小像元大小。“Max”表示它是依存参数的最大像元大小。 | 
| “Environment” | 输出像元大小基于 cellsize 环境设置进行计算。 | 
cellSize
当 cellSizeRule 为“AsSpecified”时,将该属性设置为要使用的像元大小。
rasterRule
该属性将确定输出栅格中包含的数据类型(整型或浮点型)。
| 值 | 描述 | 
|---|---|
| “FirstDependency” | 数据类型(整型或浮点型)与第一个依存参数相同。如果第一个依存参数为多值(值列表),则会使用多值列表中的第一个值。 | 
| “Min”、“Max” | 整数被视为小于浮点数。例如,如果有两个依存参数,一个包含整数,另一个包含浮点数,则“Min”创建整型输出,“Max”创建浮点型输出。 | 
| “Integer” | 输出栅格包含整型数(整数)。 | 
| “Float” | 输出栅格包含浮点数(小数)。 | 
rasterFormatRule
该属性确定输出栅格格式,“Grid”或“Img”。默认值为“Img”,它是 ERDAS IMAGINE 格式。“Grid”是 Esri 的格式。
additionalChildren
工作空间是保存数据集(要素、表和栅格)的容器。这些数据集相当于工作空间的子辈(如果将工作空间看作父辈的话)。如果工具将数据集添加到新的或者现有的工作空间中,那么可通过添加对子辈的描述来更新工作空间的描述。例如,工具可采用要素类列表(多值),以某种方式进行修改,然后将修改后的要素类写入现有工作空间。在模型构建器中使用此工具时,工作空间就是派生的工具输出,可能会需要将该工作空间用作选择数据工具的输入。通过选择数据可以选择容器中的某个子数据集,并将它用作其他工具的输入。
additionalChildren 的输入是对子辈的一个或多个描述。对子辈的描述分为两种形式:
| 构成 | 描述 | 
|---|---|
| value 对象 | value 属性可返回要素类、表、栅格、尺寸或注记值。 | 
| [类型, 名称, 字段, 范围, 空间参考] 形式的 Python 列表对象 | 含有要添加的子辈描述的 Python 列表。列表中只有前两个条目“类型”和“名称”是必需的。其余参数均为可选。 | 
添加多个子辈时,可以提供子辈描述的列表。如果使用 Python 列表对象形式添加子辈,将会为 additionalChildren 创建列表的列表。
Python 列表形式具有五个参数,如下表中所述。
| 参数 | 类型 | 描述 | 
|---|---|---|
| 类型 | 必选项 | 可用值包括:“Point”、“Multipoint”、“Polyline”、“Polygon”、“Table”、“Raster”、“Annotation”、“Dimension” | 
| 名称 | 必选项 | 数据集的名称。可以是数据集的基本名称(“streets”)或完整目录路径(“E:\mydata\test.gdb\infrastructure\streets”)。在给定完整目录路径的情况下,基本名称(“streets”)以外的内容都将被忽略。 | 
| 字段 | 可选 | Python 字段对象列表。这包含子辈中出现的字段(如果已知)。 | 
| 范围 | 可选 | 包含子辈空间范围的字符串或 Python 列表。 | 
| 空间参考 | 可选 | 空间参考对象。 | 
这些参数必须按照下列顺序提供。要跳过可选参数,可使用 Python 关键字 None 或“#”。
以下是设置工作空间方案的一些示例。这些示例基于具有下列参数的脚本工具:
| 参数名称 | 属性 | |
|---|---|---|
| 0 | 输入要素类 | 要素类 - 输入。 | 
| 1 | 输入表 | 表 - 输入。 | 
| 2 | 输入工作空间 | 工作空间 - 输入(包含工具结果的现有工作空间)。 | 
| 3 | 派生工作空间 | 工作空间 - 派生输出,获取自 Input_workspace。该工作空间的方案经修改可包含其他子辈。 | 
此工具获取输入要素类和表,将它们复制到工作空间,向要素类添加新字段,然后在工作空间中创建新的面要素类。(此工具的实际作用并不是很重要,因为它仅仅是用于举例说明工作空间方案的设置。)这些代码示例彼此关联,从 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
示例:将两个输入(未修改)复制到输出工作空间:
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
示例:此工具可创建一个新的面要素类。与该新要素类有关的已知属性(验证时)仅有名称(“SummaryPolygon”)和类型(“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
示例:向输入要素类添加一个字段。
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(新的面要素类)创建字段,请创建与上面示例中所示模式相似的字段对象的列表。
示例:多值输入
在该示例中,第一个参数是由要素类构成的多值。多值中的每个要素类均复制到派生工作空间中。新字段“ProjectID”将添加到每个复制的要素类中。
# 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