Python 最初只能识别存储在 ArcGIS 系统工具箱(如“数据管理工具”工具箱、“转换工具”工具箱和“分析工具”工具箱)中的工具。通过将自定义工具箱导入到 ArcPy 站点包,在 Python 窗口中,可以像访问任何系统工具一样访问由个人、第三方或组织创建的、保存在自定义工具箱中的自定义工具。
①Python访问普通工具箱(.tbx)中的工具
下例中,使用了 ImportToolbox 函数,以便使用 Python 访问自定义工具箱中包含的工具。导入该工具箱后,即能够以 arcpy.<工具名称>_<别名> 形式访问自定义工具。
>>> arcpy.ImportToolbox("c:/mytools/geometrytools.tbx") >>> arcpy.CreateRegularPolygons_geometry(
如果未定义工具箱别名,可设置一个临时别名,使其作为 ImportToolbox 函数的第二个参数。
>>> arcpy.ImportToolbox("c:/mytools/geometrytools.tbx", "mytools") >>> arcpy.CreateRegularPolygons_mytools(
②添加或者删除服务器工具箱
地理处理服务也可通过 ImportToolbox 添加到脚本环境中。无论是从本地还是从 Internet 服务器添加地理处理服务,服务器名称和工具箱名称都用分号分隔。
添加地理处理服务的示例语法
# Import a geoprocessing service # import arcpy # To add a toolbox from a Internet server, provide the url and toolbox name # delimited by a semi-colon # arcpy.ImportToolbox("http://lab13/arcgis/services;BufferByVal")
添加本地地理处理服务的示例语法
# Import a local geoprocessing service # import arcpy # To add a toolbox from a local server, provide the server and toolbox name # delimited by a semi-colon # arcpy.ImportToolbox("lab13;BufferByVal")
③Python访问Python工具箱(.pyt)中的工具
导入Python工具箱后,能够以arcpy.<工具名称>形式访问自定义工具
>>>arcpy.ImportToolbox(r"D:\制作GP工具\CommonTools.pyt") >>>arcpy.Tool(r"D:\制作GP工具\data1706.csv","POINT_X","POINT_Y",r"D:\制作GP工具\scratch.gdb\test")
下述示例介绍如何在Python窗口使用Python工具箱中的工具
具体实现:
Python工具箱:
# -*- coding: utf-8 -*- 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 = "Toolbox" self.alias = "" # List of tool classes associated with this toolbox self.tools = [Tool] class Tool(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "CSV转点" self.description = "CSVToPoint" self.canRunInBackground = False def getParameterInfo(self): """Define parameter definitions""" in_table = arcpy.Parameter( displayName="输入表", name="输入表", datatype="DEFile", parameterType="Required", direction="Input") in_table.filter.list = ['csv'] x_coords = arcpy.Parameter( displayName='x坐标', name='x坐标', datatype='Field', parameterType='Required', direction='Input') x_coords.parameterDependencies = [in_table.name] y_coords = arcpy.Parameter( displayName='y坐标', name='y坐标', datatype='Field', parameterType='Required', direction='Input') y_coords.parameterDependencies = [in_table.name] outFC = arcpy.Parameter( displayName='要素类', name='要素类', datatype='DEFeatureClass', parameterType='Required', direction='Output') params = [in_table, x_coords, y_coords, outFC] return params def isLicensed(self): return True def updateParameters(self, parameters): return def updateMessages(self, parameters): return def execute(self, parameters, messages): # Get tool parameters in_table = parameters[0].valueAsText arcpy.AddMessage("输入表 is {0}".format(in_table)) x_coords = parameters[1].valueAsText arcpy.AddMessage("x坐标 is {0}".format(x_coords)) y_coords = parameters[2].valueAsText arcpy.AddMessage("y坐标 is {0}".format(y_coords)) outFC = parameters[3].valueAsText arcpy.AddMessage("要素类 is {0}".format(outFC)) idws = IDW(in_table, x_coords, y_coords, outFC) idws.idwByFields() return class IDW(object): def __init__(self, param1, param2, param3, param4): self.param1 = param1 # 第一个参数 self.param2 = param2 # 第二个参数 self.param3 = param3 # 第三个参数 self.param4 = param4 # 第四个参数 def idwByFields(self): arcpy.env.overwriteOutput = "true" # Set the local variables in_table = self.param1 out_feature_class = self.param4 x_coords = self.param2 y_coords = self.param3 z_coords = "" # Make the XY event layer... arcpy.management.XYTableToPoint(in_table, out_feature_class, x_coords, y_coords, z_coords, arcpy.SpatialReference(4326))
Python窗口使用Python工具箱:
第三方Python编辑器,例如:pycharm,也是可以这样调用Python工具箱中的工具
import arcpy # Import custom toolbox arcpy.ImportToolbox(r"D:\制作GP工具\CommonTools.pyt") try: # Run tool in the custom toolbox. The tool is identified by # the tool name and the toolbox alias. arcpy.Tool(r"D:\制作GP工具\data1706.csv","POINT_X","POINT_Y",r"D:\制作GP工具\scratch.gdb\test11222") except arcpy.ExecuteError: print(arcpy.GetMessages(2))
其中arcpy.Tool中的这个“Tool”是Python工具箱某个工具的名称,对应Python工具箱代码部分的self.tools = [Tool]中的这个“Tool”。
作者:gislaozhang
链接:https://blog.csdn.net/gislaozhang/article/details/105054562
来源:CSDN
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。