HOW TO

Use scripting in ArcGIS Desktop to run custom tools and repetitive geoprocessing workflows and tasks

Summary

A variety of scripting languages are supported in the geoprocessing environment of ArcGIS 9.0 Desktop, including Python, VBScript, JScript, and Perl, which provide the ability to run custom tools batch style. Any scripting language that is COM compliant can be used to create successful geoprocessing scripts. Users can use Model Builder in conjunction with geoprocessing tools to create time-saving tools based on complex workflows.

Procedure

Scripting allows tools to be run again and again by looping over the same section of code many times. A good introduction to scripting in ArcGIS can be found in the Online Scripting Documentation. In addition to a good general introduction to the use of Python scripting in ArcGIS, there is an entire section dedicated to Batch Geoprocessing with Scripting. A sample script is provided below, which shows how to clip all feature classes in a folder (workspace). Help on Batch Processing can also be found in the Desktop Help System under: Geoprocessing > Automating your work with scripts > Batch Processing with geoprocessing > About Batch Processing with geoprocessing scripts.

While tracing through the script below, there are a number of important things to notice. First, the script sets the workspace property of the geoprocessor object to a path. This tells the geoprocessor object that all data can be found in this location unless otherwise stated. The script then calls the ListFeatureClasses method, which returns an enumeration of all the feature classes in the workspace. This enumeration can be stepped through and various operations applied to the contents of the enumeration. To step through the enumeration, the script uses the '.next' method of the enumeration in conjunction with a 'while' loop. The while loop says, "While the position in the feature class enumeration (fc) contains something, execute the lines of code in the loop". The code demonstrates the simple formula for performing batch operations: Get a list of the objects to which to apply operations, and use a while loop to iterate over this list.

Code:

#Import standard library modules
import arcgisscripting, sys, os

#Create the Geoprocessor object
GP = arcgisscripting.create()

#Set the input workspace
GP.workspace = "C:/data/project"

#Set the clip featureclass
clipFeatures = "C:/data/project/StudyArea.shp"

#Set the output workspace
outWorkspace = "C:/data/project/clipped"

try:
#Get a list of the featureclasses in the input folder
fcs = GP.ListFeatureClasses()

#Loop through the list of feature classes
fcs.Reset()
fc = fcs.Next()

while fc:
#Validate the new feature class name for the output workspace.
outFeatureClass = outWorkspace + "/" + GP.ValidateTableName(fc, outWorkspace)

#Clip each feature class in the list with the clip feature class.
#Do not clip the clipFeatures, it may be in the same workspace.
if str(fc) != str(os.path.split(clipFeatures)[1]):
GP.clip_analysis(fc, clipFeatures, outFeatureClass)

#Move to the next fc in the list.
fc = fcs.Next()

except:
print GP.GetMessages(2)



Article ID:000006921

Software:
  • ArcMap 9 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic