Python & ModelBuilder. Overview Python/ModelBuilder Concepts – The Geoprocessor – Checking some environment variables – Providing feedback from your model/script.

Slides:



Advertisements
Similar presentations
WITHOUT LANGUAGE [ DEVELOPING GEO-PROCESSING MODELS USING ARCGIS MODELBUILDER 10 ] PROGRAMMING R. RYAN STEVENS / GIS RESEARCH ANALYST / THE POLIS CENTER.
Advertisements

Geoprocessing; Useful Tools You Should Know in ArcToolbox Unlock the hidden secrets of ArcToolbox to discover tools that make your work easier and analysis.
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 Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
Annotation & Nomenclature By Corey Fortezzo for PG&G GIS Workshop, 2010.
GIS Topics and Applications
Geography 465 Working with ArcGIS Tools and Environment Settings.
ModelBuilder In ArcGIS 9.x By Tim Weigel GEOG 407/607 April 3 rd, 2006.
Python and Web Programming
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.
Habitat Analysis in ArcGIS Use of Spatial Analysis to characterize used resources Thomas Bonnot
Advance Model Builder Features. Advance Features Using Lists (also Batching) Iteration Feedback Model Only Tools Inline Variable Substitution Preconditions.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Group practice in problem design and problem solving
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
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.
Working with cursors in Python GISDE Python Workshop Qiao Li.
2006 GIS Jam: ArcGIS Python Scripting
Introduction to Spatial Analysis and Spatial Modeling
Introduction to Python John Reiser May 5 th, 2010.
Introduction to ArcPy. Topics What is ArcPy? Accessing geoprocessing tools using ArcPy Writing scripts using ArcPy.
Python: An Introduction
Martin Dodge Practical 2, 24th March 2004, pm Social Science Research Methodologies.
Introduction of Geoprocessing Topic 7a 4/10/2007.
Python, Toolboxes, Tools & Script Tools
Exploring ArcToolbox Presented by: Isaac Johnson.
Linux Operations and Administration
Copyright © 2006 by Maribeth H. Price 8-1 Chapter 8 Geoprocessing.
Built-in Data Structures in Python An Introduction.
Module 6: Geoprocessing Scripts. Processing loops and decisions.
Esri UC 2014 | Technical Workshop | Creating Geoprocessing Services Kevin Hibma.
Introduction of Geoprocessing Lecture 9. Geoprocessing  Geoprocessing is any GIS operation used to manipulate data. A typical geoprocessing operation.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Trinity College Dublin, The University of Dublin GE3M25: Computer Programming for Biologists Python Karsten Hokamp, PhD Genetics TCD, 03/11/2015.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Juanita Cano City of Sacramento Spring 2014 Geography 375.
NSF DUE ; Wen M. Andrews J. Sargeant Reynolds Community College Richmond, Virginia.
Introduction to Geographic Information Systems Fall 2013 (INF 385T-28620) Dr. David Arctur Research Fellow, Adjunct Faculty University of Texas at Austin.
Introduction of Geoprocessing Lecture 9 3/24/2008.
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.
Technical Workshops | Esri International User Conference San Diego, California Branching and Iteration in ModelBuilder Shitij Mehta July 24, :15.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Outline of Script Import Modules Setup Workspace Environment and Assign Data Path Variables Summary of Script Title and Author Info.
Development Environment
Geography 385 Introduction to ArcGIS Web Application Design
PYTHON: AN INTRODUCTION
Environmental GIS Nicholas A. Procopio, Ph.D, GISP
Introduction to Scripting
Functions CIS 40 – Introduction to Programming in Python
Tan Hoang GEOG 362 – Final Project
Attribute Extraction.
String several geoprocessing processes
Creating Geoprocessing Services
User Defined Functions
Using Model Builder in ArcGIS
Lecture 9 Using Python for Geoprocessing
Topics Introduction to File Input and Output
Coding Concepts (Basics)
JavaScript: Introduction to Scripting
PYTHON: BUILDING GEOPROCESSING TOOLS
Topics Introduction to File Input and Output
Clip & Convert to ASCII Program Kelly Knapp Spring 2010
Presentation transcript:

Python & ModelBuilder

Overview Python/ModelBuilder Concepts – The Geoprocessor – Checking some environment variables – Providing feedback from your model/script – Starting a script with a model – Bringing a completed script into ArcToolbox Exercises

Why model or write scripts? Automate workflows Test hypotheses Reproducibility Extend Functionality Simplification Quality Control

The Geoprocessing Object Also known as the "geoprocessor" Has many methods Manages layers and selections Provides access to all of ArcGIS's tools Provides status of ArcGIS licensing Handles errors and warning messages

The Geoprocessing Object gp.Clip_analysis("Roads_2007", "Counties", "Mercer_Roads.shp")

Check Data Existence gp.exists() returns a boolean depending on whether or not the feature class exists Check most user-submitted data for existence – if gp.exists(data): # continue working else: # warn user about missing data ArcToolbox will check, but scripts run from the command line will not

Defining a Workspace When the geoprocessor has a workspace defined, all processing results will be stored in that folder, geodatabase, or SDE gdb gp.workspace = "C:\\Student\\" Once defined, we do not need to specify absolute paths to datasets or outputs located in that workspace

