前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ArcGIS Pro创建python工具箱

ArcGIS Pro创建python工具箱

作者头像
GIS指北
发布2022-11-14 10:59:15
6480
发布2022-11-14 10:59:15
举报
文章被收录于专栏:GIS指北GIS指北

python工具箱使用python类构建,所有工具写在一个.pyt文件中。

01

创建和编辑python工具箱

在catalog中新建一个pyt工具箱,其下会自动生成一个默认名称的工具Tool。用任意编辑器将.pyt与python文件做关联,以下为在vs code中关联:

代码语言:javascript
复制
"files.associations": {
        "*.pyt": "python"
    },

打开后能看到工具箱的模板

代码语言:javascript
复制
import arcpy

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

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

class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False
    
    def getParameterInfo(self):
        """Define parameter definetions"""
        params = None
        return params
        
    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal validation is performed. 
        This method is called whenever a parameter has been changed."""
        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

工具箱使用python类和方法构建,Toolbox类名称不能更改,其__init__方法定义了工具箱的属性,self.tools定义了包含的所有工具名称列表,self.label定义标签,self.alias定义别名,self.category可以把工具组织成不同工具集。

工具以类的形式定义,类名可更改,举个例子:

代码语言:javascript
复制
class Toolbox(object):
    def __init__(self):
        self.label = "Random Sample Tools"
        self.alias = "randomsampleing"
        self.tools = [RandomSample]
class RandomSample(object):
    def __init__(self):
        self.label = "Random Sample"

保存脚本文件后,更新工具箱,可以看到如下变化:

工具箱使用python类和方法构建,Toolbox类名称不能更改,

  • __init__方法定义了工具箱的属性,
  • self.tools定义了包含的所有工具名称列表,
  • self.label定义标签,self.alias定义别名,
  • self.category可以把工具组织成不同工具集。

工具类定义了六种方法,__init__定义了工具属性;

  • getParameterInfo()定义了参数,类似脚本工具属性中的参数界面;
  • execute()定义工具源码,必要方法,只包括该方法也可以运行工具,但是没有参数界面;
  • isLicensed()可以控制许可行为,验证能否执行,检入检出许可;
  • updateParameters()定义了工具内部验证的过程,比如输入数据达到某个条件,则启用或者禁用某个参数,或者为某个参数设置默认值;
  • updateMessage()定义了工具内部验证并返回消息的过程,比如输入数据不满足要求,则返回消息输入不可用。

02

定义工具和参数

getParameterInfo()方法定义工具参数,每个参数用Parameter类创建对象。

Parameter对象有多个属性,

  • filter属性可以限定参数类型;
  • displayOrder定义参数在工具框的显示顺序;
  • parameterDependencies定义参数依赖性;

有多个方法,多用来做消息处理。

定义完参数后需要返回参数列表,如下

代码语言:javascript
复制
 def getParameterInfo():
     param0 = arcpy.Parameter(
         displayName="Input Features",
         name="in_features",
         datatype="GPFeatureLayer",
         parameterType="Required",
         direction="Input")
      param0.filter.list = ["Polyline"]
      parameters = [param0]
      return parameters

可以对参数命名为param0,param1,param2,如此规律命令方便索引;当然也可以以其他方式命名。

在pro中更新python工具箱,如下,更改已应用到工具界面的参数属性中。

03

创建Random Sample工具

根据参数定义的方法,对上一篇脚本工具中的Random Sample定义参数如下

代码语言:javascript
复制
class RandomSample(object):
    def __init__(self):
        self.label = "Random Sample"
    def getParameterInfo(self):
        input_features = arcpy.Parameter(
        name="input_features",
        displayName="Input Features",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")
    output_features = arcpy.Parameter(
        name="output_features",
        displayName="Output Features",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Output")
    no_of_features = arcpy.Parameter(
        name="number_of_features",
        displayName="Number of Features",
        datatype="GPLong",
        parameterType="Required"
        direction="Input")
    no_of_features.filter.type = "Range"
    no_of_features.filter.list = [1, 1000000000]
    params = [input_features, output_features, no_of_features]
    return params

更新工具后,可以在pro界面查看界面和属性。

最后想要工具能执行,还要定义execute()方法,通过Parameter的value和valueAsText方法获取之前定义的参数,因为最后有输出结果,execute不需要返回值。

代码语言:javascript
复制
    def execute(self, params, messages):
        """The source code of the tool."""

        inputfc = params[0].valueAsText
        outputfc = params[1].valueAsText
        outcount = params[2].value
        inlist = []
        with arcpy.da.SearchCursor(inputfc, "OID@") as cursor:
            for row in cursor:
                id = row[0]
                inlist.append(id)
        randomlist = random.sample(inlist, outcount)
        desc = arcpy.da.Describe(inputfc)
        fldname = desc["OIDFieldName"]
        sqlfield = arcpy.AddFieldDelimiters(inputfc,fldname)
        sqlexp = "{} in {}".format(sqlfield,tuple(randomlist))
        arcpy.Select_analysis(inputfc, outputfc, sqlexp)

工具类中只有execute方法是必须的,getParameterInfo方法定义了工具界面和参数属性,updateParameters方法可以对工具做更精细的控制。

04

比较脚本工具与python工具箱

脚本工具和python工具各有优势,都能创建自定义工具。

脚本工具是自定义工具箱(.tbx)的一部分,关联了一份.py文件,工具界面在pro内设置。

python工具箱所有代码都在.pyt文件中,工具界面也在代码中设置完成。

但是只能在IDE中debug py文件。两者组织工具文档的方式类似。

参考

https://pro.arcgis.com/zh-cn/pro-app/latest/arcpy/geoprocessing_and_python/a-quick-tour-of-creating-tools-in-python.htm

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-08-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 GIS指北 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档