ESRIGrootens

Download Report

Transcript ESRIGrootens

Introduction to
Geoprocessing in
ArcGIS 9
Jason Grootens
ESRI-Minneapolis
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Workshop objectives

Introduce Geoprocessing at ArcGIS 9: New Framework
 Dialogs
(Tools)
 Modelbuilder
 Command
Line
 Scripting

Ask questions and participate in discussions
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-2
What is ArcGIS?
ArcGIS clients
ArcReader
E
X
T
E
N
S
I
O
N
S
Components
ArcGIS Desktop
ArcGIS Engine
ArcInfo
Custom
application
ArcEditor
ArcView
E
X
T
E
N
S
I
O
N
S
Web
browser
ArcPad
ArcObjects
Network
ArcGIS Server
Application/Data
servers
ArcIMS
ArcSDE
RDBMS
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-3
What is geoprocessing?

Perform a variety of geographic based tasks
CAD
Projections
GDB
Conversion
Database
Spatial
Analysis
Data
Management
Proximity?
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-4
Geoprocessing answers spatial questions
Census Block Groups
Distance to Roads
Land Cost
Distance to competitors
Where is the best
location for a store?
What is the closest fire
hydrant to each building?
What is
the
fastest
route over
rugged
terrain?
How many people are within
a contamination zone?
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-5
Geoprocessing in ArcGIS

ArcGIS 9 enhances geoprocessing

Hundreds of tools available

Wide variety of methods for accessing geoprocessing
tools

Geoprocessing is what makes a GIS
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-6
Geoprocessing at ArcGIS 9

Geoprocessing available with ArcGIS 9
Command Line
Models
Scripts
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-7
Accessing the ArcToolbox window

A dockable window with toolboxes
 Obtained
in ArcMap, ArcCatalog, ArcGlobe, or ArcScene
 Tree
view in
ArcCatalog
 No
longer a
separate
application
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-8
Toolsets and tools

Toolbox: Container for tools and toolsets

Toolset: Logical container of tools and other toolsets

Tool: Single geoprocessing operation
 System
 All
tool, model, script, custom built tool
tools look and act the same
Toolbox
Toolset
Tool
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-9
Executing tools

Double-click any tool to
specify parameters

Messages appear in
progress dialogs and
Command Line window
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-10
Overview of toolboxes
Core toolbox
*Ability to
create your
own toolboxes
Analysis
Geoprocessing for feature classes
Conversion
CAD tools, conversion tools
Data Management
Field and workspace tools
Coverage – *Workstation install
Geoprocessing for coverages
Linear Referencing
Creating routes, locations
Geocoding
Building locators, table matching
Cartography
Masking annotation, features
Spatial Statistics
Analyzing patterns, distributions
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Extensions
3D Analyst
TIN tools
Geostatistical Analyst
Convert to GRID tool
Spatial Analyst
All tools currently in Spatial Analyst
What's New in ArcGIS 9
1-11
Tools and licensing

Ability to use ArcToolbox, command line, models,
scripts included at all license levels

The amount of tools vary depending on
license/extensions
 ArcView
= 102 tools
 ArcEditor
 ArcInfo
 Spatial
 3D
= 104 tools
= 251 tools
Analyst extension = 158 tools
Analyst extension = 45 tools
 Geostatistical
 Total
Analyst = 1 tool
of 455 tools with ArcInfo and all extensions
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-12
Toolbox tabs

Favorites
 Most
frequently used
tools

Index
 Alphabetical
listing of
all tools

Search
 Search
tools by name
or keyword
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-13
Supported data types

At ArcGIS 8.x, ArcToolbox worked primarily with coverages

At ArcGIS 9, ArcToolbox works with all data types used in
ArcGIS
Shapefile
Coverage Geodatabase
Tables
CAD
Layer files
Raster
Behavior
Geoprocessing
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-14
Accessing geoprocessing functionality
Command Line
ArcToolbox
Execute as dialogs
Simple operations
Simple execution
Shortcut to tools
Scripts
Model Builder
ArcObjects
Batch processing
Branching
Graphical Documentation
Chain tools into one process
Use tools with
ArcObjects
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-15
Creating your own toolbox

Used to store models, scripts, custom tools, or a shortcut to
frequently used tools

My Toolboxes



Created in ArcToolbox or Catalog Tree

Stored as .tbx in user profile
Folder

Created in Catalog Tree

Stored as .tbx in folder
Geodatabase (personal or
enterprise)

Created in Catalog Tree

Stored as table in GDB
Stored in geodatabase
Stored as .TBX file
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-16
Creating your own tools

Three ways of creating a tool
 ModelBuilder
 Script
 Custom-built

tool with ArcObjects
All tools act and behave
the same
 Execute
as a dialog
 Accessing
documentation
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-17
Using and creating models

Drag and drop environment

Allows processing of multiple tools at once

Documentation of process

Easy-to-change parameters and re-execute model
Project data
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Tool
Derived data
What's New in ArcGIS 9
1-18
Using and creating tools with scripts

Use any language with a COM interpreter
 Python,
VBScript, JScript, Perl
 Geoprocessor
also accessible in programming languages
 VBA, VB, C# , etc
#For each shapefile in the list
fc = fcs.next()

Batch processing

Branching based
upon conditions
outname = outworkspace + "\\" + fc

Delayed processing
#Clip each feature class in the list

Can run outside
of ArcGIS
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
while fc:
#Give output fc the name of the input
gp.Clip_analysis(fc, clipfc, outname)
fc = fcs.next()
What's New in ArcGIS 9
1-19
Using tools at the command line

Run a tool from the interface without a dialog

Shortcut for advanced users

Similar to ArcInfo Workstation
Clip_analysis <in_features> <clip_features> <out_featureclass>
Clip_analysis county_censusblocks citylimit city_censusblocks
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-20
Using and creating tools with ArcObjects

All geoprocessing tools can be called as COM components

Create custom tools (.dll)
Geoprocessing
Object Model Diagram
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-21
Environment Settings

Common parameters that can be applied to many tools
within a geoprocessing session

Settings persist through all tools (models, scripts,
custom tools)
General settings
Current workspace
Raster settings
Output spatial reference
Cell size
Extent
Mask
Geodatabase settings
Geodatabase raster
X/Y Domain
Statistics
Precision
Compression
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-22
Saving environment settings

Where environment settings get saved
 Normal.gxt
A

for ArcCatalog
map document (.mxd) or template (.mxt)
Save settings into an XML file to load into other
sessions
 Favorites
tab
 Environment
 Command
 Save
settings
line variables
settings to default
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-23
Using ArcGIS 9
ModelBuilder
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Modeling spatial problems

Models help understand and solve complex problems
 Models
simplify reality
 Models
combine geographic layers to answer questions
 Where should we build our next store?
Reality
GIS layers
Suitability for store
Model criteria:
- Zoned commercial
- Near target population
- Away from competition
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-25
Types of models

Repetitive tasks
 Minimize
grunt work
 Efficiently
execute a series of
frequently used tools
GIS data layers

Suitability models
 Use
to find best location
(businesses, vineyards, evacuation centers)

Best store location
Process models
 Show
the landscape as conditions change
(fire spreads, rivers flood, oil slicks move)
Filling a reservoir
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-26
Why use ModelBuilder?

Fast analysis; efficient processing

Re-execute the same model, slightly changing
parameters to see how end results differ

Complex models

Graphical documentation of work
Project data
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Tool
Derived data
What's New in ArcGIS 9
1-27
Using ModelBuilder

Obtained from a toolbox

Create new models in your own toolboxes

All tools within ArcToolbox can be used in ModelBuilder
Existing model
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-28
Model elements

Project data: Data that exists before model is run


Tool: Operation performed on input data


Yellow-orange rectangle
Derived Data: Output data created by a function


Blue oval
Model
Green oval
Process
Process: Set of elements

Run one process at a time
Project
data
Tool
Derived
data
Process
or all at once
Tool
Process
Project
data
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Tool
Derived
data
Derived
data
What's New in ArcGIS 9
1-29
Adding model elements

Drag and drop from ArcCatalog or ArcMap
 Tools
from ArcToolbox
 Data

Add empty variables
 Supply
data source at a later time
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-30
Tools within a model

Right-click or double-click to obtain parameters
 Same
dialog as tools from a toolbox
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-31
Three states of model elements

Not ready to run: Parameters not defined

Ready to run: All elements colored

Already run: All elements colored and shadowed
Not Ready to Run
Ready to Run
Already Run
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-32
How to execute models

Entire model from ModelBuilder
 Can

execute one process at a time from ModelBuilder
As a tool dialog
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-33
Parameters

Input/Output data and values for a tool

Used for running model as dialog

Right-click model element and choose to create
parameter
Data and location
Project
ProjectData
data
Input data
Function
Tool
Buffer distance
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Output name and location
Derived
DerivedData
data
Cell size
What's New in ArcGIS 9
1-34
Running models

All parameters created in model appear in model dialog
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-35
Validating a model

Validating a model
 Returns
model elements to
ready-to-run or
not ready-to-run states
 Validate
to determine if
model is invalid

Invalid models
 Parameter
 Tools
 COM
values no longer valid (referencing non-existent data)
referenced no longer exist
tool inside the model is unregistered
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-36
Intermediate data

You determine which derived data elements are
intermediate

Can delete after model has been executed
 Save
disk space
Intermediate data
Roads
Buffer
Roads
buffer
Erase
Land use
Select
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Best
sites
Suitable
sites
What's New in ArcGIS 9
1-37
Navigating the model
Auto Layout
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Navigate tool
Add Connection
What's New in ArcGIS 9
1-38
Model diagram properties

Use grids for better control in placing elements

Change flow orientation

Change the color, shape, and font
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-39
Adding labels

Give the model canvas more detail

Diagram labels
 Free

floating labels
Element labels
 Maintain

position relative to model element
Connector labels (tool or data)
 Maintain
position relative
to connector
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-40
Exporting models


Export to a graphic
 Export
as .bmp, .jpg, .emf
 Useful
for placing in map layouts
Printing models
 Add
borders, captions, page
numbers
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-41
Tool documentation

Ability to edit documentation for tools and toolboxes
 Documentation
built into
ArcGIS online help system

Ability to create metadata
 Useful
for Metadata server
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-42
Model Report

XML document available for
viewing or creation

Useful for historical view
of processes executed

Holds information on
 Variables
 Processes
 Messages
 Time
and date of execution
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-43
Saving and sharing models

Model is saved to .tbx file or geodatabase
 Give
others .tbx or geodatabase to share model
 Set parameters if used with different data
 Export
model as a script
Windows
Explorer
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-44
Example Model

Find suitable area for airport based upon available land
and distance from existing airports
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-45
ModelBuilder
Demo
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Using the
Command Line window
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Command Line window

Available in all ArcGIS applications

Holds messaging for all tools executed

Similar interface to ArcInfo Workstation
 Commands
and arguments may be different
ArcInfo Workstation 7.x
ArcGIS 9
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-48
Why use command line?

Saves time if familiar with geoprocessing tools
 Allows
you to obtain quick view of all tools available

Execute scripts and models

Shortcut to tools in ArcToolbox
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-49
What can be accomplished at command line?

Executing tools
select soils48 C:\undrained.shp "OTHERPH" = 'UNDRAINED'

Environment settings
CellSize 50
Workspace C:\Arc9output\OrangeCounty.mdb

Create variables
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-50
Syntax notation

Similar to ArcInfo Workstation and Spatial Analyst
ToolName <Argument> {Argument} {KEYWORD|KEYWORD}
Required
Optional
Optional Keywords

First optional keyword will be taken as default

Use # to skip arguments

Allows multi-line commands
 Shift
+ Enter
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-51
Passing in parameters

Usage appears showing all parameters

Dropdown list shows
available layers in
ArcMap TOC

Type in name of command and press Enter
 Usage
appears in Command Line window
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-52
Messages

Successful completion of a tool
 Includes

Start and End Time
Errors (red font)
 Indicates

a tool did not execute
Warnings (green font)
 Indicates
that a step may have been skipped; gives usage
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-53
Editing and re-executing existing processes

Allows to edit parameters or re-execute without
changing
 If
source of layers changes
 Change
 Edits
