Skip to content

Instantly share code, notes, and snippets.

@jontwo
Created January 12, 2017 11:32
Show Gist options
  • Save jontwo/1a25f0fc70a5bb55741392a4d40882c5 to your computer and use it in GitHub Desktop.
Save jontwo/1a25f0fc70a5bb55741392a4d40882c5 to your computer and use it in GitHub Desktop.
Generic python toolbox template for ArcGIS
# ----------------------------------------------------------------------------
# Name: My tool
# Purpose: TODO
#
# Author: Jon Morris
#
# Created: 2017
# Copyright: MY COMPANY 2017
# Licence: ArcGIS for Desktop Advanced
# ----------------------------------------------------------------------------
import arcpy
class LicenseError(Exception):
pass
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "My toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [MyTool]
class MyTool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "My tool"
self.description = "TODO"
self.canRunInBackground = True
arcpy.env.overwriteOutput = True
# arcpy.CheckOutExtension("Spatial")
def getParameterInfo(self):
"""Define parameter definitions"""
# # First parameter
# param0 = arcpy.Parameter(
# displayName="Input Param 1",
# name="in_param_1",
# datatype="GPString",
# parameterType="Required",
# direction="Input")
# # Second parameter
# param1 = arcpy.Parameter(
# displayName="Output Param 1",
# name="out_param_1",
# datatype="GPString",
# parameterType="Optional",
# direction="Output")
# param0.value = "default value"
# params = [param0, param1]
params = None
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
# try:
# if arcpy.CheckExtension("Spatial") != "Available":
# raise LicenseError
# except LicenseError:
# return False # tool cannot be executed
return True # tool can be executed
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."""
try:
OUTWorkspace = arcpy.env.scratchGDB
#MyParam = parameters[0].value
arcpy.env.workspace = OUTWorkspace
arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)
arcpy.AddWarning('Generic warning')
#parameters[1].value = MyParam
# Refresh Catalog
arcpy.RefreshCatalog(OUTWorkspace)
except Exception, e:
arcpy.AddError(e.message)
# finally:
# arcpy.CheckInExtension("Spatial")
return
@jontwo
Copy link
Author

jontwo commented Jan 12, 2017

Just update the tool name and doc, uncomment license/param details and add your script to the execute method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment