Lecture 9 Using Python for Geoprocessing

Slides:



Advertisements
Similar presentations
How to enter the world of Python Programming for ArcGIS Or, a funny thing happened on the way from an ESRI conference By Katherine Paybins WVAGP Membership.
Advertisements

Intro to Python Welcome to the Wonderful world of GIS programing!
Customisation The GUI in most GIS applications is sufficient for most needs. However, situations arise where you want either to: –Modify the interface,
Why python? Automate processes Batch programming Faster Open source Easy recognition of errors Good for data management What is python? Scripting programming.
Introduction to Python. Python is a high-level programming language Open source and community driven “Batteries Included” – a standard distribution includes.
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.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Introduction to ESRI Add-Ins
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.
Geography 465 Analytic Cartography: Getting Started with Python Scripting.
Introduction to GIS Programming By Jun Liang Department of Geography UNC-CH.
Geography 465 GIS Database Programming Getting Started with GIS Database Programming.
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.
Working with cursors in Python GISDE Python Workshop Qiao Li.
2006 GIS Jam: ArcGIS Python Scripting
Writing Geoprocessing Scripts With ArcGIS Lecture 9.
Introduction to Spatial Analysis and Spatial Modeling
Copyright © 2002, 2003, 2004 ESRI. All rights reserved. What's New in ArcGIS 9 Introduction to Geoprocessing in ArcGIS 9 Jason Grootens ESRI-Minneapolis.
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
Introduction to Programming Peggy Batchelor.
ArcGIS Network Analyst: Automating Workflows with Geoprocessing
Python – Part 1 Python Programming Language 1. What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source.
Python, Toolboxes, Tools & Script Tools
Technical Workshops | Esri International User Conference San Diego, California Creating Geoprocessing Services Kevin Hibma, Scott Murray July 25, 2012.
Module 6: Geoprocessing Scripts. Processing loops and decisions.
Esri UC 2014 | Technical Workshop | Creating Geoprocessing Services Kevin Hibma.
Juanita Cano City of Sacramento Spring 2014 Geography 375.
Lecture 10: Geoprocessing with Python (II) Dr. Taysir Hassan Abdel Hamid Associate Professor, Information Systems Dept., Faculty of Computers and Information.
Lecture 9: Geoprocessing with Python
Development Environment
Introduction to GIS PythonScript CGIS-NURIntroduction to ArcGIS II.
Basic 1960s It was designed to emphasize ease of use. Became widespread on microcomputers It is relatively simple. Will make it easier for people with.
Data Virtualization Tutorial: Introduction to SQL Script
Operating System Interface between a user and the computer hardware
PYTHON: AN INTRODUCTION
Environmental GIS Nicholas A. Procopio, Ph.D, GISP
Testing and Debugging.
Introduction to PL/SQL
Introduction to Programming the WWW I
Programming Concepts and Languages
Programming and Automation
Writing Geoprocessing Scripts With ArcGIS
How to enter the world of Python Programming for ArcGIS
Lecture 10 Accessing tools and environment setting in Scripts
Lecture 3 Introducing ArcObjects
Writing Geoprocessing Scripts With ArcGIS
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Lecture 22 Inheritance Richard Gesick.
Electronics II Physics 3620 / 6620
Automating and Validating Edits
Geography 465 Managing Custom Python Script Tools
Software Engineering System Modeling Chapter 5 (Part 1) Dr.Doaa Sami
Geography 465 GIS Database Programming
Leveraging ArcGIS Online Elevation and Hydrology Services
Network Analysis using Python
Chapter 1: Programming Basics, Python History and Program Components
Network Analyst – Automating Workflows with Geoprocessing
Programming Arc.
Topics Introduction to File Input and Output
Clip & Convert to ASCII Program Kelly Knapp Spring 2010
Presentation transcript:

Lecture 9 Using Python for Geoprocessing ESRI chose Python as the support language for geoprocessing because: • Python is easy to learn because of its clean syntax and simple, clear concepts. • Python supports object-oriented programming in an easy-to-understand manner. • Documenting Python is easier because it has readable code. • Complicated data structures are easy to work with in Python. • Python is simple to integrate with C++ and Fortran. • Python can be seamlessly integrated with Java. • Python is free from the Web and has a widespread community. 2018/11/10 Jun Liang, Geography @ UNC