a parameter (buffer distance)
are made (new features added)
 Selection
changes
*Can also use up and down arrows
on keyboard for previous commands
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-54
Adding a toolbox to ArcToolbox

Only tools in ArcToolbox recognized in command line

In ArcToolbox
 Browse

for toolboxes
In ArcCatalog
 Add
any toolbox by right-clicking
 Copy
and paste into ArcToolbox
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-55
Variable manager

Make variables for parameters
 Data
and workspaces
 Expressions
 Reclassification
 Spatial
reference
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-56
Accessing data and tools

Some tools can have the same name
 ToolName_ToolboxAlias
Buffer_analysis infeatures outfeatures
or
Buffer_arc infeatures outfeatures
Core Toolbox
Analysis =
Analysis
Conversion =
Conversion
Data Management =
Coverage =
Management
Arc
Geocoding =
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Geocoding
Extensions
3D Analyst = 3D
Spatial Analyst = SA
What's New in ArcGIS 9
1-57
Adding a toolbox alias

Add aliases to your toolboxes
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-58
Executing a model from command line

Toolbox containing model must be in ArcToolbox

Execute model by calling model name, not the label
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-59
Command Line
Demo
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Scripting and the
Geoprocessor
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Why write scripts for geoprocessing?

Similar advantages that models have

Efficiently execute series of different tasks

Easy to read and document

Easy to share

Perform batch operations

Use logic to control tool execution

Interface with other systems

Self contained (single file)

Run any time

Familiar environment for AML and Avenue users

Users don’t have to learn a proprietary language
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-62
GpDispatch

An ArcObject accessible in all COM languages/scripts

Also referred to as the Geoprocessor

Exposes all geoprocessing functionality
 Object
is late-bound (no code completion)
VBScript
Geoprocessor
Access to all
geoprocessing tools
Buffer
Clip
AddField
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Python
Communication
through
IGPDispatch
JScript
Programming
Languages
What's New in ArcGIS 9
1-63
Where to write scripts?

Choice of scripting languages: Any language with a
COM interpreter
VBScript
JScript
Based on VB: Simpler
Syntax similar to C/Java
Popular Web language
Works in many Web
browsers
Python
Perl and other scripting
languages
Flexible, powerful
Easy to learn
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-64
Why use Python?

Flexible, easy scripting language

Object-oriented

Ability to compile scripts

Offers a debugging environment

Modular: Can be broken apart

Cross platform

Works in many Web browsers

It is FREE

ESRI samples provided
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Python
script
What's New in ArcGIS 9
1-65
Installing Python for geoprocessing (Windows)

Included with ArcGIS 9

Also a free download

Python Foundation (www.python.org)

Includes command line, code editor, and documentation
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-66
Writing scripts

Import COM client support
import win32com.client

Instantiate the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GPDispatch.1")

Set properties (e.g., where processing will occur?)
gp.workspace = "c:\\Florida.mdb"

Comment code
# Buffer roads by 100 meters

Run tools
gp.Buffer_analysis("roads", "rdbuf100", "100")
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-67
Constructing Python statements

Use the Geoprocessor with object-oriented
programming

Assign a value to an environment setting with
Geoprocessor.EnvironmentSetting = Value
gp.Workspace = "c:\\temp"

Call tools with Geoprocessor.ToolName (parameter1, ..,
parameterN)
gp.CreateFolder("c:\\Florida", "Scripts")
gp.Clean("Europe", "#", "#", "#", "POLY")
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-68
Referencing data in Python

Use \\ or / for path names
 Python

