GIS Jam ArcGIS Python Scripting Dave Verbyla

46
2006 GIS Jam: ArcGIS Python Scripting http://nrm.salrm.uaf.edu/~dverbyla/ arcgis_python

Transcript of GIS Jam ArcGIS Python Scripting Dave Verbyla

Page 1: GIS Jam ArcGIS Python Scripting Dave Verbyla

2006 GIS Jam: ArcGIS Python Scripting

http://nrm.salrm.uaf.edu/~dverbyla/arcgis_python

Page 2: GIS Jam ArcGIS Python Scripting Dave Verbyla

Getting Started With Python Geoprocessing

• Why use scripting with ArcGIS?

• Python Basics

• ArcGIS Geoprocessor Basics

• Simple Applications

• Sources of confusion

Page 3: GIS Jam ArcGIS Python Scripting Dave Verbyla

Why bother with scripting?

Page 4: GIS Jam ArcGIS Python Scripting Dave Verbyla

When NOT to bother with scripting?

Page 5: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 6: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 7: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 8: GIS Jam ArcGIS Python Scripting Dave Verbyla

• VB script

• Jscript

• PERL

• Python (comes with ArcGIS)

Scripting Languages for Geoprocessing

Page 9: GIS Jam ArcGIS Python Scripting Dave Verbyla

Python

• Platform independent (linux, unix, windows)

• Object-oriented, developer language

• Good website (www.python.org)

• Comes with ArcGIS, free from web

Page 10: GIS Jam ArcGIS Python Scripting Dave Verbyla

GeoProcessor Model

Page 11: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 12: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 13: GIS Jam ArcGIS Python Scripting Dave Verbyla

Export Model to Script

Page 14: GIS Jam ArcGIS Python Scripting Dave Verbyla

ArcGIS Geoprocessor Basics

• Import modules

• Create geoprocessor object

• Use geoprocessor

Page 15: GIS Jam ArcGIS Python Scripting Dave Verbyla

# polygon_to_poly_line.py# Created on: Fri Dec 31 2004 12:34:54 PM# (generated by ArcGIS/ModelBuilder)

# Import system modulesimport sys, string, os, win32com.client

# Create the Geoprocessor objectgp =win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

# Local variables...poly_lines_shp = "C:/temp/poly_lines.shp"selected_polygons_shp = "C:/temp/selected_polygons.shp"

# Process: Polygon To Line...gp.toolbox = "C:/workshop_geoprocessing/ExampleToolbox.tbx"gp.PolygonToLine(selected_polygons_shp, poly_lines_shp)

• Python system

• String module

• Operating System

• COM Dispatch

Page 16: GIS Jam ArcGIS Python Scripting Dave Verbyla

OROR

Page 17: GIS Jam ArcGIS Python Scripting Dave Verbyla

# Script arguments or variables...Input_Features = sys.argv[1]Output_Feature_Class = sys.argv[2]

# Process: Polygon To Line...gp.toolbox = "C:/temp/My Toolbox.tbx"gp.PolygonToLine(Input_Features, Output_Feature_Class)

Script Arguments---input from user

Page 18: GIS Jam ArcGIS Python Scripting Dave Verbyla

Running python scripts

• Using PythonWin debugger

• Using Python command window

• As system process

• As scheduled process

Page 19: GIS Jam ArcGIS Python Scripting Dave Verbyla

PythonWin

Page 20: GIS Jam ArcGIS Python Scripting Dave Verbyla

1) Check for syntax errors

2) Step Through Script Using Debugger

Page 21: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 22: GIS Jam ArcGIS Python Scripting Dave Verbyla

Thousands of Toolbars Bog Down System To A Crawl!

Page 23: GIS Jam ArcGIS Python Scripting Dave Verbyla

“Easy Fix”— comment out self.SaveBarState(ToolbarDefault)

Page 24: GIS Jam ArcGIS Python Scripting Dave Verbyla

Python command window

Page 25: GIS Jam ArcGIS Python Scripting Dave Verbyla

Running script as system process

Page 26: GIS Jam ArcGIS Python Scripting Dave Verbyla

Scheduling Scripts

Page 27: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 28: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 29: GIS Jam ArcGIS Python Scripting Dave Verbyla

Messages From Executing Tools

Page 30: GIS Jam ArcGIS Python Scripting Dave Verbyla

Example Applications

For every polygon theme in workspace

• Convert polygon theme to polyline theme

• Compute area and perimeter in all themes

For every workspace in text file:

• Build raster pyramids for every raster in each workspace

Page 31: GIS Jam ArcGIS Python Scripting Dave Verbyla

Convert all pond polygon to line themes # Import system modulesimport sys, string, os, win32com.client

# Create the Geoprocessor objectgp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

#set workspacegp.workspace = "C:/ponds"; print "workspace set to: ", str(gp.workspace)

#get list of feature classesfcs = gp.ListFeatureClasses("*","polygon")fcs.reset()

print "All pond polygon themes will be converted to pond shoreline themes..."# Get the first theme and start the loopCurrent_Theme = fcs.next()while Current_Theme: # While the Current_Theme is not empty print "Converting Theme:", str(Current_Theme) gp.PolygonToLine(Current_Theme, "c:/shorelines/" + Current_Theme) Current_Theme = fcs.next()

print "End of Script"

Page 32: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 33: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 34: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 35: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 36: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 37: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 38: GIS Jam ArcGIS Python Scripting Dave Verbyla

Problems With Python Scripting

• Interpreted language

• Case sensitive

• Indentation source of structure

• \ is a reserved character

• # is a commenting character

• Newline character at end of lines

Page 39: GIS Jam ArcGIS Python Scripting Dave Verbyla

Interpreted Line by Line = SLOW

Page 40: GIS Jam ArcGIS Python Scripting Dave Verbyla

Case Sensitive

Page 41: GIS Jam ArcGIS Python Scripting Dave Verbyla

Indentation Source of Structure

Page 42: GIS Jam ArcGIS Python Scripting Dave Verbyla

/ is a reserved character# is a comment character

Page 43: GIS Jam ArcGIS Python Scripting Dave Verbyla

Determine shape type of

every theme listed in text file…..

Page 44: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 45: GIS Jam ArcGIS Python Scripting Dave Verbyla
Page 46: GIS Jam ArcGIS Python Scripting Dave Verbyla

More information

Python• http://wiki.python.org/moin/BeginnersGuide

• http://www.python.org/

• http://diveintopython.org/

ArcGIS Python Scripting• http://hobu.biz/software/python_guide_esri/

• http://arcscripts.esri.com/