Dynamic Geoprocessing Scripts

Posted on March 18, 2008. Filed under: Uncategorized |

Other posts in this series: 
Scripting Your ArcGIS Geoprocessing Tasks (Part 1)
Scripting Your ArcGIS Geoprocessing Tasks with Cursors (Part 2)
More ArcGIS Geoprocessing Concepts – Enumeration Objects (Part 3)
Obtaining Descriptive Information About GIS Datasets with ArcGIS Geoprocessing Objects (Part 4)
Executing ArcGIS Tools from Geoprocessing Scripts (Part 5)

Through the use of parameters, also known as arguments, you can pass data into your geoprocessing scripts.   The use of parameters makes your scripts more generic and flexible since they allow you to provide for the dynamic input of datasets, tool parameters, or to control the processing logic of a script.  For example, if you had a geoprocessing script that called the Buffer tool you would need to specify the input features, output feature class, and a buffer distance at a minimum.  To make your script more flexible you could take advantage of these dynamic features to input variable data sources and buffer distances depending upon the circumstance. 

In this post we will examine two methods for passing data into your scripts.  The first, GetParameterAsText( ) is a method on GpDispatch that allows you to pass parameters into your scripts through a tool dialog available through ArcToolbox.  We’ll also look at Python’s sys module which can also be used to pass data into a geoprocessing script.

GetParameterAsText( )

The GetParameterAsText method on the GpDispatch object can only be used with ArcToolbox. The advantage of using this method is that it provides for the use of a visual dialog that accepts input data from the user.  This input data is then retrieved inside your geoprocessing script through the GetParameterAsText method.  GetParameterAsText is zero based, with the first argument located at position zero, and each successive argument increments to the next largest integer (see figure below).

There are a number of advantages to running your scripts from ArcToolbox using GetParameterAsText. When you take this approach, the script becomes part of the geoprocessing framework which means that it can be run from a model, ArcGIS command line or another script. In addition, the script has access to ArcMap environment settings and help documentation. Other advantages include a nice user interface that is easier to use, and error prevention capabilities. Error prevention capabilities provided include a dialog box that informs the user of certain errors.

There are a couple negatives to using GetParameterAsText that I should point out.  As I mentioned above GetParameterAsText can only be used within ArcToolbox which means that you can’t execute a script using this method from PythonWin or a DOS prompt.  One additional negative to consider with GetParameterAsText is that it is more difficult to debug the scripts since they can’t be tested from PythonWin.

Python system Module
Python, like other scripting languages, provides a mechanism for accessing arguments passed into the script.  Python’s system, or sys, module is used to accomplish this task. Specifically, the sys.argv[] list contains the name of the script being executed along with any parameters being passed into the script.  As with any other list in Python you can access the parameters through an integer value.  This list, like GetParameterAsText, is zero based.  The difference is that the first item in the list references the script that is being run whereas with GetParameterAsText, the first item references an actual parameter value.  So, let’s take a look at a quick example. 

In this example we use Python from a command line prompt to start a script named PointDistance.py. In addition, we pass in three arguments to the script by including them on the same line, but separated by a space. The sys.argv[] function stores each of the arguments in a list which we can access. Notice that the first argument, referenced by the integer 0 contains the name of the script, PointDistance.py. The arguments that are passed into the script can then be referred to by each successive integer.  Each of these arguments can then be used in your script as necessary.

A similar script called from ArcToolbox:

More Information
We will be presenting a new Virtual GIS Classroom course, “GIS Programming 101: Mastering Python for Geoprocessing in ArcGIS” starting March 24th and ending April 18th.  We still have 1 seat available so hurry to reserve your seat now.  We do offer a 15% discount for government, educational, and non-profit entities.

We also offer an e-learning course entitled “Introduction to Geoprocessing with Python” which contains even more detailed information on ArcGIS geoprocessing with approximately 200 slides of audio and video lectures, visual software demonstrations, exercises and data.

Advertisements

Make a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: