Last active
January 9, 2018 22:05
-
-
Save nmpeterson/7dbf3cb13af2a9ff1ad9544089a964e1 to your computer and use it in GitHub Desktop.
ArcGIS Python Toolbox (.pyt) Template
This file contains hidden or 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
#!/usr/bin/env python | |
''' | |
python_toolbox_name.pyt | |
Author: username | |
Revised: mm/dd/yyyy | |
--------------------------------------------------------------------------- | |
Python toolbox (.pyt) description and special instructions. | |
''' | |
import os | |
import sys | |
import arcpy | |
def make_param(disp_name, p_name, data_type, default_val=None, p_type='Required', p_dir='Input'): | |
''' Global function to create new parameters for a tool. Avoid a lot of | |
repetitive code with this. ''' | |
param = arcpy.Parameter( | |
displayName=disp_name, | |
name=p_name, | |
datatype=data_type, | |
parameterType=p_type, | |
direction=p_dir | |
) | |
param.value = default_val | |
return param | |
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 = 'Tool' | |
self.description = '' | |
self.category = '' # Optionally place tool in a named toolset within toolbox | |
self.canRunInBackground = False | |
self.parameters = [ | |
# make_param('Output Geodatabase', 'out_gdb', 'DEWorkspace', r'C:\WorkSpace\Temp\temp.gdb'), | |
# make_param('Input Layer', 'in_lyr', 'GPFeatureLayer'), | |
# make_param('Input Field', 'in_field', 'Field'), | |
# ... | |
] | |
def getParameterInfo(self): | |
''' Return parameter list defined in tool's __init__ method. Can set | |
any additional proprieties, such as filters, for parameters here. | |
Just reference them using their index in the parameter list. ''' | |
# self.parameters[0].filter.list = ['Local Database'] | |
# self.parameters[2].parameterDependencies = [self.parameters[1].name] | |
# ... | |
return self.parameters | |
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. ''' | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment