Skip to main content

Parameter

汇总

每个工具参数都拥有一个包含可用于工具验证的属性和方法的关联 Parameter 对象。

讨论

虽然很多 Parameter 对象属性为读/写,但其中大多数属性只能在最初创建或修改对象时进行设置或修改。 以下属性建立了参数的特征,并且在验证方法(例如 updateMessagesupdateParameters )执行期间不能修改:

  • category

  • controlCLSID

  • datatype

  • direction

  • displayName

  • displayOrder

  • multiValue

  • name

  • parameterDependencies

  • parameterType

语法

Parameter({name}, {displayName}, {direction}, {datatype}, {parameterType}, {enabled}, {category}, {symbology}, {multiValue})

名称 说明 数据类型

name

(可选)

参数名称。

(默认值为 None)

String

displayName

(可选)

地理处理 窗格中显示的参数标注。

(默认值为 None)

String

direction

(可选)

指定参数的方向。

(默认值为 Input)

String

datatype

(可选)

参数的数据类型。

了解有关地理处理数据类型的详细信息

(默认值为 GPString)

String

parameterType

(可选)

指定参数类型。

(默认值为 Required)

String

enabled

(可选)

指定参数在 地理处理 窗格中是否可见。

(默认值为 True)

Boolean

category

(可选)

参数的类别。

(默认值为 None)

String

symbology

(可选)

用于绘制输出的图层文件( .lyrx.lyr )的路径。

(默认值为 None)

String

multiValue

(可选)

指定参数是否为多值参数。

(默认值为 False)

Boolean

属性

名称 说明 数据类型

altered

(只读)

制定用户是否修改了值。

Boolean

category

(读取和写入)

参数的类别。

String

charts

(读取和写入)

添加至输出参数的 Chart 对象列表。

Object

columns

(读取和写入)

“值表”参数中的列结构。

这些列按列表的列表形式进行组织,每个内部列表表示列数据类型、列名称和可选只读值。 如果只读值设置为 ReadOnly ,则用户无法修改该列。

import arcpy
param = arcpy.Parameter()
param.datatype = "GPValueTable"
param.columns = [["GPFeatureLayer", "Features"], ["GPLong", "Ranks"]]

String

controlCLSID

(读取和写入)

可用于覆盖数据类型的默认控件的类标识符。

了解有关参数控件的详细信息

String

datatype

(读取和写入)

参数的数据类型。

了解有关地理处理数据类型的详细信息

String

defaultEnvironmentName

(读取和写入)

将用于设置参数默认值的地理处理环境名称。

String

direction

(读取和写入)

指定参数的方向。

  • Input - 输入参数

  • Output - 输出参数

String

displayName

(读取和写入)

地理处理 窗格中显示的参数标注。

String

displayOrder

(读取和写入)

参数在 地理处理 窗格中的显示顺序。 该顺序可能与从 Python 访问参数的顺序不同。

Integer

enabled

(读取和写入)

指定参数在 地理处理 窗格中是否可见。

Boolean

filter

(只读)

要应用于参数中值的 过滤器

Filter

filters

(读取和写入)

filter 属性相似,但是用于支持值表和复合参数。

对于值表参数,设置 filters 属性,并使用 list 和可选 type 为值表中的每一列定义 Filter 对象。

class ValueTableSampleTool(object):
    # __init__ left out to simplify example

    def getParameterInfo(self):
        in_fc = arcpy.Parameter(
            name='in_features',
            displayName='Input Features',
            datatype='GPFeatureLayer',
            direction='Input',
            parameterType='Required')

        vt = arcpy.Parameter(
            name='summary_fields',
            displayName='Summary fields',
            datatype='GPValueTable',
            direction='Input',
            parameterType='Optional')

        vt.parameterDependencies = [in_fc.name]
        vt.columns = [['Field', 'Field'], ['GPString', 'Statistic'], ['GPDouble', 'Multiplier']]
        vt.filters[0].list = ['Double', 'Float', 'Short', 'Long']
        vt.filters[1].type = 'ValueList'
        vt.filters[1].list = ['SUM', 'MIN', 'MAX', 'MEAN']
        vt.filters[2].type = 'Range'
        vt.filters[2].list = [0, 10]

        return [in_fc, vt]

对于复合参数,设置 filters 属性,并使用复合参数中的每种数据类型的 list 定义 Filter 对象。

class CompositeSampleTool(object):
    # __init__ left out to simplify example

    def getParameterInfo(self):
        composite = arcpy.Parameter(
            name="composite_example",
            displayName="Composite Example",
            datatype=["GPString", "GPDouble"],
            parameterType="Optional",
            direction="Input",
            multiValue=True)

        composite.filters = ["CodedValue", "Range"]
        composite.filters[0].list = ["MIN", "MAX"]
        composite.filters[1].list = [1, 100]

        return [composite]

Filter

hasBeenValidated

(只读)

指定内部验证例程是否检查了参数。

Boolean

message

(只读)

