Last active
December 4, 2020 23:18
-
-
Save mfunk/112c8fc5cf1035b41277 to your computer and use it in GitHub Desktop.
Python Toolbox (PYT) Template for ArcGIS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
''' | |
------------------------------------------------------------------------------ | |
Copyright 2016 Esri | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
------------------------------------------------------------------------------ | |
================================================== | |
.pyt | |
-------------------------------------------------- | |
requirements: ArcGIS X.X, Python 2.7 or Python 3.4 | |
author: ArcGIS Solutions | |
contact: ArcGISTeam<Solution>@esri.com | |
company: Esri | |
================================================== | |
description: <Description> | |
================================================== | |
history: | |
<date> - <initals> - <modifications> | |
================================================== | |
''' | |
# IMPORTS ========================================== | |
import os | |
import sys | |
import traceback | |
import arcpy | |
from arcpy import env | |
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] | |
# <tool> =========================================== | |
class Tool(object): | |
""" Tool class docstring """ | |
def __init__(self): | |
"""Define the tool (tool name is the name of the class).""" | |
self.label = "Tool" | |
self.description = "" | |
self.canRunInBackground = False | |
def getParameterInfo(self): | |
"""Define parameter definitions""" | |
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.""" | |
deleteme = [] # intermediate datasets to be deleted | |
debug = True # extra messaging during development | |
try: | |
# get/set environment | |
env.overwriteOutput = True | |
# Add execution code | |
# Set output | |
except arcpy.ExecuteError: | |
# Get the tool error messages | |
msgs = arcpy.GetMessages() | |
arcpy.AddError(msgs) | |
print(msgs) | |
except: | |
# Get the traceback object | |
tb = sys.exc_info()[2] | |
tbinfo = traceback.format_tb(tb)[0] | |
# Concatenate information together concerning the error into a message string | |
pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1]) | |
msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages() + "\n" | |
# Return python error messages for use in script tool or Python Window | |
arcpy.AddError(pymsg) | |
arcpy.AddError(msgs) | |
# Print Python error messages for use in Python / Python Window | |
print(pymsg + "\n") | |
print(msgs) | |
finally: | |
if debug is False and len(deleteme) > 0: | |
# cleanup intermediate datasets | |
if debug is True: arcpy.AddMessage("Removing intermediate datasets...") | |
for i in deleteme: | |
if debug is True: arcpy.AddMessage("Removing: " + str(i)) | |
arcpy.Delete_management(i) | |
if debug is True: arcpy.AddMessage("Done") | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment