更新 Python 工具箱中的方案
类型为要素类、表、栅格或工作空间的每个输出参数都具有 schema 对象。只有输出要素类、表、栅格和工作空间具有 schema,其他类型则没有。schema 对象通过地理处理进行创建。可以通过参数对象访问此方案,或者设置规则以便描述工具的输出。在设置方案规则之后,地理处理内部验证代码会检查设置的规则并更新输出的描述。
回顾一下,控制流程如下:
- 在首次打开工具对话框时,调用 getParameterInfo。设置静态规则(不会因用户输入而发生改变的规则)以描述输出。此时不会创建任何输出描述,因为用户尚未指定任何参数的值(除非已提供默认值)。
- 一旦用户与工具对话框通过任何方式发生交互,就会调用 updateParameters。
- updateParameters 可通过修改 schema 对象来解释无法根据参数依赖项确定的动态行为(例如像“添加字段”工具一样添加新字段)。
- 从 updateParameters 获得返回值后,调用内部验证例程并通过应用 schema 对象中的规则更新输出数据的描述。
- 然后调用 updateMessages。您可以检查内部验证可能已创建的警告消息和错误消息,然后修改它们或者添加您自己的自定义警告消息和错误消息。
除 type 是只读属性以外,其他所有 schema 属性均是可读写的。
Property name | Value(s) |
---|---|
type | String: "Feature", "Table", "Raster" , "Container" (for workspaces and feature datasets) (Read-only property) |
clone | Boolean |
featureTypeRule | String: "AsSpecified", "FirstDependency" |
featureType | String: "Simple", "Annotation", "Dimension" |
geometryTypeRule | String: "Unknown", "FirstDependency", "Min", "Max", "AsSpecified" |
geometryType | String: "Point", "Multipoint", "Polyline", "Polygon" |
extentRule | String: "AsSpecified", "FirstDependency", "Intersection", "Union", "Environment" |
extent | Extent object |
fieldsRule | String: "None", "FirstDependency", "FirstDependencyFIDs", "All", "AllNoFIDs", "AllFIDsOnly" |
additionalFields | Python list of field objects |
cellSizeRule | String: "AsSpecified", "FirstDependency", "Min", "Max", "Environment" |
cellsize | Double |
rasterRule | String: "FirstDependency", "Min", "Max", "Integer", "Float" |
rasterFormatRule | String: "Img", "Grid" |
additionalChildren | Python list of datasets to add to a workspace 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 属性是只读的,并通过地理处理进行设置。
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”。
下面的两个代码示例有着相同作用。两个示例均基于使用裁剪工具创建输出方案的过程。
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
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
该设置用于确定输出要素类的要素类型。此规则对输出栅格或输出表不起作用。
Value | Description |
---|---|
"AsSpecified" | The feature type will be determined by the featureType property. |
"FirstDependency" | The feature type will be the same as the first parameter in the dependencies. If the first dependent parameter is a multivalue (a list of values), the first value in the multivalue list is used. |
featureType
当 featureTypeRule 为“AsSpecified”时,featureType 中的值用于指定输出的要素类型。
Value | Description |
---|---|
"Simple" | The output will contain simple features. The geometry type of the features is specified with geometryTypeRule. |
"Annotation" | The output will contain annotation features. |
"Dimension" | The output will contain dimension features. |
geometryTypeRule
该设置用于确定输出要素类的几何类型(例如点或面)。
Value | Description |
---|---|
"Unknown" | This is the default setting. Typically, you should be able to determine the geometry type in updateParameters() based on the values of other parameters. You'd only set the rule to "Unknown" if you don't have enough information to determine the geometry type. |
"FirstDependency" | The geometry type is the same as the first dependent parameter. If the first dependent parameter is a multivalue (a list of values), the first value in the multivalue list is used. |
"Min", "Max" | Examines the geometries of all dependent parameters and sets the output geometry type to the minimum or maximum type found. "Min" and "Max" are defined as follows:
|
"AsSpecified" | The geometry type will be determined by the value of the geometryType property. |
geometryType
当 geometryTypeRule 为“AsSpecified”时,将该属性设置为要使用的几何类型(“Point”、“Multipoint”、“Polyline”或“Polygon”)。
extentRule
Value | Description |
---|---|
"AsSpecified" | The output extent will be specified in the extent property. |
"FirstDependency" | The output extent is the same as the first dependent parameter. If the first dependent parameter is a multivalue (a list of values), the first value in the multivalue list is used. |
"Intersection" | The output extent will be the geometric intersection of all dependent parameters. (This is what the Clip tool uses, as shown below.) |
"Union" | The output extent will be the geometric union of all dependent parameters. |
"Environment" | The output extent will be calculated based on the output extent environment setting. |
示例
# 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 字段。
Value | Description |
---|---|
"None" | No fields will be output except for the object ID. |
"FirstDependency" | Output fields will be the same as the first dependent parameter. If the first dependent parameter is a multivalue (a list of values), the first value in the multivalue list is used. |
"FirstDependencyFIDs" | Only the ObjectID of the first dependent input will be written to the output. |
"All" | All fields in the list of dependent parameters will be output. |
"AllNoFIDs" | All fields except for the ObjectIDs will be written to the output. |
"AllFIDsOnly" | All ObjectID fields are written to the output, but no other fields from the inputs will be written. |
additionalFields
除了通过应用 fieldsRule 而添加的字段以外,还可向输出添加其他字段。additionalFields 将采用字段对象的 Python 列表。
cellSizeRule
此属性用于确定输出栅格或输出格网的像元大小。
Value | Description |
---|---|
"AsSpecified" | The output cellsize is specified in the cellSize property. |
"FirstDependency" | The cell size is calculated from the first dependent parameter. If the dependent parameter is a raster, then its cellsize is used. For other types of dependent parameters, such as feature classes or feature datasets, the extent of the data is used to calculate a cellsize. If the first dependent parameter is a multivalue (a list of values), the first value in the multivalue list is used. |
"Min", "Max" | "Min" means the output cellsize is the smallest cellsize of the dependent parameters. "Max" means it is the largest cellsize of the dependent parameters. |
"Environment" | The output cellsize is calculated based on the cellsize environment setting. |
cellSize
当 cellSizeRule 为“AsSpecified”时,将该属性设置为要使用的像元大小。
rasterRule
该属性将确定输出栅格中包含的数据类型(整型或浮点型)。
Value | Description |
---|---|
"FirstDependency" | The data type (integer or float) is the same as the first dependent parameter. If the first dependent parameter is a multivalue (a list of values), the first value in the multivalue list is used. |
"Min", "Max" | Integer is considered smaller than float. For example, if there are two dependent parameters, one containing integers and the other containing floats, "Min" creates an integer output, and "Max" creates a float output. |
"Integer" | The output raster contains integers (whole numbers). |
"Float" | The output raster contains floats (fractional numbers). |
rasterFormatRule
该属性确定输出栅格格式,“Grid”或“Img”。默认值为“Img”,它是 ERDAS IMAGINE 格式。“Grid”是 Esri 的格式。
additionalChildren
工作空间是保存数据集(要素、表和栅格)的容器。这些数据集相当于工作空间的子辈(如果将工作空间看作父辈的话)。如果工具将数据集添加到新的或者现有的工作空间中,那么可通过添加对子辈的描述来更新工作空间的描述。例如,工具可采用要素类列表(多值),以某种方式进行修改,然后将修改后的要素类写入现有工作空间。在模型构建器中使用此工具时,工作空间就是派生的工具输出,可能会需要将该工作空间用作选择数据工具的输入。通过选择数据可以选择容器中的某个子数据集,并将它用作其他工具的输入。
additionalChildren 的输入是对子辈的一个或多个描述。对子辈的描述分为两种形式:
Form | Description |
---|---|
value object | A feature class, table, raster, dimension, or annotation value, as returned by the value property. |
Python list object of the form [type, name, fields, extent, spatial reference] | A Python list containing a description of the child to be added. Only the first two entries in the list, type and name, are required. The remaining arguments are optional. |
添加多个子辈时,可以提供子辈描述的列表。如果使用 Python 列表对象形式添加子辈,将会为 additionalChildren 创建列表的列表。
Python 列表形式具有五个参数,如下表中所述。
Argument | Type | Description |
---|---|---|
type | required | One of the following: "Point", "Multipoint", "Polyline", "Polygon", "Table", "Raster", "Annotation", "Dimension" |
name | required | The name of the dataset. It can just be the base name of the dataset ("streets") or the full catalog path ("E:\mydata\test.gdb\infrastructure\streets"). When a full catalog path is given, all but the base name ("streets") is ignored. |
fields | optional | A Python list of field objects. This contains the fields appearing on the child, if known. |
extent | optional | A string or Python list containing the spatial extent of the child. |
spatial reference | optional | A spatial reference object. |
这些参数必须按照下列顺序提供。要跳过可选参数,可使用 Python 关键字 None 或“#”。
以下是设置工作空间方案的一些示例。这些示例基于具有下列参数的脚本工具:
Parameter name | Properties | |
---|---|---|
0 | Input feature class | Feature class—input. |
1 | Input table | Table—input. |
2 | Input workspace | Workspace—input (an existing workspace that contains the results of the tool). |
3 | Derived workspace | Workspace—Derived output, obtained from Input_workspace. The schema of this workspace is modified to contain additional children. |
此工具获取输入要素类和表,将它们复制到工作空间,向要素类添加新字段,然后在工作空间中创建新的面要素类。(此工具的实际作用并不是很重要,因为它仅仅是用于举例说明工作空间方案的设置。)以下代码示例基于另一个代码示例,从 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