操作方法

操作方法:通过从 Python 脚本调用模型并向其传递参数以重复运行该模型

Last Published: July 20, 2023

摘要

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 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 a list of all the feature classes in the workspace. This list can be stepped through and various operations applied to the contents of the list. To step through the list, the script uses the '.next' method of the list in conjunction with a 'while' loop. The while loop says, "While the position in the feature class list (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.

过程

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)

文章 ID:000008276

从 ArcGIS 专家处获得帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项

相关信息

发现关于本主题的更多内容