Slides GeoprocessWithPythonInArcGIS10 1
Slides GeoprocessWithPythonInArcGIS10 1
Melissa Brenneman
www.topoworks.com
Welcome
Instructor: Melissa Brenneman Restrooms Schedule
Introduction
Lecture 1 - 1
Python
free , powerful, easy to learn, installed with ArcGIS create programs (scripts) for geoprocessing
Introduction
Lecture 1 - 2
Python window
Use additional functionality of Python Includes help/usage Test ideas outside of large script Starting point for creating scripts
IDLE (Free) - Default editor with standard Python install PythonWin (Free)
Windows development environment Must install separately
Others
PyScripter (Free) Wing
Introduction
Lecture 1 - 3
PythonWin
Script
Interactive Window
Buffer, Clip, AddField Workspace, Coordinate System, Cell Size ListFields, ListFeatureClasses, Describe, SearchCursor SpatialReference, FieldMap arcpy.mapping manipulating map docs, map automation arcpy.sa Spatial Analyst functions and operators arcpy.ga Geostatistical Analyst functions arcpy.da Data Access functions (10.1) arcpy.na Network Analyst functions (10.1)
Set environments
Introduction
Lecture 1 - 4
Set environments
arcpy.env.workspace = "C:\\Temp\\ToolData\\Indiana.gdb"
arcpy.env.overwriteOutput = True
Run tools
arcpy.Buffer_analysis (inData, outData, 50)
Writing scripts
Comments preface with #
# Buffer roads
10
Introduction
Lecture 1 - 5
Concatenating strings
print "This teacher is " + "GREAT!" inData = "Indiana.gdb" + "\\" + "cnty"
11
r o a ds . s hp
0 1 2 3 4 5 6 7 8
r o a ds . s hp
0 1 2 3 4 5 6 7 8 9 -9 -8 -7 -6 -5 -4 -3 -2 -1
12
Introduction
Lecture 1 - 6
Indentation
No begin/end statements to identify blocks of code Everything at an indentation is a block of code
if x > 5: print "x is greater than 5" print "but I don't care" if y == 0: print "y is 0" == is used to test conditions = is used to assign values : colon at the end of if 1st block of code 2nd block of code
13
14
Introduction
Lecture 1 - 7
Computer Exercises
Accessing the exercises Training data Starting ArcMap Starting PythonWin
15
Introduction
Lecture 1 - 8
ArcPy
Tools
Basic, Standard, Advanced (ArcView, ArcEditor, ArcInfo) Spatial Analyst, Geostatistical Analyst,
Functions
Lexture 2 - 1
ArcPy
Classes
Have properties
Ex: properties of env are used for environment settings arcpy.env.workspace = "c:/data/hydrology.gdb"
arcpy.mapping manipulating map docs, map automation arcpy.sa Spatial Analyst functions and operators arcpy.ga Geostatistical Analyst functions arcpy.da Data Access functions (10.1) arcpy.na Network Analyst functions (10.1)
feature classes, tables, datasets, shapefiles, workspaces, files Returns Boolean (True or False) Uses the current workspace
arcpy.env.workspace = "c:/data/HamiltonCnty.gdb" if not arcpy.Exists(outFC): # create outFC arcpy.Select_analysis (inFC, outFC, wClause)
Lexture 2 - 2
Cursors
Iterate through rows in a table
read or write geometries make mass updates to a field summarize and report field values
Read rows Insert rows Update or delete rows
No backing up
Use reset method to step through again
Lexture 2 - 3
With cursor field values >>> print row[0] >>> Clinton >>> print row[0][:4].upper() >>> CLIN
Lexture 2 - 4
Listing data
Get a list of feature classes, tables, rasters,
Print vs. print, If vs. if, For vs. for arcpy.env.Workspace vs. arcpy.env.workspace
Incorrect indentation Missing colons after if, else, for, etc. Missing quotation marks on ArcPy function parameters that require strings (most tool parameters) Missing double equal sign for testing conditions
10
Lexture 2 - 5
Getting Help
11
Lexture 2 - 6
Executing Scripts
In IDE (eg. PythonWin) At the DOS prompt
Python C:\PythonScripts\RoadBuffer.py
Can also be scheduled to run at a certain time Can be added to a batch file (.BAT) Add a script to a toolbox Double-click to run from a dialog box Edit to open script source in IDE editor (eg. PythonWin) Script tools behave similarly to ArcGIS system tools
Can be added to models, other scripts Can be executed from ArcGIS Python window Can be documented
Lexture 3 - 1
Capturing arguments
Arguments are inputs to scripts Make scripts more flexible, values arent hard-coded
arcpy.GetParameterAsText()
(0-based)
import arcpy # set variables to capture user arguments inWksp = arcpy.GetParameterAsText(0) cntyBnd = arcpy.GetParameterAsText(1) outPath = arcpy.GetParameterAsText(2)
Lexture 3 - 2
Assigning parameters
Display name Data type (Field)
Can be used to wrap entire programs or portions Error > exception raised > code under except is executed arcpy.GetMessages() to retrieve geoproc messages
(0) all messages, (1) warnings, (2) errors
try: arcpy.Buffer_analysis("roads", "roads_buf") #if error running tool, print tool messages except arcpy.ExecuteError: print arcpy.GetMessages(2) #any other error except Exception as err: print err.message 6
Lexture 3 - 3
ArcPy Modules
Mapping module (arcpy.mapping)
Manipulate contents of .mxd and .lyr files Functions to automate exporting and printing Perform map algebra Set up complex neighborhood searches Automate network analysis workflows Module for working with data: edit sessions, improved cursors (see Part 2), NumPy arrays
Lexture 3 - 4
TopoWorks 110 Dry Run Drive Noblesville, IN 46060 Tel: (317) 674-3396 [email protected]
www.topoworks.com