Skip to main content

什么是 Python 工具箱

Python 工具箱是在 Python 中创建的地理处理工具箱。 Python 工具箱及其工具的外观、操作和运行方式与任何以其他方式创建的工具箱和工具相同。 Python 工具箱是一个带有 .pyt 扩展名的 Python 文件,该文件定义了一个工具箱以及一个或多个工具。

创建后,Python 工具箱中的工具将提供以下功能:

  • 您创建的脚本工具会同系统工具一样成为地理处理的组成部分,您可以从目录窗格中将其打开,可以在 ModelBuilder 和 Python 窗口中使用它,还可以从其他脚本中调用它。

  • 您可以将消息写入地理处理历史窗口和工具对话框。

  • 使用内置的文档工具,可以创建文档。

  • 将脚本作为脚本工具运行时,ArcPy 知晓调用它的应用程序。 应用程序中的设置,例如,arcpy.env.overwriteOutputarcpy.env.scratchWorkspace 都可从脚本工具中的 ArcPy 中获得。

创建 Python 工具箱

要创建 Python 工具箱,右键单击要在其中创建工具箱的文件夹,然后单击新建 > Python 工具箱

最初,Python 工具箱包含一个名为 Toolbox 的 Python 类(用于定义工具箱的特征),以及名为 Tool 的 Python 类(提供无存根的地理处理工具)。

下表显示了在 Python 工具箱中创建工具的步骤和信息链接:

入门

步骤

信息

入门

新建 Python 工具箱

编辑 Python 工具箱

定义工具

定义工具

定义工具参数

定义 Python 工具箱中的参数

在 Python 工具箱中定义参数数据类型

自定义工具行为

自定义 Python 工具箱中的工具行为

更新 Python 工具箱中的方案

控制 Python 工具箱中的许可行为

Python 工具箱中的后处理验证

编写工具的源代码

访问 Python 工具箱中的参数

在 Python 工具箱中写入消息

编写工具的文档说明

记录自定义地理处理工具

Python 工具箱示例

以下示例是包含单个工具的 Python 工具箱。 名为 CalculateSinuosity 的工具添加一个字段并计算要素的曲折度,曲折度用于衡量直线的弯曲程度。

注:

要使用该工具,请将示例代码复制到 Python 集成开发环境 (IDE) 中,或复制到 Notepad 中,然后以 .pyt 扩展名保存该文件。

import arcpy
class Toolbox(object):
 def __init__(self):
     self.label =  "Sinuosity toolbox"
     self.alias  = "sinuosity"
     # List of tool classes associated with this toolbox
     self.tools = [CalculateSinuosity]
class CalculateSinuosity(object):
 def __init__(self):
     self.label       = "Calculate Sinuosity"
     self.description = "Sinuosity measures the amount that a river " + \
                        "meanders within its valley, calculated by " + \
                        "dividing total stream length by valley length."
 def getParameterInfo(self):
     #Define parameter definitions
     # Input Features parameter
     in_features = arcpy.Parameter(
         displayName="Input Features",
         name="in_features",
         datatype="GPFeatureLayer",
         parameterType="Required",
         direction="Input")
     in_features.filter.list = ["Polyline"]
     # Sinuosity Field parameter
     sinuosity_field = arcpy.Parameter(
         displayName="Sinuosity Field",
         name="sinuosity_field",
         datatype="Field",
         parameterType="Optional",
         direction="Input")
     sinuosity_field.value = "sinuosity"
     # Derived Output Features parameter
     out_features = arcpy.Parameter(
         displayName="Output Features",
         name="out_features",
         datatype="GPFeatureLayer",
         parameterType="Derived",
         direction="Output")
     out_features.parameterDependencies = [in_features.name]
     out_features.schema.clone = True
     parameters = [in_features, sinuosity_field, out_features]
     return parameters
 def isLicensed(self):
     return True
 def updateParameters(self, parameters):
     if parameters[0].altered:
         parameters[1].value = arcpy.ValidateFieldName(parameters[1].value,
                                                       parameters[0].value)
     return
 def updateMessages(self, parameters):
     return
 def execute(self, parameters, messages):
     inFeatures  = parameters[0].valueAsText
     fieldName   = parameters[1].valueAsText
     if fieldName in ["#", "", None]:
         fieldName = "sinuosity"
     arcpy.management.AddField(inFeatures, fieldName, 'DOUBLE')
     expression = '''
import math
def getSinuosity(shape):
 length = shape.length
 d = math.sqrt((shape.firstPoint.X - shape.lastPoint.X) ** 2 +
               (shape.firstPoint.Y - shape.lastPoint.Y) ** 2)
 return d/length
'''
     arcpy.management.CalculateField(inFeatures,
                                     fieldName,
                                     'getSinuosity(!shape!)',
                                     'PYTHON_9.3',
                                     expression)
 def postExecute(self, parameters):
     return

调试

Python 工具箱可以通过 Python 集成式开发环境 (IDE) 进行调试。

了解有关调试 Python 工具的详细信息