27
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

Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Embed Size (px)

Citation preview

Page 1: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 2: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 3: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Finding tools

Page 4: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Accessing ArcGIS Geoprocessing tools

GUI

Python Window

Models

ScriptsRun in PythonWindow,PyScripter/IDE,or command line

Page 5: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Using ArcGIS tools with Python

To use any tool …

import arcpyarcpy.<ToolName>_<ToolboxAlias>(args)

OR …

from arcpy.ToolboxAlias import as abbrev

ToolName

ToolboxAlias

Page 6: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Using ArcGIS tools with Python

Open the tool and get Tool Help

Page 7: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Using ArcGIS tools with Python

… and scroll downto Syntax andCode samples

Page 8: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 9: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 10: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 11: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 12: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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.

Page 13: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 14: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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 …

Page 15: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 16: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 17: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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.

Page 18: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 19: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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:

Page 20: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 21: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 22: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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

Page 23: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

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 …

Page 24: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Tool Messages

Refer to Writing messages in script tools in help.arcgis.com

Page 25: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Parameter messagesUsed for warnings or errors when parameter values change (before tool is executed)

Page 26: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Error trapping

GetMessages(2) Messages with severity of 2 = Error Messages

Page 27: Python, Toolboxes, Tools & Script Tools Accessing ArcGIS Geoprocessing tools with Python Creating a toolbox Adding script tools to a custom (TBX) or Python

Enable PYT editing in PyScripter

Change “Open dialog Python filter” fromPython Files (*.py;*.pyw)|*.py;*.pyw

toPython Files (*.py;*.pyw;*.pyt)|*.py;*.pyw;*.pyt