Introduction to Geoprocessing using Python

Report 8 Downloads 189 Views
Introduction to Geoprocessing using Python

Jorge RuizRuiz-Valdepeña

EdUC 2009 Tech Session

1

Geoprocessing options Command line

Dialog Models

ArcObjects

Scripts EdUC 2009 Tech Session

2

Why write scripts? • Automate a work flow – Copy all incoming data into a geodatabase – Perform clip and buffer operations on 1000 data sets

• Run code at specific dates and times – Windows AT command – Windows scheduler – Every Friday print the map

• Easily distribute code – A script is a selfself-contained, single file

• Free up p time for other important p tasks! • Make yourself more efficient

EdUC 2009 Tech Session

3

Why use Python? • Easy to learn! • Free! • Open Open--source, object object--oriented, scripting language – Can view and modify source code – Support pp for large g projects p j – Easy to use

• • • •

Offers Development environments with debugging tools Cross platform and works in many WebWeb-browsers Ability to compile scripts I t ll d with Installed ith ArcGIS A GIS and d ESRI samples l provided id d

EdUC 2009 Tech Session

4

PythonWin • Integrated development environment or IDE • IDE contains menus, toolbars, and context menus – Windows look and feel – All iin one application li ti – Script tools open in PythonWin

• Script window – Write and save code – Autosave A t b f before each h run

• Interactive window – Test lines of code – Report messages

EdUC 2009 Tech Session

5

Statements • The Th print i t statement t t t • Type print ‘Hello’ in the Script window

• Then Th click li k the th Run R button b tt • Prints Hello in the Interactive window

EdUC 2009 Tech Session

6

Comments • • • •

Comment: A nonnon-executable line of code Helps you remember Helps others decipher Lets you add notes – One pound sign (#) for green and italicized – Two pound signs (##) for grey

# # # #

Date: August 3, 2008 P Purpose: T To b buffer ff a f feature t class l This code will contain … This code was written by …

EdUC 2009 Tech Session

7

Variables • Variables are dynamically typed – No declaration keyword – No type assignment – Make up a name and set it equal to a value

fc = “C: C:\ \\Data Data\ \\SanDiego SanDiego\ \\Streets.shp Streets.shp””

• Variables are case sensitive scale = 10000 Scale = 20000

Two different variables

• Variables can hold different data types – Strings, numbers, lists, files, and more

EdUC 2009 Tech Session

8

Decisions • Testing conditions

if x == 1: print "x is 1" elif x == 2: print "x x is 2" 2 else: print "x is not 1 or 2" • Colons used at end of each condition • Indentation defines what executes for each condition – Python y automatically y indents when y you p press Enter – Use tabs or spaces, must be consistent

• Two equal signs for conditions, one for assignment

EdUC 2009 Tech Session

9

Loops • While x = 5 while x < 10:

print x x = x + 1

• Colons end each statement • Indentation defines what executes

EdUC 2009 Tech Session

10

Loops • While x = 5 while x < 10:

print x x = x + 1

• Counted for x in range(1,5): print x

EdUC 2009 Tech Session

11

Loops • While x = 5 while x < 10:

print x x = x + 1

• Counted for x in range(1,5): print x

• List x = [1 [1, 2 2, 3] for a in x: print a

EdUC 2009 Tech Session

12

Common ArcObjects • ArcGIS is built with a set of ArcObjects Application Map Document Feat re Feature

Map

Layer

EdUC 2009 Tech Session

13

More ArcObjects • Scripting doesn’t get to User Interface objects • Which objects can you get to?

Selection

Symbol

Field

Row

Feature Class EdUC 2009 Tech Session

14

Object Model Diagram • Map of geoprocessing objects, properties, and methods

EdUC 2009 Tech Session

15

Object Model Diagram • Map of geoprocessing objects, properties, and methods

EdUC 2009 Tech Session

16

Syntax for properties and methods • Set a property’s value Object.Property = Value gp Workspace = "C:\ gp.Workspace "C:\\temp"

EdUC 2009 Tech Session

17

Syntax for properties and methods • Set a property’s value Object.Property = Value gp Workspace = "C:\ gp.Workspace "C:\\temp"

• Get a property’s value x = Object.Property x = gp.Workspace k print "The workspace name is " + x

EdUC 2009 Tech Session

18

Syntax for properties and methods • Set a property’s value Object.Property = Value gp.Workspace = "C:\ "C: C:\ C: \\temp temp"

• Get a property’s value x = Object.Property x = gp gp.Workspace p print "The workspace name is " + x

• Use a method Object.Method (arg, arg, arg, arg, …) gp.Buffer_analysis (fc fc, , " "C: C:\ \\temp temp\ \\buff.shp", buff.shp", 100)

• Parentheses around arguments • Arguments separated by commas

EdUC 2009 Tech Session

19

Create a geoprocessor object in code • Geoprocessor can be used in any COM language – Component object model language – Perl, Perl VBScript VBScript, JScript JScript,, Python, Python VBA VBA, VB VB, and C# C#

• arcgisscripting module

You access all geoprocessing functionality y through g this variable

– ESRI ESRI--written – Cross Cross--platform

• 9.3 Argument – accept p and return common Python y structures such as lists and Booleans

EdUC 2009 Tech Session

20

Create a geoprocessor object in code in previous versions

• The Python win32com module must be loaded using the Import command. command – This module enables the COM IDispatch communication within Python.

You access all geoprocessing functionality through this variable

EdUC 2009 Tech Session

21

The Buffer tool • Syntax Buffer_analysis (in_features in_features, , out_feature_class, out_feature_class, buffer distance or field, line_side buffer_distance_or_field, buffer_distance_or_field line side, line_end_type line_side, line end type, line_end_type, dissolve_option, dissolve_option , dissolve_field) dissolve_field)

• Example gp.Workspace gp Workspace = "C:\\Database Database\ \\World World" " gp.Toolbox = "Analysis" gp.Buffer("Lakes.shp", gp.Buffer ("Lakes.shp", "BuffLakes.shp", "100 feet feet") ")

