430 likes | 607 Views
Geography 465. Analytic Cartography: Getting Started with Python Scripting. What is Geoprocessing?. The application of GIS operations on a set of inputs for generating new information (or new data as appropriate to situation) All geoprocessing tools are accessed from ArcToolbox
E N D
Geography 465 Analytic Cartography: Getting Started with Python Scripting
What is Geoprocessing? • The application of GIS operations on a set of inputs for generating new information (or new data as appropriate to situation) • All geoprocessing tools are accessed from ArcToolbox • A tool can be a dialog, a script, or a model.
Why write scripts? • Scripts supply the added benefit of decision making logic and looping functionality • Automate a work flow, for example: • Copy all incoming data into a geodatabase • Perform a project, clip, buffer operation on multiple data sets (iterate) • Easily distribute code • A script is a self-contained, single file
Why use Python for Scripting? • An open-source, object-oriented, scripting language • Offers IDE (integrated development environment) with debugging tools • Modular, can be broken apart • Ability to compile scripts • Installed with ArcGIS 9 and ESRI samples provided
Writing code in Python • Where to write Python code? • Python command line • IDE (integrated development environment): PythonWin • IDE allows you to perform all jobs from one location • Write, • Save, • Run, and • Debug code
PythonWin Interface • Script window • Write and save code • Interactive window • Test lines of code • Report messages • Menus and Toolbars • standard and debugging
Basics of Python • Comment: A non-executable line of code • One number sign (#) for green and italicized • Two number signs (##) for gray # Name: Tim Nyerges # Date: January 3, 2007 # Purpose: To buffer a feature class import win32com.client gp = win32com.client.Dispatch(“esriGeoprocessing.GpDispatch.1”) ## gp.Workspace = “C:\\Python_Data\\SanDiego.mdb” Gp.Buffer_analysis (“Freeways”, “BuffFreeway”, 1000) • Can comment and uncomment blocks of code
Variables in Python • Variables are dynamically typed • No declaration required • No type assignment required fc = “C:\\ProjectData\\SanDiego.mdb\\Freeways.shp” • Variables are case sensitive fc = “Freeways.shp” Fc = 20000 • Variables can hold different data types • strings, numbers, lists, files • Two different variables
Strings • Variables can hold strings folder = “c:/Student” • Strings are surrounded in double (“) or single (‘) quotes • Pathnames use two back (\\) or one forward (/) slash One backslash (\) is a reserved escape character and a line continuation character
Strings • Strings can be combined together gdbPath = “c:\\SanDiego.mdb” fc = “Roads” fullPath = gdbPath + “\\” + fc • Strings are indexed • strings are zero-based from the left and one-based from the right fc = “Streets.shp” fc[0] ---> “S” # S is in the 0 position fc[1:3] ---> “tr” # start at 1st, up to not including 3rd fc[:-4] ---> “Streets” # get rid of the last 4 charaters • C:\SanDiego.mdb\Roads”
Numbers and lists • Variables can hold numbers and expressions num1 = 1.2 num2 = 3 + 5 • Variables can hold lists numList = [1, 2, 3] fcList = [“Roads”, “Streets”, “Parcels”, “Zipcodes”] • Lists are indexed fc1 = fcList[1] fc2 = fcList[0:2] ---> “Roads”, “Streets” fc3 = fcList[0:-1] ---> “Roads”, “Streets”, “Parcels” fc4 = fcList[2:] ---> “Parcels”, “Zipcodes” ---> “Streets”
Variable naming conventions • Upper case versus lower case • First word lower case, capitalize each successive word tableFieldName = “Street” - Acronym at the beginning, use lower case letters gdbPath = “C:\\SanDiego.mdb” • Acronym in the middle or at the end, use upper case letters inputFC = “Streets.shp” • Avoid special characters (for example / \ & % # !) • Use descriptive variable names
Line continuation • Line continuation characters • Parentheses ( ), brackets [ ], and braces { } • Backslash \ • Indentation is automatic fcList = [“Roads”, “Climate”, “Streams”, “Zipcodes”, “Coastlines”] distanceValues = 100, 200, 300, 400, 500, \ 1000, 1500 gp.Buffer_analysis(fcList[2], BuffStreams1000, distanceValues[5])
Built-in functions • Python has many built-in functions • Built-in means that Python automatically makes these functions available – they don’t have to be imported like most other functions • You can see the list of built-in functions by typing dir(__builtins__) into the interactive window of PythonWin
Examples of Built-in functions • len ( ) – returns the length fc = “Streams.shp” len (fc) ---> 11 • max ( ) – returns the maximum value xExtent = (5210474.99, 7438807.99) max (xExtent) ---> 7438807.99 • open ( ) – opens a file coord = open(“C:\\Mydata.txt”, “r”).read( ) ---> holds the contents of the Mydata.txt file round ( ) – Rounds a number xCoord = 5210474.99 round(xCoord) ---> 5210475.0
Accessing modules • Most functions are imported from modules • The math module import math math.sqrt(64) ---> 8.0 math.pi ---> 3.14156 • The string module Import string string.split(“75.7 -45.3”) ---> [’75.7’, ‘-45.3’] string.upper(“c:\\student”) ---> ‘C:\\STUDENT’
Accessing modules • The os.path module import os.path os.path.basename(“C:\\SanDiego.mdb\\Streets,shp” ---> ‘Streets.shp’ os.path.dirname((“C:\\SanDiego\\Streets.shp” ---> ‘C:\\SanDiego’
Defining your own functions • Function is a named sequence of statements that performs a desired operation. • This operation is specified in a function definition. def NAME( LIST OF PARAMETERS ): STATEMENTS
Defining your own functions - example def newLine(): print print "First Line." newLine() print "Second Line." def threeLines(): newLine() newLine() newLine() print "First Line." threeLines() print "Second Line."
Defining your own functions with parameters- example def printTwice(n): print n, n printTwice(“Geog465 project!”) ---> ? n = “Geog465 project!” printTwice(n)
Boolean Expressions • A boolean expression is an expression that is either true or false. • In Python an expression that is true has the value 1, and an expression that is false has the value 0. • In this case the equal comparison (not assignment) operator is as follows. >>> 5 == 5 1 >>> 5 == 6 0
Boolean Expressions The == operator is one of the comparison operators; the others are: x != y # x is not equal to y x > y # x is greater than y x < y # x is less than y x >= y # x is greater than or equal to y x <= y # x is less than or equal to y
Logical Operators • There are three logical operators: and, or, and not. • The semantics (meaning) of these operators is similar to their meaning in English. For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10.
Decision Statement Syntax • Conditional statement: if…elif…else if x == 1: print “x is 1” elif x == 2: print “x is 2” else: print “x is not 1 or 2” • Colons are used at the end of each condition • Indentation defines what executes for each condition
Looping syntax • While loop x = 1 while x < 10: print x x = x + 1 • Colons used at end of each statement • Indentation defines what executes for the loop
Looping syntax • Counted loop for x in range(1, 5): print x • Counted loops increment and test a variable on each iteration of the loop • The last value is not executed
Looping syntax • List loop x = [1, 2, 3] For a in x: print a • List loops iterate over each value in a list • The loop will execute once for each value in the list
Case sensitive rules • Case sensitive • Functions and statements Correct: max len open print import if Incorrect: Max LEN OpEn imporT IF • Variable names • Not case sensitive • Path names “C:\\DATA” = “c:\\Data” • Geoprocessing properties and methds gp.BUFFER = gp.buffer
Exposing Geoprocessing Functions • An ArcObjects component, the geoprocessor object, is the main vehicle for exposing the geoprocessing functions to scripting in Python • It is an object that provides a single access point and environment for the execution of any geoprocessing tool in ArcGIS, including extensions.
Exposing Geoprocessing Functions • GpDispatch is a COM wrapper for the geoprocessor making it possible for Python to access more than 450 available tools • Example: import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # use Clip_Analysis tool gp.Clip_analysis ("C:\\World\\Cities.shp", "C:\\World.mdb\\Yemen\\Yemen", "C:\\World.mdb\\Yemen\\ClipCitiesScript")
Using COM Objects with Python To use COM objects: import win32com.client o = win32com.client.Dispatch(“Object.Name") Example: import win32com.client o = win32com.client.Dispatch("Excel.Application") o.Visible = 1 o.Workbooks.Add() o.Cells(1,1).Value = "Hello"
Interacting with Geoprocessor • Geoprocessor, like any ArcObject, has properties and methods • Property: characteristic of an object (an adjective) • Method: something the object knows how to do (a verb) • Interact with Geoprocessor through properties and methods • to buffer use Buffer method • to clip use Clip methods • to delete feature class use Delete method
Accessing the Geoprocessor from Python • Tell Python you want to use COM objects import win32com.client • Create the instance of Geoprocessor gp = win32com.client.Dispatch(“esriGeoprocessing.GpDispatch.1”)
Syntax for Properties and Methods • To assign a value to property Object.Property = value gp.Workspace = “C:\\Data” • To get a value of a property Object.Property print “The name of the workspace is “ + gp.Workspace • To use a method Object.Method(argument, argument, …) gp.Buffer_analysis (“Freeways”, “FreewaysBuffer”, 100) - parentheses around arguments - arguments separated by commas
Running Scripts in Python • Use of ArcGIS help system to find: • Usage, command syntax, and scripting examples of standard ArcToolbox tools, • Usage and syntax of Geoprocessor properties and methods, which are only accessible through scripting
Running Python Scripts Three Modes 1) Running scripts in PythonWin 2) Running scripts as script tools in ArcGIS 3) Running scripts as embedded model components in ArcGIS
Running Scripts in Python • Problem with the schema lock • Cannot manipulate with the same file open in ArcGIS and in PythonWin at the same time
Running scripts in PythonWin Example of a simple script to buffer a feature class # Name: Tim Nyerges # Date: January 3, 2007 # Purpose: To buffer a feature class import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1“ gp.Workspace = "C:\\Python_Data\\SanDiego.mdb” gp.Buffer_analysis ("Freeways", "BufferedFreeways", 1000)
Debugging the Code • Test code in PythonWin • A check for syntax errors • Do not test a script from ArcToolbox • Test code in PythonWin, then add it to ArcToolbox
Running scripts in PythonWin • Run the script from the script window of PythonWin • Check the message in the lower left corner of the interactive window in PythonWin: • While the script is running you will see the message “running script <….>.py” • The indication of the successful run is the message “Script <….> returned the exit code 0” • Display the result in ArcCatalog
Running scripts in PythonWin Example of a simple script to clip a feature class # Name: Tim Nyerges # Date: January 3, 2007 # Purpose: To clip a feature class import win32com.client gp =win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") gp.Workspace = "C:\\Python_Data\\SanDiego.mdb" gp.Clip_analysis ("MajorAttractions", "SDdowntown", "SDdowntownAttractions")
Running scripts in PythonWin Example of a simple script to buffer and clip a feature class # Name Tim Nyerges # Date: January 3, 2007 # Purpose: To buffer SD freeways first and then to clip the downtown section # of freeways import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") gp.Workspace = "C:\\Python_Data\\SanDiego.mdb" if gp.Exists(gp.Workspace + "\\bufferedFreeways"): gp.Delete_management(gp.Workspace + "\\bufferedFreeways") gp.Buffer_analysis ("Freeways", "bufferedFreeways", "500") gp.Clip_analysis ("bufferedFreeways", "SDdowntown", "downtownFreeways")
Gene will get you started with hands-on Python in lab session