Geography 465 Analytic Cartography: Getting Started with Python Scripting.

43
Geography 465 Analytic Cartography: Getting Started with Python Scripting
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    1

Transcript of Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Page 1: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Geography 465Analytic Cartography:

Getting Started with Python Scripting

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

Page 3: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 4: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 5: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 6: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

PythonWin Interface

• Script window– Write and save code

• Interactive window– Test lines of code– Report messages

• Menus and Toolbars– standard and debugging

Page 7: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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 classimport win32com.clientgp =

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

Page 8: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Variables in Python

• Variables are dynamically typed– No declaration required– No type assignment required fc = “C:\\ProjectData\\SanDiego.mdb\\Freeways.shp”

• Variables are case sensitivefc = “Freeways.shp” Fc = 20000

• Variables can hold different data types– strings, numbers, lists, files

Two different variables

Page 9: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Strings

• Variables can hold strings folder = “c:/Student”

• Strings are surrounded in double (“) or single (‘) quotes

• Pathnames use two back (\\) or one forward (/) slashOne backslash (\) is a reserved escape

character and a line continuation character

Page 10: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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 rightfc = “Streets.shp”fc[0] ---> “S” # S is in the 0 positionfc[1:3] ---> “tr” # start at 1st, up to not including 3rdfc[:-4] ---> “Streets” # get rid of the last 4 charaters

C:\SanDiego.mdb\Roads”

Page 11: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Numbers and lists• Variables can hold numbers and expressions

num1 = 1.2 num2 = 3 + 5

• Variables can hold listsnumList = [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”

Page 12: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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 lettersinputFC = “Streets.shp”

• Avoid special characters (for example / \ & % # !)

• Use descriptive variable names

Page 13: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Line continuation• Line continuation characters

– Parentheses ( ), brackets [ ], and braces { }– Backslash \

• Indentation is automaticfcList = [“Roads”, “Climate”, “Streams”,

“Zipcodes”, “Coastlines”]

distanceValues = 100, 200, 300, 400, 500, \1000, 1500

gp.Buffer_analysis(fcList[2], BuffStreams1000, distanceValues[5])

Page 14: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 15: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Examples of Built-in functions

• len ( ) – returns the lengthfc = “Streams.shp” len (fc) ---> 11

• max ( ) – returns the maximum valuexExtent = (5210474.99, 7438807.99)max (xExtent) ---> 7438807.99

• open ( ) – opens a filecoord = open(“C:\\Mydata.txt”, “r”).read( )---> holds the contents of the Mydata.txt file

round ( ) – Rounds a numberxCoord = 5210474.99round(xCoord) ---> 5210475.0

Page 16: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Accessing modules

• Most functions are imported from modules– The math module

import math

math.sqrt(64) ---> 8.0

math.pi ---> 3.14156

– The string moduleImport string

string.split(“75.7 -45.3”) ---> [’75.7’, ‘-45.3’]

string.upper(“c:\\student”) ---> ‘C:\\STUDENT’

Page 17: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Accessing modules

– The os.path moduleimport os.path

os.path.basename(“C:\\SanDiego.mdb\\Streets,shp”

---> ‘Streets.shp’

os.path.dirname((“C:\\SanDiego\\Streets.shp”

---> ‘C:\\SanDiego’

Page 18: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 19: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Defining your own functions - exampledef newLine():print

print "First Line."newLine()print "Second Line."

def threeLines():newLine()newLine()newLine()

print "First Line."threeLines()print "Second Line."

Page 20: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Defining your own functions with parameters- example

def printTwice(n):print n, n

printTwice(“Geog465 project!”)---> ?

n = “Geog465 project!”printTwice(n)

Page 21: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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 == 51>>> 5 == 60

Page 22: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Boolean Expressions

The == operator is one of the comparison operators; the others are:

x != y # x is not equal to yx > y # x is greater than yx < y # x is less than yx >= y # x is greater than or equal to yx <= y # x is less than or equal to y

Page 23: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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.

Page 24: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Decision Statement Syntax

• Conditional statement: if…elif…elseif 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

Page 25: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Looping syntax

• While loopx = 1

while x < 10:print xx = x + 1

• Colons used at end of each statement• Indentation defines what executes for the loop

Page 26: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Looping syntax

• Counted loopfor 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

Page 27: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Looping syntax

• List loopx = [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

Page 28: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Case sensitive rules

• Case sensitive– Functions and statementsCorrect: 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 methdsgp.BUFFER = gp.buffer

Page 29: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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.

Page 30: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 31: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Using COM Objects with Python

To use COM objects:import win32com.cliento = win32com.client.Dispatch(“Object.Name")

Example:import win32com.cliento =

win32com.client.Dispatch("Excel.Application")o.Visible = 1o.Workbooks.Add()o.Cells(1,1).Value = "Hello"

Page 32: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 33: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Accessing the Geoprocessor from Python

• Tell Python you want to use COM objectsimport win32com.client

• Create the instance of Geoprocessorgp = win32com.client.Dispatch(“esriGeoprocessing.GpDispatch.1”)

Page 34: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Syntax for Properties and Methods

• To assign a value to propertyObject.Property = value

gp.Workspace = “C:\\Data”

• To get a value of a propertyObject.Property

print “The name of the workspace is “ + gp.Workspace

• To use a methodObject.Method(argument, argument, …)

gp.Buffer_analysis (“Freeways”, “FreewaysBuffer”, 100)

- parentheses around arguments

- arguments separated by commas

Page 35: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 36: Geography 465 Analytic Cartography: Getting Started with Python 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

Page 37: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 38: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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)

Page 39: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 40: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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

Page 41: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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 classimport win32com.clientgp =win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")gp.Workspace = "C:\\Python_Data\\SanDiego.mdb"gp.Clip_analysis ("MajorAttractions", "SDdowntown",

"SDdowntownAttractions")

Page 42: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

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 freewaysimport win32com.clientgp = 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")

Page 43: Geography 465 Analytic Cartography: Getting Started with Python Scripting.

Gene will get you started with hands-on Python in lab session