1 / 29

Module 6: Geoprocessing Scripts

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

kynton
Download Presentation

Module 6: Geoprocessing Scripts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 6: Geoprocessing Scripts

  2. Processing loops and decisions

  3. Most COM compliant scripting languages • AML (Arc Macro Language) • VB script • Jscript • PERL • Python (comes with ArcGIS)

  4. Python • Platform independent (linux, unix, windows) • Object-oriented, developer language • Good website (www.python.org) • Comes with ArcGIS, free from web

  5. Installing Python • ArcGIS Desktop CD • Explore rather than open to avoid autoinstallation of ArcGIS

  6. In Python folder.. • Run both exe files

  7. GeoProcessor Object

  8. Export Model to Script

  9. # 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

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

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

  12. # use + to concatenate strings:# single or double-quotes enclose string charsname = ‘moose_locations’type = “.shp”shapefile = name + typeprint shapefilemoose_locations.shp

  13. # decisions or branching:# indentation used to indicate structureiftype == '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

  14. Listing Data

  15. 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)

  16. 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”

  17. 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"

  18. 1) Check for syntax errors 2) Step Through Script Using Debugger

  19. Test Batch Process….

  20. Scheduling Scripts

  21. 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

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

More Related