赞
踩
注:以下内容总结自Esri官网
自 ArcGIS 10.1 起,使用 Python 创建自定义地理处理工具的方式有以下两种:自定义工具箱中的脚本工具和 Python 工具箱中的脚本工具。二者异同:
自定义工具箱 | Python 工具箱 | |
组织方式 |
|
|
优缺点:自定义工具箱各部分都是分离的,而且很难进行集中管理;而在Python工具箱中,参数定义、代码验证和源代码都在同一位置进行处理,因而Python工具的创建和维护更加容易。
注:按照上表中的步骤,以“为文件夹内shp文件批量添加字段”这一功能为例说明,代码中的具体函数不做进一步说明
- from imp import reload
- import arcpy
- # import os
- # from os import path
- import sys
- import traceback
-
- reload(sys)
- sys.setdefaultencoding('utf8')
-
- inFCDir = arcpy.GetParameterAsText(0)
- fieldName = arcpy.GetParameterAsText(1)
- fieldType = arcpy.GetParameterAsText(2)
-
- arcpy.env.workspace = inFCDir
-
- try:
- fileList = arcpy.ListFiles("*.shp")
- for file in fileList:
- arcpy.AddField_management(file, fieldName, fieldType)
- arcpy.AddMessage("{0} successfully".format(file))
- printSuccess() #封装方法:打印成功提示信息
- except arcpy.ExecuteError:
- printArcPyException() #封装方法:打印arcpy异常信息
- except:
- printException() #封装方法:打印python异常信息
在“ArcGIS->我的工具箱->右键->新建Python工具箱”创建Python工具箱;
创建新 Python 工具箱时,工具箱创建为名为 Toolbox 的类。在 Toolbox 类的 __init__ 方法中已定义工具箱的属性,其中包括 alias、label 和 description。工具箱的名称由 .pyt 文件的名称定义。必须将 tools 属性设置为包含工具箱中定义的所有工具类的列表。本例中,创建一个名为 BatAddField 的工具。直接上代码,哈哈。
- 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 = "pptools"
- self.alias = "pp arcpy tools"
- # List of tool classes associated with this toolbox
- self.tools = [BatAddField]
-
- class BatAddField(object):
- def __init__(self):
- """Define the tool (tool name is the name of the class)."""
- self.label = "Bat Add Field"
- self.description = ""
- self.canRunInBackground = False
-
- def getParameterInfo(self):
- """Define parameter definitions """
- param0 = arcpy.Parameter(displayName="Input folder",
- name="in_dir",
- datatype="DEFolder",
- parameterType="Required",
- direction="Input")
- param1 = arcpy.Parameter(displayName="Field Name",
- name="field_name",
- datatype="GPString",
- parameterType="Required",
- direction="Input")
- param2 = arcpy.Parameter(displayName="Field Type",
- name="field_type",
- datatype="GPString",
- parameterType="Required",
- direction="Input")
- param2.filter.type = "ValueList"
- param2.filter.list = [
- 'Short', 'Long', 'Float', 'Single', 'Double', 'Text', 'Date'
- ]
- params = [param0, param1, param2]
- 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."""
- arcpy.env.workspace = parameters[0].valueAsText
- fieldName = parameters[1].valueAsText
- fieldType = parameters[2].valueAsText
- try:
- fileList = arcpy.ListFiles("*.shp")
- for file in fileList:
- arcpy.AddField_management(file, fieldName, fieldType)
- arcpy.AddMessage("{0} successfully".format(file))
- #addField(fileList, fieldName, fieldType)
- printSuccess(messages)
- except arcpy.ExecuteError:
- printArcPyException(messages)
- except:
- printException(messages)
- return
函数说明:
getParameterInfo(self):用于定义工具参数;
isLicensed(self):工具是否可用;
updateParameters(self, parameters):当更改参数值时候触发
updateMessages(self, parameters):This method is called after internal validation.
execute(self, parameters, messages):执行工具
最终两种工具展示的结果是一样的,但是python工具箱的参数定义、代码验证和源代码都在同一位置进行处理,所以更易于维护。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。