29
Module 6: Geoprocessing Scripts

Geoprocessing Scripts

Embed Size (px)

Citation preview

Page 1: Geoprocessing Scripts

Module 6: Geoprocessing Scripts

Page 2: Geoprocessing Scripts

Processing loops and decisions

Page 3: Geoprocessing Scripts
Page 4: Geoprocessing Scripts
Page 5: Geoprocessing Scripts

• AML (Arc Macro Language)

• VB script

• Jscript

• PERL

• Python (comes with ArcGIS)

Most COM compliant scripting languages

Page 6: Geoprocessing Scripts

Python

• Platform independent (linux, unix, windows)

• Object-oriented, developer language

• Good website (www.python.org)

• Comes with ArcGIS, free from web

Page 7: Geoprocessing Scripts

Installing Python

• ArcGIS Desktop CD

• Explore rather than open to avoid autoinstallation of ArcGIS

Page 8: Geoprocessing Scripts

In Python folder..

• Run both exe files

Page 9: Geoprocessing Scripts

GeoProcessor Object

Page 10: Geoprocessing Scripts
Page 11: Geoprocessing Scripts
Page 12: Geoprocessing Scripts

Export Model to Script

Page 13: Geoprocessing Scripts

# 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")

# Set the ArcGIS product code (Arcview, ArcEditor, or ArcInfo)gp.SetProduct("ArcInfo")

• Python system

• String module

• Operating System

• COM Dispatch

Page 14: Geoprocessing Scripts

# 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)

Page 15: Geoprocessing Scripts

# 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)

Page 16: Geoprocessing Scripts

# use + to concatenate strings:# single or double-quotes enclose string chars

name = ‘moose_locations’type = “.shp”shapefile = name + typeprint shapefilemoose_locations.shp

Page 17: Geoprocessing Scripts

# 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

Page 18: Geoprocessing Scripts

Listing Data

Page 19: Geoprocessing Scripts

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()

print "First two polygon themes in workspace: ", str(theme1), str(theme2)

Page 20: Geoprocessing Scripts

List all pond polygon feature classes # 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("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”

Page 21: Geoprocessing Scripts
Page 22: Geoprocessing Scripts

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("pond*","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 23: Geoprocessing Scripts

1) Check for syntax errors

2) Step Through Script Using Debugger

Page 24: Geoprocessing Scripts

Test Batch Process….

Page 25: Geoprocessing Scripts

Scheduling Scripts

Page 26: Geoprocessing Scripts
Page 27: Geoprocessing Scripts
Page 28: Geoprocessing 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

Page 29: Geoprocessing Scripts

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