Jun Liang, Geography @ UNC Python and ArcGIS 9.2 Pythonwin was installed with Arc8.3, 9.0 and 9.1, but not with ArcGIS 9.2. Only IDLE editor is installed with ArcGIS 9.2 Pythonwin was required to run win32com scripts, so your existing scripts may not work in ArcGIS 9.2 ArcGIS 9.2 is hard wired to Python2.4.1, and Python2.5 or higher may not work with 9.2 and also is not supported by ESRI To take advantage of new geoprocessing scripting functionality, such as the arcgisscripting module and cross platform support, the win32com module cannot be used. For more information about python compatibility, check http://support.esri.com/index.cfm?fa=knowledgebase.techarticles.articleShow&d=31900 2018/11/10 Jun Liang, Geography @ UNC

Scripting Functionalities 2018/11/10 Jun Liang, Geography @ UNC

Scripting Functionalities (Cont.) Review frequently used data types and control structures in geoprocessing: Import math Import sys, os If…elif…else for…in while fcList=[‘river.shp’, ‘county.shp’, ‘city.shp’, ‘street.shp’] 2018/11/10 Jun Liang, Geography @ UNC

Tools and environment settings Tools up to 455 • Union, Buffer, Project, Import from CAD, Delete, Geocode Addresses, Hillshade, Create TIN, Feature Outline Masks, Create Routes, Mean Center, Build, and much more... Environment settings: 22 • Workspace, Cell Size, Output Coordinate System, Compression, Tile Size, Output Extent, and much more... Example in pythonwin with win32com.client import win32com .client gp • win32com.c1ient.Dispatch(”esriGeoprocesing.GpDispatch.1”) gp .Workspace = “c :\\Data\\Canada .mdb” gp.Buffer(”Streams”, “NoBuildZone”, 100) gp.Erase(”LandUse”, “NoBuildone”, “BuildZone”) 2018/11/10 Jun Liang, Geography @ UNC

Pythonwin compatibility issue win32com require PythonWin win32com module is part of PythonWin To run or edit previous pythonwin scripts that use the win32com module, PythonWin needs to be installed. 2018/11/10 Jun Liang, Geography @ UNC

Jun Liang, Geography @ UNC Code without win32.com import arcgisscripting #Create the Geoprocessor object gp = arcgisscripting.create() gp.overwriteoutput = 1 gp.Workspace = "S:\\data\\GPS\\CNTrail.mdb" gp.Buffer("NLTrail", "trail10", 10) print "done" For gp.overwriteoutput: http://webhelp.esri.com/arcgisdesktop/9.1/body.cfm?tocVisable=1&ID=1880&TopicName=OverWriteOutput%20method 2018/11/10 Jun Liang, Geography @ UNC

Geoprocessor Programming Model Diagram The Geoprocessor Programming Model is not based on the Unified Modeling Language (UML) notation used in the ArcObjects object model diagrams. The arrows on the diagram indicate instantiation. GpDispatch is the COM name for the geoprocessor IDispatch object. Any scripting language that can instantiate a COM IDispatch object may create the geoprocessor. In Python, the Dispatch method on the win32com module’s client object is used to create the geoprocessor, while Visual Basic and VBScript use the CreateObject function. The geoprocessor is the only ArcGIS scripting object that can be directly created using these methods. All other ArcGIS scripting objects are created by a method on the geoprocessor, such as CreateObject, or as a property of an object, such as the fields of a feature class. 2018/11/10 Jun Liang, Geography @ UNC

Geoprocessor Programming Model Diagram The different colors are meant to show logical relationships of objects. Some properties, such as Field in a feature class, are colored to indicate they are an object, which, in this case, is the fields object that is purple. The GpDispatch object supports the execution of tools as dynamic methods and the use of environment settings as dynamic properties. The diagram does not show these dynamic members of the geoprocessor, as the tools and environments that are accessible depend on what is installed on the desktop machine being used and what toolboxes are being referenced. The geoprocessor object simply refers to tools and environments as generic methods and properties. 2018/11/10 Jun Liang, Geography @ UNC

