Geography 465 - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Geography 465

Description:

fc = 'Freeways.shp' Fc = 20000. Variables can hold different data types ... gp.Buffer_analysis ('Freeways', 'bufferedFreeways', '500' ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 44
Provided by: pio758
Category:

less

Transcript and Presenter's Notes

Title: Geography 465


1
Geography 465
  • Analytic Cartography
  • Getting Started with Python Scripting

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

3
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

4
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

5
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

6
PythonWin Interface
  • Script window
  • Write and save code
  • Interactive window
  • Test lines of code
  • Report messages
  • Menus and Toolbars
  • standard and debugging

7
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.G
    pDispatch.1)
  • gp.Workspace C\\Python_Data\\SanDiego.mdb
  • Gp.Buffer_analysis (Freeways, BuffFreeway,
    1000)
  • Can comment and uncomment blocks of code

8
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
9
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

10
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
  • fc0 ---gt S S is in the 0 position
  • fc13 ---gt tr start at 1st, up to not
    including 3rd
  • fc-4 ---gt Streets get rid of the last 4
    charaters

C\SanDiego.mdb\Roads
11
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 fcList1
  • fc2 fcList02 ---gt Roads, Streets
  • fc3 fcList0-1 ---gt Roads, Streets,
    Parcels
  • fc4 fcList2 ---gt Parcels,
    Zipcodes

---gt Streets
12
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

13
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(fcList2, BuffStreams1000,
    distanceValues5)

14
Built-in functions
  • Python has many built-in functions
  • Built-in means that Python automatically makes
    these functions available they dont 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

15
Examples of Built-in functions
  • len ( ) returns the length
  • fc Streams.shp
  • len (fc) ---gt 11
  • max ( ) returns the maximum value
  • xExtent (5210474.99, 7438807.99)
  • max (xExtent) ---gt 7438807.99
  • open ( ) opens a file
  • coord open(C\\Mydata.txt, r).read( )
  • ---gt holds the contents of the Mydata.txt file
  • round ( ) Rounds a number
  • xCoord 5210474.99
  • round(xCoord) ---gt 5210475.0

16
Accessing modules
  • Most functions are imported from modules
  • The math module
  • import math
  • math.sqrt(64) ---gt 8.0
  • math.pi ---gt 3.14156
  • The string module
  • Import string
  • string.split(75.7 -45.3) ---gt 75.7, -45.3
  • string.upper(c\\student) ---gt C\\STUDENT

17
Accessing modules
  • The os.path module
  • import os.path
  • os.path.basename(C\\SanDiego.mdb\\Streets,shp
  • ---gt Streets.shp
  • os.path.dirname((C\\SanDiego\\Streets.shp
  • ---gt C\\SanDiego

18
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

19
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."
20
Defining your own functions with parameters-
example
  • def printTwice(n)
  • print n, n

printTwice(Geog465 project!) ---gt ?
n Geog465 project! printTwice(n)
21
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.

gtgtgt 5 5 1 gtgtgt 5 6 0
22
Boolean Expressions
  • The operator is one of the comparison
    operators
  • the others are
  • x ! y x is not equal to y
  • x gt y x is greater than y
  • x lt y x is less than y
  • x gt y x is greater than or equal to y
  • x lt y x is less than or equal to y

23
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 gt 0 and x lt 10
  • is true only if x is greater than 0 and less
    than 10.

24
Decision Statement Syntax
  • Conditional statement ifelifelse
  • 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

25
Looping syntax
  • While loop
  • x 1
  • while x lt 10
  • print x
  • x x 1
  • Colons used at end of each statement
  • Indentation defines what executes for the loop

26
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

27
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

28
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

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

30
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.G
    pDispatch.1")
  • use Clip_Analysis tool
  • gp.Clip_analysis ("C\\World\\Cities.shp",
    "C\\World.mdb\\Yemen\\Yemen",
    "C\\World.mdb\\Yemen\\ClipCitiesScript")

31
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"

32
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

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

34
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

35
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

36
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

37
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

38
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.G
    pDispatch.1
  • gp.Workspace "C\\Python_Data\\SanDiego.mdb
  • gp.Buffer_analysis ("Freeways",
    "BufferedFreeways", 1000)

39
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

40
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 lt.gt.py
  • The indication of the successful run is the
    message
  • Script lt.gt returned the exit code 0
  • Display the result in ArcCatalog

41
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.Gp
    Dispatch.1")
  • gp.Workspace "C\\Python_Data\\SanDiego.mdb"
  • gp.Clip_analysis ("MajorAttractions",
    "SDdowntown", "SDdowntownAttractions")

42
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.G
    pDispatch.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")

43
Gene will get you started with hands-on Python in
lab session
Write a Comment
User Comments (0)
About PowerShow.com