要向用户显示的消息。

String

multiValue

(读取和写入)

指定参数是否为多值参数。

Boolean

name

(读取和写入)

参数名称。

String

parameterDependencies

(读取和写入)

各依存参数的索引列表。

在脚本工具中,使用参数索引列表设置 parameterDependencies ;在 Python 工具箱工具中,使用参数名称列表设置 parameterDependencies

Integer

parameterType

(读取和写入)

指定参数类型。

  • Required - 在提供值之前,该工具无法运行。

  • Optional - 无需为参数提供值。

  • Derived - 参数将返回输出值。 派生参数始终为输出参数,不会在 地理处理 窗格中显示。

对于派生参数,将 direction 属性设置为 Output 并将 parameterType 属性设置为 Derived

在验证中,无法动态修改 parameterType 值。 但是,根据其他参数设置,参数可能需要充当必需参数或可选参数。 如果是这样,将参数设置为可选。 然后,在验证 updateMessages 方法中,将 Parameter setIDMessage 方法与消息 ID 530 或 735 搭配使用。 使用消息 ID 530 或 735 时将导致可选参数的行为与必需参数相同。

String

schema

(只读)

输出数据集的 方案

Schema

symbology

(读取和写入)

用于绘制输出的图层文件( .lyrx.lyr )的路径。

String

value

(读取和写入)

参数的值。

Object

valueAsText

(只读)

字符串形式的参数值。

String

values

(读取和写入)

值表参数的值,使用一系列列表进行设置。

Object

方法

clearMessage()

清除所有消息文本并将状态设置为信息性状态(无错误或警告)。

hasError()

如果参数包含错误,则返回 True

要评估参数是否包含错误,应在工具验证的 updateMessages 方法中调用 hasError

返回值

数据类型 说明

Boolean

如果参数包含错误,则为 True

hasWarning()

指定参数是否包含警告。

返回值

数据类型 说明

Boolean

返回参数是否包含警告。

isInputValueDerived()

指定是否在模型内部验证工具,以及输入值是否是模型中另一个工具的输出。

返回值

数据类型 说明

Boolean

返回是否在模型内部验证工具,并且输入值是否是模型中另一个工具的输出。

setErrorMessage(message)

通过提供的消息将参数设置为存在错误。 如果其中任何参数存在错误,则不会执行工具。

名称 说明 数据类型

message

The string to be added as an error message to the geoprocessing tool messages.

String

setIDMessage(message_type, message_ID, {add_argument1}, {add_argument2})

将参数设置为存在系统消息。

名称 说明 数据类型

message_type

Defines whether the message will be an error or a warning.

  • ERROR—The message will be an error message.

  • WARNING—The message will be a warning message.

String

message_ID

The message ID allows you to reference existing system messages.

Integer

add_argument1

(可选)

Depending on which message ID is used, an argument may be necessary to complete the message. Common examples include dataset or field names. The data type is variable depending on the message.

Object

add_argument2

(可选)

Depending on which message ID is used, an argument may be necessary to complete the message. Common examples include dataset or field names. The data type is variable depending on the message.

Object

setWarningMessage(message)

通过提供的消息将参数设置为存在错误。 与存在错误时不同,工具在有警告消息的情况下也会执行。

名称 说明 数据类型

message

The string to be added as a warning message to the geoprocessing tool messages.

String

代码示例

参数示例

ToolValidator 类中启用或禁用参数。

def updateParameters(self):
    # If the option to use a weights file ("Get Spatial Weights From File")
    # is selected, enable the parameter for specifying the file;
    # otherwise, disable it.

    if self.params[3].value == "Get Spatial Weights From File":
        self.params[8].enabled = True
    else:
        self.params[8].enabled = False

    return
参数示例 2

ToolValidator 类中的参数设置默认值。

def updateParameters(self):
    # Set the default distance threshold to 1/100 of the larger of
    # the width or height of the extent of the input features.  Do
    # not set if there is no input dataset yet, or the user has set
    # a specific distance (Altered is true).

    if self.params[0].value:
        if not self.params[6].altered:
            extent = arcpy.Describe(self.params[0].value)
        width = extent.XMax - extent.XMin
        height = extent.YMax - extent.YMin

        if width < height:
            self.params[6].value = width / 100
        else:
            self.params[6].value = height / 100

        return
参数示例 3

ToolValidator 类中的参数设置自定义错误消息。

def updateMessages(self):
    self.params[6].clearMessage()

    # Check whether the threshold distance contains a value of
    # zero and the user has specified a fixed distance band.
    if self.params[6].value <= 0:
        if self.params[3].value == "Fixed Distance Band":
            self.params[6].setErrorMessage(
                "Zero or a negative distance is invalid when "
                "using a fixed distance band. Please use a "
                "positive value greater than zero.")
        elif self.params[6].value < 0:
            self.params[6].setErrorMessage(
                "A positive distance value is required when "
                "using a fixed distance band. Be sure to specify "
                "a distance.")

    return