Accessing the Geoprocessor from Python A typical start of a python script: #Import standard library modules import arcgisscripting, sys, os gp = arcgisscripting.create() This imports the system, operating system, and arcgisscripting modules to the script. The sys module refers to the Python system and will be used to access user-specified inputs. The os module provides easy access to the most fundamental tools of the operating system. Some of the os module’s filename manipulation tools are used in this script. Gp can be used to access all geoprocessing functionality through this variable. 2018/11/10 Jun Liang, Geography @ UNC

Accessing the Geoprocessor from Python (Cont.) #Set the input workspace GP.workspace = sys.argv[1] print “Current workspace: ”, GP.workspace #Set the clip featureclass clipFeatures = sys.argv[2] #Set the output workspace outWorkspace = sys.argv[3] #Set the cluster tolerance clusterTolerance = sys.argv[4] try: #Get a list of the featureclasses in the input folder fcs = GP.ListFeatureClasses() 2018/11/10 Jun Liang, Geography @ UNC

Accessing the Geoprocessor from Python (Cont.) The try statement defines the beginning of a block of code that will be handled by its associated exception handler, or except statement. It is good practice to use exception handling in any script using the geoprocessor so its error messages can be propagated back to the user. This also allows the script to exit gracefully and return informative messages instead of simply causing a system error. The geoprocessor’s ListFeatureClasses method returns an enumeration of feature class names in the current workspace. The workspace defines the location of your data and where all new data will be created unless a full path is specified. The workspace has already been set to the first argument’s value. 2018/11/10 Jun Liang, Geography @ UNC

Accessing the Geoprocessor from Python (Cont.) Add the following code to get the first value of the enumeration: #Loop through the list of feature classes fcs.Reset() fc = fcs.Next() while fc: #Validate the new feature class name for the output workspace. outFeatureClass = outWorkspace + "/" + GP.ValidateTableName(fc, outWorkspace) #Clip each feature class in the list with the clip feature class. #Do not clip the clipFeatures, it may be in the same workspace. if str(fc) != str(os.path.split(clipFeatures)[1]): GP.Clip(fc, clipFeatures, outFeatureClass, clusterTolerance) except: GP.AddMessage(GP.GetMessages(2)) print GP.GetMessages(2) 2018/11/10 Jun Liang, Geography @ UNC

Jun Liang, Geography @ UNC Customize ArcTools ArcToolbox includes many tools (more than 400), and you can access them from ArcToolbox – also, you can use them in your python script. Set up workspace, get workspace information from users’ input, or retrieve workspace information from environment parameters. Set inputs for ArcToolbox. 2018/11/10 Jun Liang, Geography @ UNC

Customize ArcTools (Cont.) Examples of customizing tools: …. # You may also set input parameters from users’ input # Local variables... test_shp = “S:/liangj/test.shp" CITIES_shp = “S:/data/USA/CITIES.shp" orange_shp = “S:/data/orange.shp" # Process: Clip... gp.Clip_analysis(CITIES_shp, orange_shp, test_shp, "") 2018/11/10 Jun Liang, Geography @ UNC

Customize ArcTools (Cont.) Another example: #Import standard library modules import arcgisscripting, sys, os gp = arcgisscripting.create() gp.Workspace = r"D:\GTKArcGIS\Data\Tongass.mdb" print "current workspace is " + gp.Workspace gp.Workspace = r"D:\GTKArcGIS\shapefiles" bufferFC = sys.argv[1] outFeature = sys.argv[2] gp.Buffer_analysis(bufferFC,outFeature,100) 2018/11/10 Jun Liang, Geography @ UNC

Customize ArcTools (Cont.) Customize selection tool: ….. # Local variables... COUNTIES_Select_shp = "K:/data/USA/COUNTIES_Select.shp" COUNTIES_shp = "K:/data/USA/COUNTIES.shp" # Process: Select... gp.Select_analysis(COUNTIES_shp, COUNTIES_Select_shp, ""NAME" ="Orange" OR "NAME" ="Durham"") 2018/11/10 Jun Liang, Geography @ UNC

Customize ArcTools (Cont.) Find out usage for a script: gp.Usage(“Buffer_analysis”) gp.Usage(“Select_analysis”) gp.Usage(“Clip_analysis”) Or, you can use ArcGIS Desktop help. You may also output the corresponding model to a python script and check how parameters are defined. 2018/11/10 Jun Liang, Geography @ UNC