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

2020-10-13 14:05发布

需求:

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

具体实现:

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 



作者:gislaozhang

链接:https://blog.csdn.net/gislaozhang/article/details/104687007

来源:CSDN
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。