python, toolboxes, tools & script tools accessing arcgis geoprocessing tools with python...
TRANSCRIPT
Python, Toolboxes, Tools & Script Tools
Accessing ArcGIS Geoprocessing tools with Python
Creating a toolbox
Adding script tools to a custom (TBX) or Python (PYT) toolbox
Tool and parameter messages
Error trapping
ArcToolbox & Geoprocessing
ArcToolbox Toolbox
Tool Toolset
Model
System tool
Script
* *
*
Tools may have same nameif they are in different toolboxesExample: Clip_analysis Clip_management
Toolboxes, Toolsets, Toolsdepend on license
Example:
Excel Toolset
Finding tools
Accessing ArcGIS Geoprocessing tools
GUI
Python Window
Models
ScriptsRun in PythonWindow,PyScripter/IDE,or command line
Using ArcGIS tools with Python
To use any tool …
import arcpyarcpy.<ToolName>_<ToolboxAlias>(args)
OR …
from arcpy.ToolboxAlias import as abbrev
ToolName
ToolboxAlias
Using ArcGIS tools with Python
Open the tool and get Tool Help
Using ArcGIS tools with Python
… and scroll downto Syntax andCode samples
Tool Result object
ResultinputCount: IntegermaxSeverity: IntegermessageCount: IntegeroutputCount : IntegerresultID: Stringstatus : Integer
cancel() : VoidgetInput(index : Integer) : Recordset or StringgetMapImageURL({parameter_list}, {height}, {width},
{resolution}) : StringgetMessage(index : Integer): StringgetMessages({severity : Integer}) : StringgetOutput(index : Integer) : ObjectgetSeverity(index : Integer) : Integer
Creating script tools
To create a script tool, you need three things:
1. A script2. A toolbox3. A precise definition of the
parameters of your script
Create a script
From scratch …Write and debug in PyScripterUse sys.argv for script parameters/arguments to startOnce script is working, add it to a toolbox …
Using ModelBuilderCreate a modelExport to PythonEdit in PyScripter (might not work in ModelBuilder after edit)
Once script is working, add it to a toolbox …
To create a script tool, you need three things:1. A script2. A toolbox3. A precise definition of the parameters of your script
Create a Toolbox
Create a custom toolbox (.tbx)Or a Python Toolbox (.pyt)
Default location
Custom location
To create a script tool, you need three things:1. A script2. A toolbox3. A precise definition of the parameters of your script
C:\Users\<username>\AppData\Roaming\ESRI\Desktop10.2\ArcToolbox\My Toolboxes
Custom or Python?
To create a script tool, you need three things:1. A script2. A toolbox3. A precise definition of the parameters of your script
If you use or are planning to use significant validation code in your script tool, almost certainly you will find the experience more straightforward in a Python toolbox.
Creating Custom/Python Toolbox
Custom (.tbx)Define Toolbox Alias, Description, Help, etc.Add script to Toolbox (“separate” from .tbx)Define parameters (Input/Output, Type, etc.) using wizard
Python (.pyt)Define Toolbox Alias, Description, Help, etc.Add script to .pyt (“embedded” in .pyt)Define parameters by editing functions within .pyt
To create a script tool, you need three things:1. A script2. A toolbox3. A precise definition of the parameters of your script
Define Toolbox Alias & DescriptionTo create a script tool, you need three things:
1. A script2. A toolbox3. A precise definition of the parameters of your script
Alias in Properties window of PYT is read-only.Change in PYT file …
Add script tool to Custom ToolboxTo create a script tool, you need three things:
1. A script2. A toolbox3. A precise definition of the parameters of your script
TBX
Add script tool to Python ToolboxTo create a script tool, you need three things:
1. A script2. A toolbox3. A precise definition of the parameters of your script
The tools attribute of the Toolbox contains a list of “Tools” in the Python Toolbox.
Change the name of the default (Tool) to a newname.
PYT
Add script tool to Python ToolboxTo create a script tool, you need three things:
1. A script2. A toolbox3. A precise definition of the parameters of your script
The execute method in the Tool class isrun when the tool is executed.
The source code for the script can bein this function, in a function called bystatements in execute, or in an imported module.
Defining input parameters - TBX
Like sys.argv without 1024 bytelimit and only used in script tools
To create a script tool, you need three things:1. A script2. A toolbox3. A precise definition of the parameters of your script
The list of parameters is defined in the getParameterInfo method of the tool.
Create a parameter using the arcpy.Parameter class
Change params from None to a list(or not). As long as the function returns a list, the tool will work.
Defining input parameters - PYTTo create a script tool, you need three things:
1. A script2. A toolbox3. A precise definition of the parameters of your script
The Parameter filter property can be used to constrain values:
The datatype of a Parameter is defined bya datatype keyword.
The complete list is in ArcGIS help …
Defining input parameters - PYTTo create a script tool, you need three things:
1. A script2. A toolbox3. A precise definition of the parameters of your script
Value ListsValue Lists constrain values in scripts …
To create a script tool, you need three things:1. A script2. A toolbox3. A precise definition of the parameters of your script
TBX PYT
Defining output parameters
Ref: Setting output parameters …
# Set output parameter # arcpy.SetParameterAsText(1, outFCName)
To create a script tool, you need three things:1. A script2. A toolbox3. A precise definition of the parameters of your script
TBX PYTTools should have an output esp. when the output could be used as input to another tool in ModelBuilder
The output parameter and dependencyon an input parameter is defined in getParameterInfo.parameterType = "Derived"direction = "Output"paramOut.parameterDependencies = [paramIn.name]
Ref: Defining parameters in Python Toolbox
Using script tool parameter values in the script
Custom Toolbox (TBX)In script associated with script tool (separate or embedded in TBX)
Python Toolbox (PYT)In PYT file, class definition associated with the script tool …
Tool Messages
Refer to Writing messages in script tools in help.arcgis.com
Parameter messagesUsed for warnings or errors when parameter values change (before tool is executed)
Error trapping
GetMessages(2) Messages with severity of 2 = Error Messages
Enable PYT editing in PyScripter
Change “Open dialog Python filter” fromPython Files (*.py;*.pyw)|*.py;*.pyw
toPython Files (*.py;*.pyw;*.pyt)|*.py;*.pyw;*.pyt