is based upon the C programming language
Defined by double (") or single (') quotes
gp.workspace = "c:\\Florida"
gp.CreateFolder('c:\\Florida', 'Scripts')

Concatenate strings
outputfc = gp.workspace + "\\" + inputfc
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-69
Referencing tools and toolboxes

Only system toolboxes available by default from
Geoprocessor
 Analysis,
 Use
coverage, data management, etc.
toolbox alias if tools have the same name
gp.Buffer_analysis(roads, Out_Buff, "10 Meters", "FULL")

Add your own toolbox
gp.AddToolbox("c:\\MyProject\\CrossCountryMobility.tbx")
gp.BestPath("start.shp", "destination.shp", "results.shp")
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-70
Example script
# Import COM support
import win32com.client
# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
roads = "C:/Data/Florida/ROADS.shp"
Out_Buff = "C:/Data/Florida.mdb/ROADS_Buffer"
citylimit = "C:/Data/Florida/citylimit.shp"
Out_Clip = "C:/Data/Florida.mdb/BUFF_Clip"
# Process: Buffer...
gp.Buffer_analysis(roads, Out_Buff, "10", "FULL", "ROUND")
# Process: Clip...
gp.Clip_analysis(Out_Buff, citylimit, Out_Clip)
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-71
Running scripts in different environments

Check syntax and run in PythonWin


ArcGIS applications can be closed
Run at DOS prompt
Python C:\PythonScripts\RoadBuffer.py

Create a tool

Add a script to a toolbox

Open command runs script tool

Edit command opens script
source in text editor
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-72
Distributing scripts

As Python source file .py

As script tools in toolboxes
 User
creates script tool for parameters
 Share
the .py file and toolbox
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-73
Batch processing and
advanced scripting
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Batch processing

Scripts are ideally suited for batch processing

Example: Clip all shapefiles in a folder to a boundary
Batch processing
script

Rerun script when new data is added to folder
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-75
Listing data

Enumeration: Lists of objects without a known count

Use a looping structure to process one object at a time

ListFeatureClasses (Wildcard (optional), Type (optional))
#return a list of shapefiles in a workspace
gp.workspace = "C:\\Yellowstone"
fcs = gp.ListFeatureClasses("*", "all")

Examples of Enumerations

ListFeatureClasses

ListFields

ListTables

List Indexes

ListRasters

ListWorkspaces

ListDatasets
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
fcs = all shapefiles
in C:\Yellowstone
What's New in ArcGIS 9
1-76
The Geoprocessor Programming Model

Geoprocessor diagram lists full functionality
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-77
Looping

While a condition is true
gp.workspace = "C:\\Yellowstone"
fcs = gp.ListFeatureClasses ("*", "all")
fc = fcs.next()
clipfc = "C:\\Yellowstone\\studyarea.shp"
outputws = "C:\\Yellowstone\\model"
while fc != "":
gp.clip_analysis(fc, clipfc, outputws + "\\" + fc)
fc = fcs.next()

Loop is defined by indentation in Python

Indentation is a language construct in Python

Needs to be consistent

Use <Tab> or spaces

Checked by TabNanny utility in PythonWin
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-78
Batch processing example
import win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.gpDispatch.1")
gp.workspace = "C:\\Database\\Wyoming\\AlbanyCounty"
#Get a list of rasters in the workspace
rasters = gp.listrasters("*", "all")
#Store path to the output folder
outputPath = "C:\\Database\\Wyoming\\AlbanyCounty\\PilotPeakStudyArea"
#For each raster in the list
raster = rasters.next()
while raster != "":
#Set the outputname to be the same as the input
outName = outputPath + "\\" + raster
#For each raster, clip it to a specific extent
gp.clip_management(raster, "387211 231491 410734 250303", outName)
raster = rasters.next()
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-79
Built-in geoprocessor methods

Check whether an object (feature class, GDB) exists
gp.Exists(myGDB)

Refresh Catalog to notify script of new data created in
script
gp.RefreshCatalog("c:\wksp\geodatabase.mdb")

File Management tools existing in toolboxes
gp.Delete(myGDB)
gp.Copy(myGDB, "C:\temp\newGDB")
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-80
Describe

Method that returns an object with a set of properties

Properties vary depending on what is being described
Describe (InputValue):
Object
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-81
Making decisions

If statement
dsc = gp.describe(fc)
inputtype = dsc.ShapeType
if inputtype == "Point":
gp.Buffer_analysis(fc, "hospital_buff", "500.00 Meters")
else: #inputtype is polygon or line
gp.Clip_analysis(fc, "cntybnd", "hospital_buff_clp")

Break statement to jump out of closest enclosing loop
while field:
if field.Name == "Flag":
gp.CalculateField(fc, "Flag", "1")
break
field = fields.next()
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-82
Why use parameters with script tools?

Makes scripts more flexible

Dynamically assigns script arguments


Workspace

Feature classes

String, numbers
Write scripts to capture input arguments
import win32com.client, sys
gp = win32com.client.Dispatch("esriGeoprocessing.gpDispatch.1")
gp.workspace = sys.argv[1] # first argument
buffc = sys.argv[2] # second argument
buffoutput = sys.argv[3]
buffdistance = sys.argv[4]
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-83
Parameter properties
Display
name
Required/
Optional
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
Data type
(workspace)
Input/
Output
What's New in ArcGIS 9
1-84
Running scripts that have parameters

Use form to browse for data types
you specify
 Form
automatically built based
upon parameters you specify
 Name
of parameter you set when
creating the script

Supply arguments at command line
or DOS prompt
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-85
Advanced modeling
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Using advanced functionality

Models can carry out actions beyond what is within the
toolbox
 Run
a previous model as a tool
 Run
a script as a tool
 Use
an .exe or external function as part of a model
Executable
Model
Script
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-87
Running a previous model in another model

Drag and drop model from toolbox like any other tool

If output from a previous model is a parameter, it will be
exposed in a larger model
P
P
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
P
What's New in ArcGIS 9
1-88
Relative paths

Property of a model or a script

Keeps track of all tools and data elements
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-89
History

C:\Documents and Settings\<USER>\Application
Data\ESRI\ArcToolbox\My Toolboxes

Each history model contains tools for each process
executed in each ArcMap/ArcCatalog session

Honor generating reports
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-90
Using a script

Drag and drop a script from a toolbox
 Create

parameters in script to add connections between tool
Attach an .exe or AML to a script
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-91
Exporting to a script

Easy way to start writing scripts
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-92
Run a script at a specific time

Windows AT command
AT 9:00 C:\yellowstone\yellowstone.py

Windows scheduler
 Control
Panel > Scheduled Tasks
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-93
Precondition property

Used to control flow of model execution
Precondition

Used to branch within a model
Preconditions
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-94
Branching in a model

Precondition property from a script
If the input
is a line or point
Output will
be points
Output will
be polygons
Script
If the input
is a polygon
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-95
Steps for branching within a model
 Create a script that will set parameters as output-based
upon if/then conditions
 Add the script to a toolbox, specifying output
parameters that are in the script
 Apply precondition properties to tools to determine
model flow
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-96
Writing a script to branch in a model

Specify parameters to control which tools are executed in
a model
inputfc = gp.GetParameterAsText(0) #SAME AS sys.argv[1]
dsc = gp.describe(inputfc)
if dsc.shapetype == "polygon":
gp.SetParameterAsText(1, "True")
gp.SetParameterAsText(2, "False")
else:
gp.SetParameterAsText(1, "False")
gp.SetParameterAsText(2, "True")
A
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-97
Adding output text parameters
Data type =
Boolean
Type =
Derived
Direction =
Output
A
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-98
Applying a precondition property

Set for tools accepting parameters from a script
1. Apply Precondition
to next tool
2. Select condition
from script
output
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-99
Scripting with Python
Demo
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
Resources for learning Python


Books

Learn to Program Using Python

Learning Python

The Quick Python Book

Python, Essential Reference
Web sites


The Python Foundation (www.python.org): Tutorials, documentation
ESRI Instructor-led course

Introduction to Geoprocessing Scripts Using Python

Writing Geoprocessing Scripts with ArcGIS .pdf

Online help
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-101
Learning paths
http://www.esri.com/training_events.html

Learning Guide
 Learning
paths organized
by software and topic

Learning options
 Instructor-led
 Virtual
Campus courses
 Training
 Web
courses
seminars
workshops
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
What's New in ArcGIS 9
1-102
Software support resources

ESRI Support Center
a gateway to resources
http://support.esri.com



Knowledge Base

Technical articles

White papers

System requirements
Downloads

Patches and service packs

Data models

ArcScripts

User Forums

Discussion groups

E-mail lists
Copyright © 2002, 2003, 2004 ESRI. All rights reserved.
For Developers


Developer resources
Request ESRI Technical Support

Available to support subscribers
What's New in ArcGIS 9
1-103