Geoprocessing with Scripts
In this
part, you'll be introduced to Python scripts. Don't worry,
you won't have to write any scripts. However, you will learn how to use custom
scripts to add power and flexibility to your models.
Although you
don't need to use scripts to perform even advanced geoprocessing
operations, you'll find that using scripts can streamline your work and add
functionality that is not available from system tools.
You can use scripts to create
custom geoprocessing tools for enhancing your
analysis.
Introduction to
scripting
You may
already know more about scripts than you think. In the previous module you
worked with visually diagrammed scripts—better known as models. Using ModelBuilder™, you arranged variables and tools in a
logical sequence and ran them as a single operation. Any model that you create
in ModelBuilder can be exported as a script file.
In this
topic you'll learn how to recognize a few key elements in a Python script, and
how to attach a script to a tool so that you can run it from ArcCatalog™ or ArcMap™.
Using scripts for geoprocessing
In the of
this geoprocessing session, you took a brief look at
a script. However, you still may not clearly understand what a script is. A
script can be defined as a set of instructions in plain text, stored in a file
and carried out by a software program, specifically a scripting application
program.
Each
scripting application has its own language, and not all scripting languages can
be used to write scripts for geoprocessing in ArcGIS®. Examples of scripting languages that can be used
include Python, Perl, VBScript and JScript. This part
focuses on Python because it is installed with the ArcGIS
software.
In the table
below, the same two lines of code are shown for three different scripting
languages. In each language, the code checks to see if a feature class named Roads_clip exists and then deletes it if found. Notice that
the syntax is different for each of the languages.
Scripting Language |
Sample Code |
Python |
|
VBScript |
|
JScript |
|
Scripts are
similar to models in that they allow you to chain processes together to run
more efficiently, and to document and share your methodology with others.
Scripts can also add functionality that is not available from the standard
system tools.
For example,
you can use scripts for batch processing of your data. Scripts can loop through
multiple datasets performing the same operation on each dataset, such as
projecting all shapefiles in a folder or all feature
classes in a geodatabase.
You can also
write your scripts to process based on certain conditions. For instance, a
script can check to see if the input data is a line or a polygon feature class
and then process the data accordingly.
Before a
script can be used for geoprocessing, it must be
attached to a tool. Once it's attached to a tool, it can be run as a standalone
tool or incorporated into models.
The anatomy of a script
Even if you
never write a line of code yourself, you may want to use scripts that others
have written. For this reason, you should be able to recognize some standard
elements in a geoprocessing script.
Standard script
elements are shown in this Python script.
Comments
One or more lines in a script that documents the code.
Comments are ignored by ArcGIS when the script is
run. In Python, comments begin with a pound sign (#) and are displayed in green
text by default. The information provided by comments is important, as it tells
you what the script does.
A word about script documentation
It is important to note that a script should be adequately
documented so that those using it can understand what the script does and can
attach it to a tool correctly—that's what comments are for. It is also
important to document the script tool so that those using it do not have to go
back to the script to figure out how it works. You'll learn how to document
tools in the next part.
Standard
code
Sets up the interaction between ArcGIS
and PythonWin, the scripting application used to
create and run Python scripts. All Python scripts used for geoprocessing
in ArcGIS will have these two lines of code (or
similar code) at the top of the script. Python scripts are run in PythonWin, and this code enables the two programs to
communicate.
Script arguments
Values that are passed into the script. Also called system arguments, this code
allows users to interact with the script by specifying values for variables
such as input and output workspaces. When you attach the script to a tool, you
will create tool parameters that correspond to the script arguments.
Environment
setting
A setting such as an input or output workspace that
applies to all processes run by a script.
Tool
Any system tool can be run from a script by specifying
the tool name (e.g., Clip), toolbox alias (e.g., analysis), and tool
parameters.
Want to learn about writing Python scripts?
To learn
about writing scripts, the ArcGIS Desktop Help topic Geoprocessing > Writing geoprocessing
scripts is a good place to start.
If you want
to see examples of Python scripts, you can open any of the system script tools
(e.g., the Multiple Ring Buffer tool) in edit mode. Also, any model that you
have created using ModelBuilder can be exported to a
Python script.
Using
scripts in models
Now that you've learned how to attach a script to a tool, you're ready
to learn how to add a script tool to a model. In this topic, you'll return to
the lynx project. The exercises will demonstrate three different ways you can
use scripts to enhance the lynx habitat suitability models you worked with in
the previous module, with each exercise increasing in complexity.
In the
first exercise, you'll learn how to add a script to a model. The script used in
this exercise was written specifically for the lynx submodels
and cannot be used in other models. The user of the models cannot change the
values used by the script without modifying the code in the script itself.
In the
second exercise, you'll work with a script that can be used with any
appropriate data. When you add the script to the model, you'll create a model
parameter from one of the script tool's parameters so that its value can easily
be changed each time the model is run.
In the
third exercise, you'll add a script tool that creates conditional branching in
a model. The script will check to see if a condition is true or false. If the
condition is true, the model will run the processes in one branch. If the
condition is false, the processes in the other branch of the model will run.
Script
tool parameters for models
If you are
using a custom script tool as an element in a model, there may be additional
factors that you need to take into consideration when you set up the tool
parameters. For example, a script tool that will only be run as a standalone
tool (such as the Clip All Feature Classes tool you created in the previous
topic), may only have input parameters. However, when a tool is added to a
model, it may also need to have an output parameter specified. Without an
output parameter, you cannot connect the custom script tool to the next process
in the model.
In the
example below, no tool parameters have been set for Script Tool 1. Therefore,
it cannot be connected to any other element in the model. (Note the lack of
connectors on both sides of the tool.) Script Tool 2 only has an input
parameter. It can take input data from the model, but without an output
parameter, it cannot connect to the next process. Script Tool 3 has both an
input parameter and an output parameter. It can take input data from the previous
process and provide output data that can be used by the next process in the
model.
A custom script tool
needs to have at least one input parameter and one output parameter to connect
to the other elements in a model.
Values for
the input and output parameters can either be supplied by adjacent processes in
the model, or by the user of the model, depending on how the script is written
and the how the tool parameter properties are specified.
A script may
be written for specific data and operations so that when it is attached to a
tool, it will only work in the model it was written for. Other scripts are
written to be more generic so that they can be used in different models. You'll
get practice incorporating both types of scripts into models in the following
exercises.
Decision-making
in a model
When
programmers write scripts, they can use statements native to the scripting
language to test a condition. If one condition is true, then the appropriate
set of code will run, and if the condition is false (or another condition is
true) then another set of code will run. You can incorporate this same
functionality into your models by using a script tool to enable conditional
branching.
For example,
you may want your model to work with shapefiles, coverages, or geodatabase feature
classes. Your script would check to see what type of data is being used as the
input, and report this information as output for use by the model. If the input
is a shapefile, the branch of the model that begins
with the Feature Class to Feature Class process would run. If the input is a coverage, the branch of the model that starts with the
Select Data process would run to extract a feature class. If the input is a geodatabase feature class, the branch beginning with the
Select process would run. Using conditions allows you to add flexibility to
your model.
In this example of a
model with conditional branching, a script checks to see if a particular field
exists. If it does, the model processes continue. If it does not, the field
will be added before the other processes are run.
Review
A script is
a set of instructions in plain text, stored in a file and carried out by a
scripting application program. Before a script can be used to perform geoprocessing operations, it must be attached to a tool.
The Add Script wizard allows you to do this easily.
Custom
script tools can be set up to work only as a standalone tool (for example, the
Clip All Feature Classes tool); only in a model (the Select and Calculate
tool); or both (the Reclass Values tool).
Some
benefits of using scripts are:
· They allow you to chain multiple
processes together.
· They can add functionality that is
not available from the standard system tools, such as batch processing or
checking for conditions.
· Script tool parameters can be
easily changed and the script can be run again using different values or
assumptions.
For a custom
script tool to work, it is essential that the tool parameters are set up
correctly. The first part of knowing how to set up parameters correctly is
being able to determine how the corresponding arguments are used in the script.
Script comments should help you figure out how the script works, and therefore
how the arguments are used. The second part is to understand the data types and
properties that can be set for tool parameters. Finally, if the tool is going
to be used in a model, it's important to remember you may need to set up an
additional parameter that does not have a corresponding script argument.