You are on page 1of 29

Module 6: Geoprocessing Scripts

Processing loops and decisions


Most COM compliant scripting languages

• AML (Arc Macro Language)


• VB script
• Jscript
• PERL
• Python (comes with ArcGIS)
Python

• Platform independent (linux, unix, windows)


• Object-oriented, developer language
• Good website (www.python.org)
• Comes with ArcGIS, free from web
Installing Python

• ArcGIS Desktop CD
• Explore rather than open to avoid
autoinstallation of ArcGIS
In Python folder..

• Run both exe files


GeoProcessor Object
Export Model to Script
• Python system
# polygon_to_poly_line.py
# Created on: Fri Dec 31 2004 12:34:54 PM
• String module
# (generated by ArcGIS/ModelBuilder)
• Operating System
# Import system modules
import sys, string, os, win32com.client • COM Dispatch

# Create the Geoprocessor object


gp =win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

# Set the ArcGIS product code (Arcview, ArcEditor, or ArcInfo)


gp.SetProduct("ArcInfo")
# Load required toolboxes...
gp.AddToolbox("C:/workshop_geoprocessing/ExampleToolbox.tbx")

# 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)
# 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)
# use + to concatenate strings:
# single or double-quotes enclose string chars

name = ‘moose_locations’
type = “.shp”
shapefile = name + type
print shapefile
moose_locations.shp
# decisions or branching:
# indentation used to indicate structure

if type == 'point' :
print 'Theme is point type'
print 'Must be polygon type to use erase tool'
elif type == 'polyline' :
print 'Theme is polyline type'
print 'Convert to polygon type, then rerun script'
elif type == 'polygon':
print 'Theme is polygon type'
print 'Correct feature type for using erase tool'
else :
print "Theme type is not point, line, or polygon"
print “End of Script” #out of if block
Listing Data
List first 2 pond polygon feature classes
# Import system modules
import sys, string, os, win32com.client
# Create the Geoprocessor object
gp =
win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
#set workspace
gp.workspace = "C:/ponds "; print "workspace set to: ",
str(gp.workspace)
#get list of feature classes
fcs = gp.ListFeatureClasses("pond*","polygon")
fcs.reset()
#get first two objects in list and assign to variables theme1,
theme2:
theme1 = fcs.next()
theme2 = fcs.next()
List all pond polygon feature classes
# Import system modules
import sys, string, os, win32com.client
# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
#set workspace
gp.workspace = "C:/ponds“; print "workspace set to: ", str(gp.workspace)
#get list of feature classes
fcs = gp.ListFeatureClasses("pond*","polygon")
fcs.reset()
# Get the first theme and start the loop
Current_Theme = fcs.next()
while Current_Theme: # While the Current_Theme is not empty
Print “Current theme in list is:”, str(Current_Theme)
Current_Theme = fcs.next()
Print “End of Script”
Convert all pond polygon to line themes
# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object


gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

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

#get list of feature classes


fcs = gp.ListFeatureClasses("pond*","polygon")
fcs.reset()

print "All pond polygon themes will be converted to pond shoreline themes..."
# Get the first theme and start the loop
Current_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"


1) Check for syntax errors

2) Step Through Script Using Debugger


Test Batch Process….
Scheduling Scripts
Sources of Confusion
• Python commands and variables are case sensitive
( print theme <> Print theme <> print Theme )

• Geoprocessor properties not case sensitive


( gp.workspace = gp.WorkSpace )

\ is a reserved character meaning line continuation


(use / or \\ for paths instead of \)

• Indentation is a source of loop structure


Sources of Confusion
• Model does not use UML like ArcObjects
• Arrows indicate instantiation
• Only non character properties are indicated in diagram

You might also like