Transcript Document

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
*
Tools may have same name
if they are in different toolboxes
Example: Clip_analysis
Clip_management
System tool
Model
Script
Example:
Toolboxes, Toolsets, Tools
depend on license
Excel Toolset
Finding tools
Accessing ArcGIS Geoprocessing tools
GUI
Python Window
Models
Scripts
Run in
PythonWindow,
PyScripter/IDE,
or command line
Using ArcGIS tools with Python
To use any tool …
import arcpy
arcpy.<ToolName>_<ToolboxAlias>(args)
ToolName
OR …
from arcpy.ToolboxAlias import as abbrev
ToolboxAlias
Using ArcGIS tools with Python
Open the tool and get Tool Help
Using ArcGIS tools with Python
… and scroll down
to Syntax and
Code samples
Tool Result object
Result
inputCount: Integer
maxSeverity: Integer
messageCount: Integer
outputCount : Integer
resultID: String
status : Integer
cancel() : Void
getInput(index : Integer) : Recordset or String
getMapImageURL({parameter_list}, {height}, {width}, {resolution}) : String
getMessage(index : Integer): String
getMessages({severity : Integer}) : String
getOutput(index : Integer) : Object
getSeverity(index : Integer) : Integer
Creating script tools
To create a script tool, you need
three things:
1.
2.
3.
A script
A toolbox
A precise definition of the
parameters of your script
Create a script
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
From scratch …
Write and debug in PyScripter
Use sys.argv for script parameters/arguments to start
Once script is working, add it to a toolbox …
Using ModelBuilder
Create a model
Export to Python
Edit 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 script
2. A toolbox
3. A precise definition of the parameters of your script
Create a Toolbox
Create a custom toolbox (.tbx)
Or a Python Toolbox (.pyt)
Custom location
Default location
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 script
2. A toolbox
3. 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
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
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 script
2. A toolbox
3. A precise definition of the parameters of your script
Define Toolbox Alias & Description
Alias in Properties window of
PYT is read-only.
Change in PYT file …
Add script tool to Custom Toolbox
TBX
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
Add script tool to Python Toolbox
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
PYT
The tools attribute of the Toolbox contains a list of
“Tools” in the Python Toolbox.
Change the name of the default (Tool) to a new
name.
Add script tool to Python Toolbox
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
The execute method in the Tool class is
run when the tool is executed.
The source code for the script can be
in this function, in a function called by
statements in execute, or in an imported module.
Defining input parameters - TBX
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
Like sys.argv without 1024 byte
limit and only used in script tools
Defining input parameters - PYT
To create a script tool, you need three things:
1. A script
2. A toolbox
3. 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.
The Parameter filter property can be
used to constrain values:
Defining input parameters - PYT
The datatype of a
Parameter is defined by
a datatype keyword.
The complete list is in
ArcGIS help …
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
Value Lists
Value Lists constrain values in scripts …
TBX
PYT
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
Defining output parameters
To create a script tool, you need three things:
1. A script
2. A toolbox
3. A precise definition of the parameters of your script
Tools should have an output esp. when the output could be used as input to another tool in ModelBuilder
TBX
PYT
The output parameter and dependency
on an input parameter is defined in
getParameterInfo.
parameterType = "Derived"
direction = "Output"
paramOut.parameterDependencies = [paramIn.name]
# Set output parameter
#
arcpy.SetParameterAsText(1, outFCName)
Ref: Setting output parameters …
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 messages
Used 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”
from
Python Files (*.py;*.pyw)|*.py;*.pyw
to
Python Files (*.py;*.pyw;*.pyt)|*.py;*.pyw;*.pyt