什么是 Python 工具箱?

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

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

创建 Python 工具箱

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

最初,Python 工具箱包含一个名为 Toolbox 的 Python 类(用于定义工具箱的特征),以及另一个名为 Tool 的 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)

相关主题

9/15/2013