1 / 46

nrm.salrm.uaf/~dverbyla/arcgis_python

ArcGIS Python Scripting Workshop Dave Verbyla Department of Forest Sciences School of Natural Resources and Agricultural Sciences University of Alaska Fairbanks. http://nrm.salrm.uaf.edu/~dverbyla/arcgis_python. Getting Started With Python Geoprocessing. Why use scripting with ArcGIS?

Download Presentation

nrm.salrm.uaf/~dverbyla/arcgis_python

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. ArcGIS Python Scripting WorkshopDave VerbylaDepartment of Forest SciencesSchool of Natural Resources and Agricultural SciencesUniversity of Alaska Fairbanks http://nrm.salrm.uaf.edu/~dverbyla/arcgis_python

  2. Getting Started With Python Geoprocessing • Why use scripting with ArcGIS? • Python Basics • ArcGIS Geoprocessor Basics • Simple Applications • Sources of confusion

  3. Why bother with scripting?

  4. When NOT to bother with scripting?

  5. Scripting Languages for Geoprocessing • VB script • Jscript • PERL • Python (comes with ArcGIS)

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

  7. GeoProcessor Model

  8. Export Model to Script

  9. ArcGIS Geoprocessor Basics • Import modules • Create geoprocessor object • Use geoprocessor

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

  11. OR OR

  12. Script Arguments---input from user # 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)

  13. Running python scripts • Using PythonWin debugger • Using Python command window • As system process • As scheduled process

  14. PythonWin

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

  16. Thousands of Toolbars Bog Down System To A Crawl!

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

  18. Python command window

  19. Running script as system process

  20. Scheduling Scripts

  21. Messages From Executing Tools

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

  23. 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("*","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"

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

  25. Interpreted Line by Line = SLOW

  26. Case Sensitive

  27. Indentation Source of Structure

  28. / is a reserved character# is a comment character

  29. Determine shape type of every theme listed in text file…..

  30. 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/

More Related