Describe Method gp.describe returns an object with numerous properties based on the type of data passednumerous properties – Feature Classes (shapefiles, geodatabase layers) – Feature Datasets – Workspaces – Raster Data gp.describe provides you with GIS-specific information about your data

Using the Describe Method Use Describe to ensure that data passed to your model fits certain criteria – Prevent line or point features being used as Clip Features – Add an area and perimeter field to shapefiles if not present – Convert rasters to 8-bit unsigned if not already – Use Python to get a list of.shp files, then process only the polygon shapefiles

Check License Availability Models and scripts might be dependent on the level of ArcGIS available The gp.CheckProduct("level") returns "Available" or "NotLicensed" depending on the availability of the product level passed The gp.SetProduct("level") tells the geoprocessor to use that specific level

Handling Input ArcGIS passes information to your Python script through sys.argv For example, a clip script would need to listen for three inputs – Input Feature / sys.argv[1] – Clip Feature / sys.argv[2] – Output Feature / sys.argv[3] Gets tricky when we ask for multiple inputs

Providing Feedback Geoprocessor methods to provide feedback – gp.AddMessage(message string) – gp.AddWarning(message string) – gp.AddError(message string) Adds messages to the message list Message list is simply that – stores all the errors, warnings and messages generated by the script

Providing Feedback gp.GetMessage() and gp.MessageCount GetMessage(x) returns message number x in the list print gp.GetMessage(gp.MessageCount - 1) – prints the last message generated Print messages at the end of script or when an error is caught Print before and after a lengthy processing step

Defining a Function A Python Function allows you to write a block of code that accepts arguments and returns results Functions allow you to write something once and reuse it as needed Helps debugging Keeps code clear and concise Easier to program

Using Functions We use the def statement to start a block of code that will only be run when our function is called We want our functions to return values we can use in our geoprocessing The last statement in our function should be return.

Basic Function A simple function that adds 1 to value passed def addone(x): return x + 1 print addone(2) >>> 3 var = addone(3) print var >>> 4

Multiple Clips Like Exercise #2 from last week, we will provide two lists to a script – List of Input Features – List of Clip Features We also need to tell the script where to place the output, using gp.workspace()

Start in ModelBuilder

Export to Python

Our First Function We need a custom function that will take a string of text, split it into pieces and return those pieces as a list SplitMultiInputs will clean up a line of code and return it as a list

Adding SplitMultiFeatures Add the function and split the input string and the clippers string passed by the Toolbox to our script

Two Loops We will need another loop for each of the inputs The "inputs" loop contains the "clippers" loop The loops are structured like so: for input in inputs: (runs m times) for clip in clippers: (runs n times) The script will then perform mn clips

Completed Script

Back to the Toolbox Once our script is complete, we can test it in IDLE and on the command line What we'd really want is to run our script like any other tool in the Toolbox We can add the script back to the Toolbox We need to specify how to talk to the script

Add Your Script Open ArcToolbox Right click on the Toolbox you want your script to be located Add > Script…

Add Your Script Our model takes two input parameters – Input Feature Classes – Clipping Feature Classes – Output Workspace Note that parameters can be set for multiple values MultiValue = Yes

County Clipper We’ll take a small ModelBuilder model that clips one input using the statewide Counties layer Using Python, we’ll enhance the model to clip the input 21 times for each of the separate counties Add functionality to clip multiple inputs by each county

Setting Up Our Model We need a County layer as our clip feature, and a user-defined layer as our input feature Make Feature Layer Select By Attributes Clip (of course) User-defined Output Workspace

Basic Model to Python Script loop just a portion of the model

Exported Python Script

Adding County Tuple We need a static list of County names to pass as part of the Select by Attributes “where” clause Put all the County names into a tuple Use a for loop to loop over the Select by Attributes and Clip steps with different Counties each time –for county in counties

Looping Our Model loop just a portion of the model

Looping Our Model For each county in the model – prepare a SQL statement to select County – run the selection to create our clipping features – use selected features to clip – create 21 output feature classes – each output layer named input_county

SQL Statement SQL statements use single and double quotes to delimit field and value names Python also uses quotes to delimit strings Keep track of which quotes are being used Escape quotes using backslash (\') or (\") SQL: "COUNTY" = 'MERCER' Python: "\"COUNTY\" = 'MERCER'"

Looping Over SQL Expressions Each time the loop is run, we want to change the SQL statement to select the current county in the loop We can concatenate the parts of the SQL statement with the county name Example:

Changing the Output Feature Each output feature name needs to be unique Append the current County name Wetlands.shp becomes – Wetlands_Atlantic.shp – Wetlands_Bergen.shp – … Some string methods are needed

Changing the Output Feature

Finalizing Our Loop Input and Output County Layer Our For Loop Modified SQL Statement Create Output Path Geoprocessing Tasks

USGS Quad Boundaries Clipped

Remember Multiple Inputs How will we modify the script to accept multiple inputs? We'll need another for loop in the script Set the Input parameter to accept multiple values MultiValue = Yes

Clip To Counties Tool If the parameters are set correctly, the dialog box will look similar to the one at right Our CountyClipper now ready to handle multiple layers, clipping each by county