• Notes – If units not specified, input feature class units used – If specifying units, make the argument a string

EdUC 2009 Tech Session

22

Syntax If I want to use the UNION tool, how would I know that the inputs are separated by semicolons?

1. ArcGIS Desktop Help 2. If help is not detailed enough, export the tool from a model to a script

EdUC 2009 Tech Session

23

Multiple tools • Run many tools in succession • Use one tool’s output as another’s input # Find a location for a new hotel that must be > 10000 ft # from an existing one and within 2000 ft of a freeway.

gp.Workspace = "C:\\Database Database\ \\SanDiego.mdb SanDiego.mdb" " # B Buffer ff F Freeways

gp.Buffer_analysis("Freeways", gp.Buffer_analysis ("Freeways", " "BuffFreeways BuffFreeways", ", 2000) # Select the Inns

gp.Select_analysis gp gp.Select_analysis( Select analysis(" analysis( ("MajorAttractions MajorAttractions", MajorAttractions ", , "HolidayInns HolidayInns", ", "[NAME] LIKE 'HOLIDAY INN*'") # Buffer the Inns

gp.Buffer_analysis(" gp analysis y ("HolidayInns HolidayInns", y ", "BuffHolidayInns "BuffHolidayInns", y ", 10000) # Erase the buffered Inn areas from the Freeways buffer

gp.Erase_analysis(" gp.Erase_analysis ("BuffFreeways BuffFreeways", ", " "BuffHolidayInns BuffHolidayInns", ", "SuitableAreas SuitableAreas") i bl ") )

EdUC 2009 Tech Session

24

Cursors • Get or edit rows and values in a table • SearchCursor reads values in a row • UpdateCursor makes changes to row values and deletes rows • InsertCursor is used to insert new rows

EdUC 2009 Tech Session

25

Geometry cur = gp.SearchCursor ("Climate") row = cur.Next cur.Next() () while row: geom = row.Shape print geom.Area print geom.Extent row = cur.Next cur.Next() () cur cu

row

geom

EdUC 2009 Tech Session

26

Resources • Web site: www.python.org – Tutorials, Documentation, Forums

• Python books – Learn to Program Using Python, Python, by Alan Gauld – Learning g Python y ((2nd Edition), ), by y Mark Lutz and David Ascher – Python Essential Reference (2nd Edition), by David M. Beazley – The Quick Python Book, Book, by Daryl Harms and Kenneth McDonald – Python Programming on Win32, Win32, by Mark Hammond and Andy Robinson

EdUC 2009 Tech Session

27

Get a free 45 45--minute hands hands--on lesson at the Hands--On Learning Center Hands Topics include: • Introduction to ArcGIS Desktop • Creating C ti a Map M In I ArcGIS A GIS • Basics of the Geodatabase Model • and more Location: ESRI Showcase

EdUC 2009 Tech Session