This lecture Introduction to arcpy Debugging Using arcpy.

Slides:



Advertisements
Similar presentations
Intro to WinHex CSC 414.
Advertisements

Geo GIS Practicuum Introduction to ArcGIS 8 Exercise 5 - ESRI Virtual Campus Chapters 1-2, ArcGIS Methods …
Environmental GIS Nicholas A. Procopio, Ph.D, GISP Some slides from Lyna Wiggins (Rutgers University)
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.
ModelBuilder at ArcGIS 9.2 Lyna Wiggins Rutgers University May 2008.
arcpy.Describe and arcpy.List*
Command Console Tutorial BCIS 3680 Enterprise Programming.
Introduction to ArcGIS for Environmental Scientists Module 3 – GIS Analysis ArcGIS Toolbox.
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.
Lecture 6 – Form processing (Part 1) SFDV3011 – Advanced Web Development 1.
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.
Network Analysis with Python
Introduction to ArcPy. Topics What is ArcPy? Accessing geoprocessing tools using ArcPy Writing scripts using ArcPy.
Python: An Introduction
DTIAtlasBuilder Adrien Kaiser Neuro Image Research and Analysis Laboratories University of North Carolina at Chapel Hill A tool to create an atlas from.
Functions Reading/writing files Catching exceptions
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Programming for Geographical Information Analysis: Advanced Skills Lecture 1: Introduction Programming Arc Dr Andy Evans.
What’s New in SEER-H 7.3 The Galorath Team. New Features Currency and Exchange Rate (with Multi-Currency option) Addin Enhancement Local Quantity, Schedule.
Introduction of Geoprocessing Topic 7a 4/10/2007.
Python, Toolboxes, Tools & Script Tools
Technical Workshops | Esri International User Conference San Diego, California Creating Geoprocessing Services Kevin Hibma, Scott Murray July 25, 2012.
Chapter 10 Chapter 10: Managing the Distributed File System, Disk Quotas, and Software Installation.
Introduction of Geoprocessing Lecture 9. Geoprocessing  Geoprocessing is any GIS operation used to manipulate data. A typical geoprocessing operation.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Welcome! Welcome! Agenda - Wednesday  Introduction  Installation Tips  New Client Features  New Application Design Features  Installing Chart Director.
Juanita Cano City of Sacramento Spring 2014 Geography 375.
Introduction of Geoprocessing Lecture 9 3/24/2008.
Python: Building Geoprocessing Tools David Wynne, Ghislain Prince.
WS16-1 ADM740, Workshop 16, June 2007 Copyright  2007 MSC.Software Corporation WORKSHOP 16 Using Adams/Linear with Adams/Car.
Lecture 10: Geoprocessing with Python (II) Dr. Taysir Hassan Abdel Hamid Associate Professor, Information Systems Dept., Faculty of Computers and Information.
Active-HDL Server Farm Course 11. All materials updated on: September 30, 2004 Outline 1.Introduction 2.Advantages 3.Requirements 4.Installation 5.Architecture.
Appendix A 12.0 Workbench Environment
Dive Into® Visual Basic 2010 Express
Appendix A Barb Ericson Georgia Institute of Technology May 2006
How to link a test to a launcher (in this case a shell launcher)
Geography 385 Introduction to ArcGIS Web Application Design
Data Virtualization Tutorial… CORS and CIS
Environmental GIS Nicholas A. Procopio, Ph.D, GISP
Software Testing With Testopia
String several geoprocessing processes
Introduction to ArcGIS
Programming and Automation
User Defined Functions
Creating Geoprocessing Services
Programming for Geographical Information Analysis: Advanced Skills
NORMA Lab. 7 Generating Reports More Display Options
Topics Introduction to File Input and Output
(Chapter 2) John Carelli, Instructor Kutztown University
Interactive I/O Input from keyboard Must prompt user User friendly
Part B – Structured Exception Handling
Addins Dr Andy Evans Welcome to the course. You’ll find extra information in these note sections below each slide.
Electronic Field Study Advanced User Training
Addins Dr Andy Evans Welcome to the course. You’ll find extra information in these note sections below each slide.
This lecture Introduction to arcpy Debugging Using arcpy.
Arcpy Dr Andy Evans Welcome to the course. You’ll find extra information in these note sections below each slide.
Programming for Geographical Information Analysis: Advanced Skills
Introduction to javadoc
CodePainter Revolution Trainer Course
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Best Practice for Geoprocessing Service
Programming Arc.
Topics Introduction to File Input and Output
Lab 8: GUI testing Software Testing LTAT
Clip & Convert to ASCII Program Kelly Knapp Spring 2010
Workshop for Programming And Systems Management Teachers
Presentation transcript:

This lecture Introduction to arcpy Debugging Using arcpy

Arc env variables arcpy.env.workspace = "c:/data/myGeodatabase.gdb" arcpy.env contains a set of variables that control overall behaviour in Arc. arcpy.env.workspace = "c:/data/myGeodatabase.gdb" From then on, this is default location for output. List all: environments = arcpy.ListEnvironments() for environment in environments: env_value = getattr(arcpy.env, environment) Reset all env settings: arcpy.ResetEnvironments() Reset an env setting: arcpy.ClearEnvironment("workspace") http://desktop.arcgis.com/en/arcmap/latest/tools/environments/output-extent.htm

Scratch space arcpy.env.scratchGDB and scratchFolder used for temp files. Set through the scratchWorkspace: arcpy.env.scratchWorkspace = 'c:/LandUse/scratch.gdb' arcpy.env.scratchWorkspace = 'c:/LandUse' If workspace is set to a GDB or folder, the other adjusts appropriately, with the default database being scratch.gdb. Can generate temp paths for use in tools, thus: temp_path = arcpy.CreateScratchName(workspace=arcpy.env.scratchGDB) http://desktop.arcgis.com/en/arcmap/latest/analyze/python/using-environment-settings.htm

Other useful env variables http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/env.htm arcpy.env.addOutputsToMap == True | False arcpy.env.autoCommit == True | False arcpy.env.overwriteOutput == True | False arcpy.env.extent = arcpy.Extent(-107.0, 38.0, -104.0, 40.0) Some tools will only process features within the current extent. http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/env.htm

Built in functions: Exists Checks whether something exists. a = arcpy.Exists("c:/data/buildings.shp") Note that the advantage of doing this in Arc is that it unifies multiple files (for example the various files that make up a full shapefile with data) to a single entity, and allows path-based exploration of geodatabases (so called catalog paths as opposed to system paths): a = arcpy.Exists("c:/data/myGeodatabase.gdb/roads") http://desktop.arcgis.com/en/arcmap/10.4/analyze/arcpy-functions/exists.htm We'll see more built in functions when we look at data, but as a start here's two.

Checking inputs For scripts, the system should check input parameters exist. Otherwise you can check with: input = arcpy.GetParameterAsText(0) if arcpy.Exists(input):

Built in functions: Walk Walk(top, topdown, onerror, followlinks, datatype, type) allows scanning of a directory tree in Arc file space including geodatabases, e.g.: c:\data\MyGeoDataBase.gdb\myfeaturedataset\myfeatureclass for dirpath, dirnames, filenames in arcpy.da.Walk( workspace, topdown=True, datatype="RasterDataset"): if "back_up" in dirnames: dirnames.remove('back_up') for filename in filenames: rasters.append(os.path.join(dirpath, filename)) http://desktop.arcgis.com/en/arcmap/latest/analyze/python/listing-data.htm Followlinks is for online databases. Types listed at: http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/walk.htm

Built in functions: Describe Function returns a Describe object particular to the data type passed in. A bit like a uber-"type()". http://desktop.arcgis.com/en/arcmap/latest/analyze/python/describing-data.htm http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/describe.htm Can contain "Property sets" which can be drilled into further. d = arcpy.Describe("c:/data/buildings") f = d.FieldInfo print(f.count) (for this example, see also ListFields and ListIndexes)

Geoprocessing tools Essentially the tools presented in ArcToolbox. Need to find the proper name of the toolbox and tool (we'll see how in practicals). You can then do: arcpy.toolname_toolboxname(params) or arcpy.toolboxname.toolname(params) In which case it will quietly run. If you want associated parameter dialogs in an addin: pythonaddins.GPToolDialog(toolboxname, toolname) But, as we'll see in the practicals, there are some issues with this.

Geoprocessing tools Inputs in [] indicate a list should be used. Will also generally take semicolon separated strings and a ValueTable: http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/valuetable.htm

Geoprocessing tools For a custom toolbox in an external script, you need to load the toolbox to use it (as you'd have to as a user): arcpy.ImportToolbox(toolbox_path, alias_for_toolbox) The alias is a short (singleword) name for the toolbox to use in, e.g. arcpy.toolname_toolboxname(params) as the toolboxname. The alias_for_toolbox is optional if the toolbox has an alias set manually.

Using extensions If tools are in Arc extension packs (not application extension addins), you need to deal with the licensing: For example: import arcpy.sa arcpy.CheckOutExtension("spatial") # do stuff arcpy.CheckInExtension("spatial") Here we're assuming the license is ok. To check licences: if arcpy.CheckExtension("3D") == "Available": arcpy.CheckOutExtension("3D") List of extension names at: http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/checkextension.htm See also: http://desktop.arcgis.com/en/arcmap/latest/analyze/python/access-to-licensing-and- extensions.htm

Finding tools Search window in Arc (we'll see how to add tools to this in the practicals). tools = arcpy.ListTools("*") tools = arcpy.ListTools("*_conversion") Note optional wildcard * for tool in tools: print(arcpy.Usage(tool)) for toolbox in arcpy.ListToolboxes(): print(toolbox)

Optional tool parameters Fill in spaces with "", "#" (including quotes), or None. Use kwargs. For tools that demand an output path, missing out the output/setting it to "#" or None will usually make the tool result a system-created temp file. This saves having to determine this beforehand, but requires write access to default locations.

GUI options We've seen that scripts and models can have parameter GUIs. Addins are explicitly GUI elements, but can spawn others (filedialogs and messages, for example). You can add a tool to buttons and menus from a custom toolbox using the customisation options. The tool should be listed under "Geoprocessing". If not, you can add it manually: http://desktop.arcgis.com/en/arcmap/latest/analyze/finding-tools/adding-and- removing-tools-on-menus-and-toolbars.htm

Tool results Come back as a "Result" object. Usually path to output, or single data value or a list of data, including lists of lists for multivalue parameters. result = arcpy.Buffer_analysis("bomb", "buffer", "100 METERS") print (result) For multiples: result.outputCount result.getOutput(i) # Returns strings, recordsets or layers. result[i] # The same. result.saveToFile("c:\temp\file.rlt") http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/result.htm

Result result.status gives info: 0 : New 1 : Submitted 2 : Waiting 3 : Executing 4 : Succeeded 5 : Failed 6 : Timed out 7 : Cancelling 8 : Cancelled 9 : Deleting 10 : Deleted for i in range(result.messageCount): print (result.getMessage(i)) Last is often the most useful: print(result.GetMessage(result.GetMessageCount - 1)) Alternatively: print(result.GetMessages()) However, because there are cases where tool failure doesn't create a results object, you can also do the same with arcpy directly: print(arcpy.GetMessages())

Online services Some tools can run online services. IsSynchronous can be used to see if the tool is asynchronous (i.e. results may not be immediate and the code can continue to run). results = arcpy.Buffer_analysis(bomb, output, "100 METERS", "FULL", "ROUND", "NONE") if not arcpy.IsSynchronous("Buffer"): while results.status < 4: time.sleep(0.1) Note just toolname used. Example of getting a map image from a server: http://desktop.arcgis.com/en/arcmap/latest/analyze/python/using-geoprocessing-services-in-python.htm See also examples for recreating results at: http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-classes/result.htm