Download presentation
Presentation is loading. Please wait.
Published byMartina Stewart Modified over 8 years ago
1
Module 6: Geoprocessing Scripts
2
Processing loops and decisions
5
AML (Arc Macro Language) VB script Jscript PERL Python (comes with ArcGIS) Most COM compliant scripting languages
6
Python Platform independent (linux, unix, windows) Object-oriented, developer language Good website (www.python.org) Comes with ArcGIS, free from web
7
Installing Python ArcGIS Desktop CD Explore rather than open to avoid autoinstallation of ArcGIS
8
In Python folder.. Run both exe files
9
GeoProcessor Object
12
Export Model to Script
13
# polygon_to_poly_line.py # Created on: Fri Dec 31 2004 12:34:54 PM # (generated by ArcGIS/ModelBuilder) # Import system modules import sys, string, os, win32com.client # Create the Geoprocessor object gp =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
14
# 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)
15
# 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)
16
# use + to concatenate strings: # single or double-quotes enclose string chars name = ‘moose_locations’ type = “.shp” shapefile = name + type print shapefile moose_locations.shp
17
# 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
18
Listing Data
19
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)
20
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”
22
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"
23
1) Check for syntax errors 2) Step Through Script Using Debugger
24
Test Batch Process….
25
Scheduling Scripts
28
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
29
Sources of Confusion Model does not use UML like ArcObjects Arrows indicate instantiation Only non character properties are indicated in diagram
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.