Python, Toolboxes, Tools & Script Tools

Slides:



Advertisements
Similar presentations
Chapter 7 JavaScript: Introduction to Scripting. Outline Simple Programs Objects and Variables Obtaining User Input with prompt Dialogs – –Dynamic Welcome.
Advertisements

Tutorial 12: Enhancing Excel with Visual Basic for Applications
Environmental GIS Nicholas A. Procopio, Ph.D, GISP Some slides from Lyna Wiggins (Rutgers University)
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
The Web Warrior Guide to Web Design Technologies
Introduction to ArcPython Dan Mahr ‘11 10/28/2014 GEOL1320.
Introduction to Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
Programming Basics Aims of Programming: –The aim of programming is to write programs to accomplish complex tasks Programming method: –functional decompositional.
GIS Topics and Applications
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Python & ModelBuilder. Overview Python/ModelBuilder Concepts – The Geoprocessor – Checking some environment variables – Providing feedback from your model/script.
ModelBuilder In ArcGIS 9.x By Tim Weigel GEOG 407/607 April 3 rd, 2006.
Spatial Analysis, Geoprocessing,
Geography 465 Overview Geoprocessing in ArcGIS. MODELING Geoprocessing as modeling.
What Geoprocessing? Geoprocessing is the processing of geographic information. Commonly used to describe a process when geographic objects are manipulated.
ModelBuilder at ArcGIS 9.2 Lyna Wiggins Rutgers University May 2008.
Advance Model Builder Features. Advance Features Using Lists (also Batching) Iteration Feedback Model Only Tools Inline Variable Substitution Preconditions.
Arc: Programming Options Dr Andy Evans. Programming ArcGIS ArcGIS: Most popular commercial GIS. Out of the box functionality good, but occasionally: You.
Python & ModelBuilder. Continuing Education Python and ModelBuilder Overview Python/ModelBuilder Concepts –The Geoprocessor –Checking some environment.
Esri International User Conference | San Diego, CA Technical Workshops | Python – Getting Started Drew Flater, Ghislain Prince July 12 - July 14, 2011.
Introduction to InVEST ArcGIS Tool Nasser Olwero GMP, Bangkok April
The NetBeans IDE CSIS 3701: Advanced Object Oriented Programming.
Microsoft Excel 2007 © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line.
Introduction to Spatial Analysis and Spatial Modeling
Introduction to Python John Reiser May 5 th, 2010.
Network Analysis with Python
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 1B Introduction (Tutorial)
Introduction to ArcPy. Topics What is ArcPy? Accessing geoprocessing tools using ArcPy Writing scripts using ArcPy.
Python: An Introduction
Functions Reading/writing files Catching exceptions
Introduction to Applets CS 3505 Client Side Scripting with applets.
ArcGIS Network Analyst: Automating Workflows with Geoprocessing
Esri UC2013. Technical Workshop. Technical Workshop 2013 Esri International User Conference July 8–12, 2013 | San Diego, California ModelBuilder – Getting.
Programming for Geographical Information Analysis: Advanced Skills Lecture 1: Introduction Programming Arc Dr Andy Evans.
ATN GIS Support ArcGIS: ArcToolbox.
Module2: Using Dialog Tools. What is the mean distance to the nearest wetland vegtype of 2 or 8 or 15 or 18?
Introduction of Geoprocessing Topic 7a 4/10/2007.
ArcGIS: ArcToolbox. Goals Develop familiarity with ArcToolbox Integrated use of the different ArcGIS components in the context of a typical GIS project.
Introduction to Model Builder Mike Sweeney ESRI-Charlotte.
ArcGIS Pro: A Quick Tour of Python David Wynne.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Converting workflows from ArcSDE Command line in ArcGIS 10.3.x
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Introduction to ArcGIS for Environmental Scientists Module 3 – GIS Analysis Model Builder.
Introduction of Geoprocessing Lecture 9. Geoprocessing  Geoprocessing is any GIS operation used to manipulate data. A typical geoprocessing operation.
Juanita Cano City of Sacramento Spring 2014 Geography 375.
Esri UC 2014 | Technical Workshop | Python: Beyond the Basics David Wynne, Jon Bodamer.
Introduction of Geoprocessing Lecture 9 3/24/2008.
Aggregator  Performs aggregate calculations  Components of the Aggregator Transformation Aggregate expression Group by port Sorted Input option Aggregate.
Python: Building Geoprocessing Tools David Wynne, Ghislain Prince.
William Perry U.S. Geological Survey Western Ecological Research Center Geography 375 Final Project May 22, 2013.
Computer Science 112 Fundamentals of Programming II Data Fields for I/O and Message Boxes for Error Recovery.
Introduction to InVEST ArcGIS Tool
PYTHON: AN INTRODUCTION
Environmental GIS Nicholas A. Procopio, Ph.D, GISP
1. Introduction to Visual Basic
Microsoft Office Illustrated
Computer Science 112 Fundamentals of Programming II GUIs II:
String several geoprocessing processes
Programming and Automation
Using Model Builder in ArcGIS
Programming for Geographical Information Analysis: Advanced Skills
CIS16 Application Development Programming with Visual Basic
Programming for Geographical Information Analysis: Advanced Skills
This lecture Introduction to arcpy Debugging Using arcpy.
Network Analysis using Python
PYTHON: BUILDING GEOPROCESSING TOOLS
Network Analyst – Automating Workflows with Geoprocessing
Programming Arc.
Variables, Constants, Assign.
Presentation 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 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

Finding tools

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

Using ArcGIS tools with Python To use any tool … import arcpy arcpy.<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 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: A toolbox A precise definition of the parameters of your script

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)

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

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.

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

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 …

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

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.

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.

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

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:

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 …

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

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

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