Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python, Toolboxes, Tools & Script Tools

Similar presentations


Presentation on theme: "Python, Toolboxes, Tools & Script Tools"— Presentation transcript:

1 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

2 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 Toolboxes, Toolsets, Tools depend on license Example: Excel Toolset Script GIS4201 Week 3 2

3 Finding tools

4 Accessing ArcGIS Geoprocessing tools
GUI Python Window Models Scripts Run in PythonWindow, PyScripter/IDE, or command line GIS4201 Week 3 4

5 Using ArcGIS tools with Python
To use any tool … import arcpy arcpy.<ToolName>_<ToolboxAlias>(args) OR … from arcpy.ToolboxAlias import as abbrev ToolName ToolboxAlias

6 Using ArcGIS tools with Python
Open the tool and get Tool Help

7 Using ArcGIS tools with Python
… and scroll down to Syntax and Code samples

8 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

9 Creating script tools To create a script tool, you need three things:
A toolbox A precise definition of the parameters of your script

10 Create a script From scratch … Using ModelBuilder
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Create a 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)

11 Create a Toolbox Create a custom toolbox (.tbx)
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Create a Toolbox Custom location Create a custom toolbox (.tbx) Or a Python Toolbox (.pyt) Default location C:\Users\<username>\AppData\Roaming\ESRI\Desktop10.2\ArcToolbox\My Toolboxes

12 To create a script tool, you need three things:
A toolbox A precise definition of the parameters of your script Custom or Python? 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.

13 Creating Custom/Python Toolbox
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script 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) Add script to .pyt (“embedded” in .pyt) Define parameters by editing functions within .pyt

14 Define Toolbox Alias & Description
To create a script tool, you need three things: A script A toolbox 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 …

15 Add script tool to Custom Toolbox
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Add script tool to Custom Toolbox TBX

16 Add script tool to Python Toolbox
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Add script tool to Python Toolbox 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.

17 Add script tool to Python Toolbox
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Add script tool to Python Toolbox 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.

18 Defining input parameters - TBX
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Defining input parameters - TBX Like sys.argv without 1024 byte limit and only used in script tools

19 Defining input parameters - PYT
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Defining input parameters - PYT 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:

20 Defining input parameters - PYT
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Defining input parameters - PYT The datatype of a Parameter is defined by a datatype keyword. The complete list is in ArcGIS help …

21 Value Lists Value Lists constrain values in scripts … TBX PYT
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Value Lists Value Lists constrain values in scripts … TBX PYT

22 Defining output parameters
To create a script tool, you need three things: A script A toolbox A precise definition of the parameters of your script Defining output parameters 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

23 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 …

24 Tool Messages Refer to Writing messages in script tools in help.arcgis.com

25 Parameter messages Used for warnings or errors when parameter values change (before tool is executed)

26 Error trapping GetMessages(2)  Messages with severity of 2 = Error Messages

27 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


Download ppt "Python, Toolboxes, Tools & Script Tools"

Similar presentations


Ads by Google