Python工具箱中使用值表(GPValueTable)

需求:

参数数据类型为值表,值表中有两列,一列是字段,一列是字符串路径,如何实现选择其中一列字段,会将要素数据的存储位置自动填充给字符串列

效果图:

具体实现:

import arcpy

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "test"
        self.alias = "test"

        # List of tool classes associated with this toolbox
        self.tools = [testTool1]

class testTool1(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "testTool"
        self.description = "test"
        self.canRunInBackground = False

    def getParameterInfo(self):
        param0 = arcpy.Parameter(
            displayName='Input Features',
            name='in_features',
            datatype="GPFeatureLayer",
            parameterType='Required',
            direction='Input')
        param1 = arcpy.Parameter(
            displayName='test',
            name='stat_fields',
            datatype='GPValueTable',
            parameterType='Required',
            direction='Input')
        param1.parameterDependencies = [param0.name]
        param1.columns = [['Field', 'Field'], ['GPString', 'path']]

        params = [param0, param1]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        if parameters[1].value:
            str=parameters[1].valueAsText

            if str.endswith('#'):
                fstr = parameters[0].valueAsText
                # arcpy.AddMessage("fstr is {0}".format(fstr))
                if fstr.find(' ') >= 0 or fstr.find(';')>=0:
                    fstr = fstr.replace(' ', '<>')
                    fstr = fstr.replace(';', '[]')
                    parameters[1].values = str[:-1].strip() + " " + fstr
                else:
                    parameters[1].values = str[:-1].strip() + " " + fstr


        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""

        return

其中param1依赖param0;

param1.columns中定义了两列,列名为Field、path,字段类型为Field和GPString;

updateParameters方法中设置字符串列的值,字符串列的值为param0的值,以响应用户在字段列输入值。

同时在updateParameter中用于判断空间数据存放路径有没有空格或者分号,如果有会用特殊字符进行替换。

参考链接:

http://pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/defining-parameter-data-types-in-a-python-toolbox.htm 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值