You are here:
Writing geoprocessing scripts
Getting started with writing geoprocessing scripts
tasks can be time intensive since they are often performed on a number of different datasets or on large datasets with numerous records. is an efficient method of automating geoprocessing tasks. Scripting allows the execution of simple processes (a single tool) or complex processes (piggybacked, multitool tasks with validation). In addition, scripts are recyclable, meaning they can be data nonspecific and used again.
Scripting support in ArcGIS
Any ArcGIS user has the option of writing a script to automate geoprocessing work flows. Even if you have never thought of yourself as a programmer, after reading the relevant online documentation, you will be able to write scripts to perform geoprocessing. Common scripting tasks include modifying scripts to incorporate loops for batch processing or if statements for conditional control.
Learn more about scripting
You can also create models in ModelBuilder, which provides a canvas for you to visually create a geoprocessing work flow.
Learn more about creating models in ModelBuilder
ArcGIS 9 introduces scripting support for many of today's most popular scripting environments, such as Python, VBScript, JScript, and Perl. A new ArcObjects component, the geoprocessor, manages all the geoprocessing functions available within ArcGIS.
Python and the geoprocessor
It is an object that provides a single access point and environment for the execution of any geoprocessing tool in ArcGIS, including extensions. The geoprocessor implements automation using the IDispatch interface, making it possible for interpretive and macro languages to access the more than 400 available tools.
Which scripting language to choose can be an open question. Any scripting language that is COM compliant, interacts well in a Web-based environment, and allows users to complete their tasks is a viable option. Although there are a number of good scripting languages on the market, for simplicity, three of the more popular languages that meet the necessary criteria: VBScript, JScript, and Python are discussed. VBScript and JScript are familiar to many people and are relatively simple languages. Similar to Visual Basic and C they are designed to operate in a Windows environment. Python is an easy-to-learn language similar to C. Python has the ease of use of a scripting language, along with the programming capabilities of a complete developer language. Moreover, Python is platform independent and can operate on a variety of operating systems including UNIX, Linux, and Windows. For more information, visit www.python.org on the Web.
ESRI sees Python as the language that fulfills the needs of our user community. Some advantages of Python are:
Learn more about accessing Python on the Web
- Python is easy to learn because of its clean syntax and simple, clear concepts.
- Python supports object-oriented programming in an easy-to-understand manner.
- Documenting Python is easier because it has readable code.
- Complicated data structures are easy to work with in Python.
- Python is simple to integrate with C++ and Fortran.
- Python can be seamlessly integrated with Java.
- Python is free from the Web and has a widespread community.
Using the Geoprocessor Programming Model diagram
An accompanying diagram, the Geoprocessor Programming Model, gives an overview of the geoprocessor object and all the other objects that may be created from its methods.
View an illustration
You may notice that the type of data returned for a property is only specified when it is not a string or number. Most values are simply strings or numbers, so to keep the diagram simple, it only informs you of a data type when it is not a string or number.
The Geoprocessor Programming Model is not based on the notation used in the ArcObjects object model diagrams. The arrows on the diagram indicate instantiation. GpDispatch is the COM name for the geoprocessor IDispatch
object. Any scripting language that can instantiate a COM IDispatch object may create the geoprocessor. In Python, the Dispatch method on the win32com module's client object is used to create the geoprocessor, while Visual Basic and VBScript use the CreateObject function. The geoprocessor is the only ArcGIS scripting object that can be directly created using these methods. All other ArcGIS scripting objects are created by a method on the geoprocessor, such as CreateObject
, or as a property of an object, such as the fields of a feature class.
The different colors are meant to show logical relationships of objects. For example, the purple objects are created by the various list methods of the geoprocessor. The list methods are purple as well to indicate their relationship with these objects. Some properties, such as Field in a feature class, are colored to indicate they are an object, which, in this case, is the fields object that is purple. The colors should help you connect the various methods and properties with the appropriate objects and make the diagram easy to read.
The GpDispatch object supports the execution of tools as dynamic methods and the use of environment settings as dynamic properties. The diagram does not show these dynamic members of the geoprocessor, as the tools and environments that are accessible depend on what is installed on the desktop machine being used and what toolboxes are being referenced. The geoprocessor object simply refers to tools and environments as generic methods and properties.
Learn more about specifying environment settings in a script
Learn more details and see examples of how to use tools and set environments
Learn more about specifying environment settings
There are many examples of using Python to coordinate and execute geoprocessing tools. Many of these examples focus on the use of specific methods, properties, or both methods and properties of the geoprocessor and are intended to be concise and easy to follow. More detailed and sometimes complicated examples of how to use the geoprocessor to solve problems may be found as tools in ArcGIS. While most tools delivered with ArcGIS are written in C++ and delivered in a binary form, some are written in Python or ModelBuilder. Some tools execute batch operations, such as loading data into a geodatabase or projecting a set of feature classes, which is perfectly suited for scripting as the solution. The underlying script may be opened at any time to see what happens when the tool is run. Use this capability to learn how to use the geoprocessor in a number of situations, such as creating multiple ring buffers or building pyramids for a set of rasters. Script tools have a unique icon
so you can easily find tools that use scripts to execute. Use the edit option in a tool's context menu to open the underlying script.
View an illustration
These scripts may not be edited as they are read-only, but you can copy the tool to a custom toolbox and its underlying script to another folder so you can make edits if desired.
Learn more about editing scripts in a toolbox
ModelBuilder may also be used to generate scripts, as it will export a model to either Python, VBScript, or JScript.
Learn more about exporting a model to a script
The Help for each geoprocessing tool also contains a section specific to scripting, with a description of each parameter and an example of how to use the tool in a Python script.
Learn more about accessing Help for tools