Working with Geoprocessing Tools in Scripts - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Working with Geoprocessing Tools in Scripts

Description:

... contains the character the operating system uses to separate pathname components. Example of Building Path Name Strings. import os ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 22
Provided by: peopleOre
Category:

less

Transcript and Presenter's Notes

Title: Working with Geoprocessing Tools in Scripts


1
Working with Geoprocessing Tools in Scripts
  • Geo 599
  • Week 5, Oct. 26, 2009

2
Topics for today
  • Strings as object names
  • Workspaces
  • Calling geoprocessing tools in scripts
  • Listing data

3
Strings as Object Names
  • Most complex ArcGIS objects are referred to by
    their names
  • Names are represented as strings
  • Full object is attached to name behind the scenes
  • Name strings can be manipulated just like any
    other string
  • Most generally, names include the full path

4
Path name strings
  • Full path name includes drive and all folder
    levels
  • Feature data sets are treated like folders

5
Escaping Characters
  • Not all characters can be simply included in
    strings
  • Special characters and whitespace are escaped
  • Python uses a backslash (\) to escape
  • Backslash is stripped and potentially problematic
    character following it remains (or is converted)
  • Common escape sequences
  • Backslash \\
  • Double quote \
  • New line \n
  • Tab \t

6
Building Path Name Strings, os.sep
  • Often want to access several items in the same
    root path
  • Store root path in a variable
  • Concatenate with each item
  • os (operating system) module contains sep data
    item
  • os.sep contains the character the operating
    system uses to separate pathname components

7
Example of Building Path Name Strings
  • import os
  • GeoDB C\\Geo599_Python\\Geo599_Python_ExData.g
    db
  • MidwestDS GeoDB os.sep MetRegions_SC1_MW
  • SouthDS GeoDB os.sep MetRegions_SC1_S

8
The Workspace
  • Geoprocessor object has workspace attribute
  • Workspace serves as default root path
  • Objects without full path specified are assumed
    have the workspace as their root
  • Operations on a folder act on the workspace
  • Workspace may be changed during a script

9
Example of using workspace
  • import arcgisscripting
  • gp arcgisscripting.create(9.3)
  • gp.workspace C\\Geo599_Python\\Geo599_Python_E
    xData.gdb
  • MidwestDS MetRegions_SC1_MW
  • SouthDS MetRegions_SC1_S

10
Pair and Compare
  • Object and Path name strings
  • Workspaces

11
Geoprocessing Tools
  • Geoprocessing tools are methods of the
    geoprocessor object
  • Method exists for each tool in ArcToolbox
  • For each tool ArcGIS help includes
  • Tools script syntax
  • Description and data type of each parameter
  • Script example

12
Toolboxes
  • Toolbox organization is the same as seen in
    ArcToolbox
  • Toolboxes containing tools used in a script must
    be added
  • gp.AddToolbox(ltfull path location of toolboxgt)
  • When using tools, toolbox must be specified

13
Specifying Toolboxes
  • As part of the tools name, after an underscore
  • gp.ltToolgt_ltToolboxgt(ltparametersgt)
  • Toolbox name is included in tool syntax in help
  • By setting the Toolbox attribute of the
    geoprocessor
  • gp.Toolbox ltToolboxName as stringgt
  • Tools in the current toolbox can be accessed with
    just the tools name

14
Geographic objects as parameters
  • Most tools take geographic objects as input
    and/or output
  • Parameter used is the string naming the object
  • If string does not include full path, object
    assumed to be in current workspace
  • Can always specify full path
  • String/object often stored in a variable to
    simplify call to tool

15
Optional Parameters and Defaults
  • Some parameters of tools are optional
  • Optional parameters have default values
  • A pair of double quotes () means use the
    default
  • Default values are indicated in parameter
    descriptions
  • Parameter order matters!
  • Any optional parameters you want to specify must
    follow the correct number of default place holders

16
Explore some tools
  • Look at the help files for a few geoprocessing
    tools
  • Scripting information is at the end of the help
    file
  • Pay attention to
  • Toolbox name
  • Parameter data types
  • Required/optional parameters
  • Default values

17
Listing Data
  • Geoprocessor contains several List methods
  • ListDatasets
  • ListFeatureClasses
  • ListFields
  • ListTables
  • Each returns a Python list
  • Batch processing done by iterating over list
  • ListWorkspaces
  • ListIndexes
  • ListRasters
  • Most List methods act on the current workspace

18
Wildcards
  • List methods allow specification of wildcards
  • Wildcards serve as a filter
  • Only the objects with names matching a specified
    pattern will be included in the list
  • Wildcards specified as a string including an
    asterisk
  • Asterisk () stands for any string

19
Example Listing Feature Classes
  • gp.workspace GeoDB os.sep
    MetRegions_SC1_MW
  • Center_FCs gp.ListFeatureClasses(Ctr)
  • for fc in Center_FCs
  • ltprocessing stepsgt
  • How many elements will the list have?
  • What will they be?

20
Exercise 3 Batch Processing
  • Part A (10 pts.) Export the model you created in
    exercise 1 as a script and examine it
  • Part B (30 pts.) Add comments to
    MetroRegMultiClip.py
  • Some already included for things we havent
    covered yet
  • Script sections separated by blank lines
  • Each section should have a comment
  • Dont forget a header

21
Exercise 3 (cont.)
  • Part C (50 pts.) Write a batch processing script
  • Create new regional boundaries by buffering the
    Urbanized Area for each metro region in the
    course geodatabase
  • Pseudocode provided
  • Can borrow from MetroRegMultiClip.py, especially
    for things we havent covered yet
  • Remember to comment your script
  • Exercise 3 due next week (Nov. 2)
Write a Comment
User Comments (0)
About PowerShow.com