ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • 帮助
  • Sign Out
ArcGIS Desktop

ArcGIS Online

专为贵组织打造的制图平台

ArcGIS Desktop

全面的专业性 GIS

ArcGIS Enterprise

面向企业的 GIS

ArcGIS Developers

用于构建位置感知应用程序的工具

ArcGIS Solutions

适用于行业的免费模板地图和应用程序

ArcGIS Marketplace

获取适用于组织的应用程序和数据

  • 文档
  • 支持
Esri
  • 登录
user
  • 我的个人资料
  • 登出

ArcMap

  • 主页
  • 入门
  • 地图
  • 分析
  • 管理数据
  • 工具
  • 扩展模块

什么是 Python 工具箱?

  • 创建 Python 工具箱
  • Python 工具箱示例

Python 工具箱 Python 工具箱 是完全用 Python 语言创建的地理处理工具箱。Python 工具箱及其所包含工具的外观、操作和运行方式与任何以其他方式创建的工具箱和工具相类似。Python 工具箱 (.pyt) 只是一个基于 ASCII 的文件,该文件定义了工具箱和一个或多个工具。

创建后,Python 工具箱中的工具具备以下优势:

  • 通过创建 Python 工具箱,您可以利用您的 Python 知识来快速构建原型并创建功能完备的地理处理工具。
  • 您所创建的工具会像系统工具一样成为地理处理的组成部分,您可以从搜索 或目录 窗口中打开它,可在模型构建器和 Python 窗口中使用它,还可以从脚本中调用它。
  • 您可以将消息写入结果 窗口和进度对话框。
  • 使用内置的文档工具,可以创建文档。
  • 将脚本作为脚本工具运行时,arcpy 完全知道从哪个应用程序(如 ArcMap)调用该脚本。在应用程序中所做的设置(如 arcpy.env.overwriteOutput 和 arcpy.env.scratchWorkspace)都可从脚本工具中的 ArcPy 中获得。

创建 Python 工具箱

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

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

入门

入门

创建新 Python 工具箱

编辑 Python 工具箱

定义工具

定义工具

定义工具的参数

定义 Python 工具箱中的参数

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

自定义工具行为

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

更新 Python 工具箱中的方案

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

编写工具的“源代码”

访问 Python 工具箱中的参数

在 Python 工具箱中写入消息

记录工具

对 Python 工具箱中的工具编写文档

Python 工具箱示例

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

注:

要使用该工具,请复制示例代码并将其粘贴到任何编辑器中,如记事本,并用 .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): #optional
        return True

    def updateParameters(self, parameters): #optional
        if parameters[0].altered:
            parameters[1].value = arcpy.ValidateFieldName(parameters[1].value,
                                                          parameters[0].value)
        return

    def updateMessages(self, parameters): #optional
        return

    def execute(self, parameters, messages):
        inFeatures  = parameters[0].valueAsText
        fieldName   = parameters[1].valueAsText
        
        if fieldName in ["#", "", None]:
            fieldName = "sinuosity"

        arcpy.AddField_management(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.CalculateField_management(inFeatures,
                                        fieldName,
                                        'getSinuosity(!shape!)',
                                        'PYTHON_9.3',
                                        expression)

相关主题

  • 使用 Python 创建工具的快速浏览
  • 比较自定义工具箱和 Python 工具箱

ArcGIS Desktop

  • 主页
  • 文档
  • 支持

ArcGIS

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS
  • ArcGIS Developer
  • ArcGIS Solutions
  • ArcGIS Marketplace

关于 Esri

  • 关于我们
  • 招贤纳士
  • Esri 博客
  • 用户大会
  • 开发者峰会
Esri
分享您的想法。
Copyright © 2021 Esri. | 隐私政策 | 法律声明