当前位置:   article > 正文

ArcPy(1):比较自定义工具箱和Python工具箱创建地理处理工具_arcpy自定义工具

arcpy自定义工具

0 版本

  • ArcGIS:10.6
  • python:2.7.14

1 两种工具箱对比

注:以下内容总结自Esri官网

ArcGIS 10.1 起,使用 Python 创建自定义地理处理工具的方式有以下两种:自定义工具箱中的脚本工具和 Python 工具箱中的脚本工具。二者异同:

自定义工具箱Python 工具箱
组织方式
  1. 通过向导定义的工具和参数定义。
  2. 对参数行为提供额外控制的可选验证代码,其位于工具箱中。
  3. 在单独的文件中为每个工具维护的源代码,通常作为 Python 脚本 (.py)。
  1. Python 工具箱是一个具有 .pyt 扩展名的 Python 脚本;
  2. 其包含工具箱及其工具的所有方面:参数、验证和执行。
  3. 通过以下 Python 类实现:一个类用于工具箱,一个类用于各工具。

优缺点:自定义工具箱各部分都是分离的,而且很难进行集中管理;而在Python工具箱中,参数定义、代码验证和源代码都在同一位置进行处理,因而Python工具的创建和维护更加容易。


2 创建自定义地理处理工具

注:按照上表中的步骤,以“为文件夹内shp文件批量添加字段”这一功能为例说明,代码中的具体函数不做进一步说明

2.1 使用自定义工具箱

2.1.1 编写批量添加字段代码

  1. from imp import reload
  2. import arcpy
  3. # import os
  4. # from os import path
  5. import sys
  6. import traceback
  7. reload(sys)
  8. sys.setdefaultencoding('utf8')
  9. inFCDir = arcpy.GetParameterAsText(0)
  10. fieldName = arcpy.GetParameterAsText(1)
  11. fieldType = arcpy.GetParameterAsText(2)
  12. arcpy.env.workspace = inFCDir
  13. try:
  14. fileList = arcpy.ListFiles("*.shp")
  15. for file in fileList:
  16. arcpy.AddField_management(file, fieldName, fieldType)
  17. arcpy.AddMessage("{0} successfully".format(file))
  18. printSuccess() #封装方法:打印成功提示信息
  19. except arcpy.ExecuteError:
  20. printArcPyException() #封装方法:打印arcpy异常信息
  21. except:
  22. printException() #封装方法:打印python异常信息

2.1.2 通过向导定义的工具和参数定义

  • 在“ArcGIS->我的工具箱->右键->新建工具箱”创建自定义工具箱;
  • 在“自定义工具箱->右键->添加脚本”创建自定义地理处理工具,并设置上步的源文件及参数定义(工具参数定义需对应源码中arcpy.GetParameterAsText创建的参数),如下图所示;


 2.2 使用Python工具箱

2.2.1 新建Python工具箱

在“ArcGIS->我的工具箱->右键->新建Python工具箱”创建Python工具箱;

2.2.2 Python工具箱模板

创建新 Python 工具箱时,工具箱创建为名为 Toolbox 的类。在 Toolbox 类的 __init__ 方法中已定义工具箱的属性,其中包括 alias、label 和 description。工具箱的名称由 .pyt 文件的名称定义。必须将 tools 属性设置为包含工具箱中定义的所有工具类的列表。本例中,创建一个名为 BatAddField 的工具。直接上代码,哈哈。

  1. import arcpy
  2. class Toolbox(object):
  3. def __init__(self):
  4. """Define the toolbox (the name of the toolbox is the name of the
  5. .pyt file)."""
  6. self.label = "pptools"
  7. self.alias = "pp arcpy tools"
  8. # List of tool classes associated with this toolbox
  9. self.tools = [BatAddField]
  10. class BatAddField(object):
  11. def __init__(self):
  12. """Define the tool (tool name is the name of the class)."""
  13. self.label = "Bat Add Field"
  14. self.description = ""
  15. self.canRunInBackground = False
  16. def getParameterInfo(self):
  17. """Define parameter definitions """
  18. param0 = arcpy.Parameter(displayName="Input folder",
  19. name="in_dir",
  20. datatype="DEFolder",
  21. parameterType="Required",
  22. direction="Input")
  23. param1 = arcpy.Parameter(displayName="Field Name",
  24. name="field_name",
  25. datatype="GPString",
  26. parameterType="Required",
  27. direction="Input")
  28. param2 = arcpy.Parameter(displayName="Field Type",
  29. name="field_type",
  30. datatype="GPString",
  31. parameterType="Required",
  32. direction="Input")
  33. param2.filter.type = "ValueList"
  34. param2.filter.list = [
  35. 'Short', 'Long', 'Float', 'Single', 'Double', 'Text', 'Date'
  36. ]
  37. params = [param0, param1, param2]
  38. return params
  39. def isLicensed(self):
  40. """Set whether tool is licensed to execute."""
  41. return True
  42. def updateParameters(self, parameters):
  43. """Modify the values and properties of parameters before internal
  44. validation is performed. This method is called whenever a parameter
  45. has been changed."""
  46. return
  47. def updateMessages(self, parameters):
  48. """Modify the messages created by internal validation for each tool
  49. parameter. This method is called after internal validation."""
  50. return
  51. def execute(self, parameters, messages):
  52. """The source code of the tool."""
  53. arcpy.env.workspace = parameters[0].valueAsText
  54. fieldName = parameters[1].valueAsText
  55. fieldType = parameters[2].valueAsText
  56. try:
  57. fileList = arcpy.ListFiles("*.shp")
  58. for file in fileList:
  59. arcpy.AddField_management(file, fieldName, fieldType)
  60. arcpy.AddMessage("{0} successfully".format(file))
  61. #addField(fileList, fieldName, fieldType)
  62. printSuccess(messages)
  63. except arcpy.ExecuteError:
  64. printArcPyException(messages)
  65. except:
  66. printException(messages)
  67. return

函数说明:

  • getParameterInfo(self):用于定义工具参数;

  • isLicensed(self):工具是否可用;

  • updateParameters(self, parameters):当更改参数值时候触发

  • updateMessages(self, parameters):This method is called after internal validation.

  • execute(self, parameters, messages):执行工具


3 最终工具展示

最终两种工具展示的结果是一样的,但是python工具箱的参数定义、代码验证和源代码都在同一位置进行处理,所以更易于维护。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/310729
推荐阅读
相关标签
  

闽ICP备14008679号