Working with Geoprocessing Tools in Scripts by nye15450


									       Working with
Geoprocessing Tools in Scripts

                     Geo 599
                Week 5, Oct. 26, 2009
Topics for today

•   Strings as object names
•   Workspaces
•   Calling geoprocessing tools in scripts
•   Listing data
Strings as Object Names

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

• Full path name
  includes drive and all
  folder levels
• Feature data sets are
  treated like folders
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
Building Path Name Strings, os.sep

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

import os
GeoDB = “C:\\Geo599_Python\\Geo599_Python_ExData.gdb”
MidwestDS = GeoDB + os.sep + “MetRegions_SC1_MW”
SouthDS = GeoDB + os.sep + “MetRegions_SC1_S”
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
Example of using workspace

import arcgisscripting
gp = arcgisscripting.create(9.3)
gp.workspace =
MidwestDS = “MetRegions_SC1_MW”
SouthDS = “MetRegions_SC1_S”
Pair and Compare
Object and Path name strings
Geoprocessing Tools

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

• Toolbox organization is the same as seen in ArcToolbox
• Toolboxes containing tools used in a script must be
  gp.AddToolbox(<full path location of toolbox>)
• When using tools, toolbox must be specified
Specifying Toolboxes

• As part of the tool‟s name, after an underscore:
  – Toolbox name is included in tool syntax in help
• By setting the Toolbox attribute of the geoprocessor:
    gp.Toolbox = <ToolboxName as string>
  – Tools in the current toolbox can be accessed with just the
    tool‟s name
Geographic objects as parameters

• Most tools take geographic objects as input and/or
• 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
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
• Parameter order matters!
              • Any optional parameters you want to
                specify must follow the correct number
                of default place holders
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
Listing Data

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

• 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”
Example: Listing Feature Classes

gp.workspace = GeoDB + os.sep + “MetRegions_SC1_MW”
Center_FCs = gp.ListFeatureClasses(“*Ctr”)
for fc in Center_FCs:
  <processing steps>

• How many elements will the
  list have?
• What will they be?
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
   –   Some already included for things we haven‟t covered yet
   –   Script sections separated by blank lines
   –   Each section should have a comment
   –   Don‟t forget a header
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
  – Pseudocode provided
  – Can borrow from, especially for
    things we haven‟t covered yet
  – Remember to comment your script
                • Exercise 3 due next week (Nov. 2)

To top