HOW TO
This article provides a procedure to repeat run a model by calling the model from Python within a loop and passing arguments to it. It is one of two methods available to create batch style processes so that geoprocessing models can be repeated.
Note: This article pertains to ArcGIS versions 9.x only. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands, and geoprocessing tools.
Another method of batching geoprocessing tasks is to create a model of the basic task, export that to a script and then add Python code to perform looping. For more information on this method, refer to How To: Use scripting in ArcGIS Desktop to run custom tools and repetitive geoprocessing workflows and tasks.
Note: While the geoprocessing environment of ArcGIS 9.x supports Python, VBScript, JScript, and Perl scripting languages,any scripting language that is COM compliant can be used to create successful geoprocessing scripts.
While tracing through the script below there are a number of important things to note.
The code example below uses the example model called 'MyClipTool'. It resides in a toolbox called 'MyToolbox'. MyClipTool has two arguments: an input shapefile and an output shapefile. The script loops through each file in 'C:\mydata\input_folder\'. For each iteration of the loop, it executes MyClipTool passing in an input shapefile and writing the output to 'C:\mydata\output_folder\'.
Code: # Import system modules import sys, string, os, win32com.client # Create the Geoprocessor object gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") # Load required toolboxes... gp.AddToolbox("C:\\mydata\\MyToolbox.tbx") gp.overwriteoutput = 1 gp.workspace = "C:\\mydata\\input_folder\\" outfolder = "C:\\mydata\\output_folder\\" try: fcs = gp.ListFeatureClasses() fcs.reset() fc = fcs.Next() while fc: gp.MyClipTool(fc, outfolder + fc) fc = fcs.Next() print "done" except: print GP.GetMessages(2)
Article ID: 000008276
Get help from ArcGIS experts
Download the Esri Support App