日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

在ArcGIS中认识 Python工具箱

發布時間:2024/1/23 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在ArcGIS中认识 Python工具箱 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

認識 Python工具箱

python 工具箱 (.pyt) 是一個簡單的文本文件,可以在任何文本編輯器中或者任何 Python IDE 中創建、查看和編輯。要確保 ArcGIS 正確識別 Python 工具箱,工具箱類的名稱必須是 Toolbox。在 Toolbox 類的 __init__ 方法中定義工具箱的屬性,這些屬性包括 alias、label 和 description,我們可以按照 幫助文檔 中的模板構建 Python 工具箱模板。

如下代碼中創建了包含一個工具(名為 Tool)的 Python 工具箱:

import arcpyclass 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 toolboxself.tools = [Tool]class Tool(object):def __init__(self):"""Define the tool (tool name is the name of the class)."""self.label = "Tool"self.description = ""self.canRunInBackground = Falsedef getParameterInfo(self):"""Define parameter definitions"""params = Nonereturn paramsdef isLicensed(self):"""Set whether tool is licensed to execute."""return Truedef updateParameters(self, parameters):"""Modify the values and properties of parameters before internalvalidation is performed. This method is called whenever a parameterhas been changed."""returndef updateMessages(self, parameters):"""Modify the messages created by internal validation for each toolparameter. This method is called after internal validation."""returndef execute(self, parameters, messages):"""The source code of the tool."""return

動手做做

下面我就依據這個模板,寫一個簡單的腳本工具箱。需求是批量裁剪,我希望我只提供一個文件夾或者數據庫等這樣的工作空間和一個裁剪區域面,就可以批量完成工作空間內全部數據的裁剪工作,并且無視柵格還是矢量,一并裁剪。

Let the scripting begin ……


1 創建工具箱

工具箱的name就是 .pyt 文件的名字,通常我們把工具添加到 ArcToolbox窗口中時會顯示工具箱的 label。在 Toolbox 類的 __init__ 方法中定義屬性,例如: aliaslabeldescription

工具作為被添加至 .pyt 中,工具箱的 tools 屬性必須設置為包含定義的所有工具列表。比如,需要做ATool,ATool,CTool三個工具,不是寫三個腳本,而是創建三個,然后將類名放入列表, self.tools = [ATool,ATool,CTool]


這里,我僅定義一個工具類 ClipWorkspace,來說明構建過程即可 :

''' Source Name: ClipWorkspace.pyt Author: Kikita Description: Python tool to clip spatial data in the same workspace by batch. '''import arcpy# The class name must be "Toolbox" ... class Toolbox(object):def __init__(self):self.label = "Clip Workspace Toolbox"self.alias = ""# List of tool classes associated with this toolboxself.tools = [ClipWorkspace]class ClipWorkspace(object):……


在 ArcGIS Desktop 中已經可以看到這個工具箱的雛形:


2 定義工具

下面就是完善工具內部的代碼。我就以 ClipVectorWorkspace 為例。


每個工具類應至少包括 __init__execute 方法。此外,還可以選擇使用 getParameterInfoisLicensedupdateParametersupdateMessages 方法向工具的行為中添加其他控制。

工具類中的 __init__ 方法是標準 Python 類初始化方法。對于 Python 工具箱中的工具,__init__ 方法用于設置該工具的屬性,例如工具的標注、描述、是否允許在后臺運行等。

下面的例子就創建了ClipVectorWorkspace這個工具:

class ClipWorkspace(object):def __init__(self):self.label = "Clip Workspace"self.description = "clip spatial data in the same workspace by batch."self.canRunInBackground = True

有了工具的構造函數,我們繼續看如何給工具定義參數。在 Python 工具箱 (.pyt) 中,可在工具類的 getParameterInfo 方法中創建 Parameter 對象,并設置對象的屬性來定義工具參數。Parameter的屬性中datatype
包含的類型可以在幫助文檔中查詢,點這里

此示例中的參數就是輸入工作空間(inWorkspace)、裁剪區域面(ClipArea)、輸出工作空間(outWorkspace)。

def getParameterInfo(self):# Parameter Definitions# First parameter - Input Workspaceparam0 = arcpy.Parameter(displayName="Input Workspace",name="inWorkspace",datatype="DEWorkspace",parameterType="Required",direction="Input")# Second parameter - Clip Areaparam1 = arcpy.Parameter(displayName="Clip Area",name="CLipArea",datatype="DEFeatureClass",parameterType="Required",direction="Input")# Third parameter - Output Workspaceparam2 = arcpy.Parameter(displayName="Output Workspace",name="outWorkspace",datatype="DEWorkspace",parameterType="Required",direction="Input")params = [param0,param1,param2]return params
  • 1

PS : 在代碼中,如果仔細看,或許你會疑惑,為何輸出工作空間的方向是 input ,而不是 output? 因為工具最終輸出的為 Feature Class 或 Raster,輸出工作空間也是作為輸入參數傳入工具使用的。如果不關心,也可以不在意這些細節…… 繼續向下了解工具的構建過程。


下面就是工具的具體執行部分了,當然里面還加了些輔助了解工具執行狀態的消息:

def execute(self, parameters, messages):"""The source code of the tool."""# Get tool parametersinWorkspace = parameters[0].valueAsTextarcpy.AddMessage("###Input Workspace is {0}".format(inWorkspace))ClipArea = parameters[1].valueAsTextarcpy.AddMessage("###Clip Area is {0}".format(ClipArea))outWorkspace = parameters[2].valueAsTextarcpy.AddMessage("###Out Workspace is {0}".format(outWorkspace))# Clip Feature by Batcharcpy.env.workspace = inWorkspace# Clip VectorFeatureClasses = arcpy.ListFeatureClasses()arcpy.AddMessage("Input Workspace contains {0}".format(FeatureClasses))for fc in FeatureClasses:arcpy.AddMessage(">> Clipping {0}".format(fc))arcpy.Clip_analysis(fc,ClipArea, os.path.join(outWorkspace,fc))arcpy.AddMessage("{0} has been clipped.".format(os.path.join(outWorkspace,fc)))# Clip RasterRasters = arcpy.ListRasters()arcpy.AddMessage("Input Workspace contains {0}".format(Rasters))for Raster in Rasters:arcpy.AddMessage(">> Clipping {0}".format(Raster))arcpy.Clip_management(in_raster = Raster,rectangle = "",out_raster = os.path.join(outWorkspace,Raster),in_template_dataset = ClipArea,nodata_value = "",clipping_geometry = "ClippingGeometry",maintain_clipping_extent = "NO_MAINTAIN_EXTENT")arcpy.AddMessage("{0} has been clipped.".format(os.path.join(outWorkspace,Raster)))return


到這里,工具的核心部分已經完成,執行下試試。


OK,應該是預期的結果:


3 完善

我們發現不像標準工具箱中的腳本工具和腳本文件本身是散列存儲的,python工具箱以及其中的所有工具的代碼都在這一個pyt文件中,維護起來便利了不少。如果想在工具箱中繼續添加工具,只要繼續增加一個工具類即可。

經過前兩步的過程,工具已經可以拿去使用。如果為了工具更友好,還可以繼續調整下代碼,以便遇到異常的時候,讓用戶了解更詳細的原因,這里就再往下進行了。工具分享給別人,最后只差要豐富下工具文檔了,同樣在 Python 工具的 Item Description 中編輯。


參考:http://desktop.arcgis.com/zh-cn/arcmap/10.3/analyze/creating-tools/a-template-for-python-toolboxes.htm

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的在ArcGIS中认识 Python工具箱的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。