import arcpy
import os
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 = "驗標出圖工具"
self.description = "驗標出圖工具"
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
# 定義工具面板上的參數形式
param0 = arcpy.Parameter(displayName=u"地圖模版",
name="mxd_path",
datatype="DEMapDocument",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(displayName=u"地塊Shp文件",
name="dk_layer",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param2 = arcpy.Parameter(displayName=u"農戶點Shp",
name="nhd_layer",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param3 = arcpy.Parameter(displayName=u"年份",
name="nf",
datatype="GPString",
parameterType="Required",
direction="Input")
param3.filter.type = "ValueList"
param3.filter.list = [
'2018', '2019', '2020', '2021', '2022', '2023', '2024'
]
param4 = arcpy.Parameter(displayName=u"結果輸出目錄",
name="dst_dir",
datatype="DEFolder",
parameterType="Required",
direction="Input")
params = [param0, param1, param2, param3, param4]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
# 取參數
arcpy.env.workspace = r"in_memory"
arcpy.env.scratchWorkspace = r"in_memory"
arcpy.env.overwriteOutput = True
mxd_path = parameters[0].valueAsText
dk_layer_path = parameters[1].valueAsText
nhd_layer_path = parameters[2].valueAsText
nf = parameters[3].valueAsText
dst_dir = parameters[4].valueAsText
# 執行邏輯
arcpy.AddMessage("all success")
arcpy.AddMessage(mxd_path)
arcpy.AddMessage(nf)
arcpy.AddMessage(dst_dir)
except arcpy.ExecuteError:
arcpy.AddError(messages)
except Exception:
arcpy.AddError(messages)
return