Show Navigation | Hide Navigation
You are here:
Geoprocessing > Automating your work with scripts > Accessing tools within a geoprocessing script

Creating the geoprocessor object

Release 9.3
Last modified June 3, 2010
E-mail This Topic Printable Version Give Us Feedback

Print all topics in : "Accessing tools within a geoprocessing script"


Related Topics

Note: This topic was updated for 9.3.1.

ArcGIS 9 introduced a scripting object, GPDispatch, that could be used in scripting languages such as Python, VBScript, and JScript. A common object model based on objects supporting IDispatch was created to provide a uniform scripting experience. However, this meant none of the unique capabilities and strengths of a particular scripting language were utilized by the geoprocessing object.

ArcGIS 9.2 introduced native Python support for geoprocessing scripting. Instead of relying on Python's win32com module to create the geoprocessor, the geoprocessor could be created using a new native arcgisscripting module. This change provides the flexibility to use Python geoprocessing scripts on a variety of platforms, instead of being limited to Windows operating systems. The arcgisscripting module also provides some IntelliSense capabilities. Server tasks can only be performed using the arcgisscripting module.

Over the past several releases, users have overwhelmingly selected Python as the scripting language of choice. At 9.3, a further enhanced native Python geoprocessing object was introduced to accept and return common Python structures such as lists and Booleans. This creates an experience for geoprocessing that is richer and more consistent with standard Python use. The 9.3 geoprocessor can be created using an optional version argument with the create method on the arcgisscripting module. The behavior of existing scripts that create the geoprocessor without setting the version will be unchanged.

All geoprocessing Python scripts can import either the arcgisscripting or win32com modules to instantiate the geoprocessor object. The examples below illustrate the difference. The arcgisscripting module is built closely upon Python 2.5.1 and requires this version to create the geoprocessor. Scripts can be run on different versions of Python if they use win32com to create the geoprocessor.

9.3-version geoprocessor

# Create the geoprocessor using the arcgisscripting module using 
#   9.3 for the optional version argument
#
import arcgisscripting
gp = arcgisscripting.create(9.3)

gp.workspace = "c:/Tongass"
gp.clip_analysis("standb4", "clipcov", "standb4_clip", "POLY", "1.25")


9.2-version geoprocessor
# Create the geoprocessor using the arcgisscripting module
#
import arcgisscripting
gp = arcgisscripting.create()

gp.workspace = "c:/Tongass"
gp.clip_analysis("standb4", "clipcov", "standb4_clip", "POLY", "1.25")


Dispatch
# Create the geoprocessor using the win3com.client module
#
import win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

gp.workspace = "c:/Tongass"
gp.clip_analysis("standb4", "clipcov", "standb4_clip", "POLY", "1.25")


Once you have the object, you can run any tool as a method. The standard toolboxes installed with ArcGIS are immediately available once the geoprocessor is created. These include the following:






Comparing the 9.3-version and 9.2-version geoprocessor

Python lists versus enumerations

For Python scripts with a geoprocessor version of 9.3 or greater, list methods (e.g., ListFeatureClasses, ListDatasets) return a true Python list object. With a 9.2-version geoprocessor, these methods return an enumeration object. With a 9.3-version geoprocessor, the following methods return a Python list: ListTools, ListEnvironments, ListToolboxes, ListFeatureClasses, ListDatasets, ListTables, ListRasters, and ListWorkspaces.

A Python list provides the opportunity to use and manage the results of a list function in a variety of ways. A list is a versatile data type and provides a number of methods (append, count, extend, index, insert, pop, remove, reverse, and sort) that can be used to manipulate and extract information.

The following examples contrast the differences between using ListFeatureClasses with a 9.3-version and 9.2-version geoprocessor.


9.3-version geoprocessor 9.2-version geoprocessor
List method code example
    import arcgisscripting
    # Use 9.3 for the optional version
    # argument
    #
    gp = arcgisscripting.create(9.3)
    gp.Workspace = "c:/data"
    # List function returns a
    # Python list
    #
    fcs = gp.ListFeatureClasses()
    print fcs



    # Step through a Python list
    # using a while loop
    #
    for fc in fcs:
    print fc

    import arcgisscripting


    gp = arcgisscripting.create()
    gp.Workspace = "c:/data"
    # List function returns an
    # enumeration
    #
    fcs = gp.ListFeatureClasses()
    print fcs
    # To get the next feature
    # class, you must use the
    # Next method
    #
    fc = fcs.Next()
    # Step through an enumeration
    # using a for loop
    #
    while fc:
    print fc
    fc = fcs.Next()

Expected results [u'counties.shp', u'roads.shp']
counties.shp
roads.shp
<geoprocessing list object …>
counties.shp
roads.shp

Tool return values

For Python scripts with a geoprocessor version of 9.3 or greater, geoprocessing tools return a geoprocessing result object. At 9.2, tools typically returned a string of the outputs.

Users familiar with geoprocessing server tools will recall that server tools returned result objects at ArcGIS 9.2. The advantage of a result object is that you can maintain information about the execution of tools, including messages, parameters, and output. These results can be maintained even after several other tools have been run. At 9.2, information about the execution of a tool was lost after running a subsequent tool.

The following examples contrast what is returned from a tool when using a 9.3-version and a 9.2-version geoprocessor.


9.3-version geoprocessor 9.2-version geoprocessor
Tool return values example
    import arcgisscripting
    import types
    # Use 9.3 for the optional version
    # argument
    #
    gp = arcgisscripting.create(9.3)
    # AddField returns a result
    # object
    #
    result = gp.AddField(
    "c:/data/fc.shp", "flag")
    # Use the result object's
    # GetOutput method to
    # get a unicode
    print result.GetOutput(0)

    # The result object has a
    # several other methods
    # and properties
    # Print the last message
    # Print the tool status
    #
    print result.GetMessage(
    result.MessageCount-1)
    print result.Status
    # 4 indicates 'Succeeded'

    import arcgisscripting
    import types


    gp = arcgisscripting.create()

    # AddField returns a string
    #
    result = gp.AddField(
    "c:/data/fc.shp", "flag")


    print result

Expected results c:/data/fc.shp
End Time: Wed May 30 15:17:12 2007 (Elapsed Time: 0.00 seconds)
4

c:/data/fc.shp

Extent and Point Objects

For Python scripts with a geoprocessor version of 9.3 or greater, extent and point properties are returned as extent and point objects. For a 9.2-version geoprocessor, these properties are returned as strings.

The following examples contrast what is returned as Describe's Extent property when using a 9.3-version and a 9.2-version geoprocessor. The 9.2-version geoprocessor returns only a space-delimited string of coordinates. In order to access the number values, the strings would need to be split and converted from a string, and several calculations would be required to make useful decisions based on those values. With the 9.3-version geoprocessor, a rich series of properties are available, which allows you to access the coordinates of the extent much more easily.

A similiar change is noticeable with the geometry objected returned from cursor methods. With a 9.3-version geoprocessor, a geometry object has an Extent property that returns an Extent object; and Centroid, TrueCentroid, LabelPoint, FirstPoint, and LastPoint properties return Point objects.


9.3-version geoprocessor 9.2-version geoprocessor
Boolean example

    import arcgisscripting
    # Use 9.3 for the optional version
    # argument
    #
    gp = arcgisscripting.create(9.3)
    fc = r"C:\geometry.gdb\fc"
    # Create a describe object from
    # a feature class
    #
    desc = gp.describe(fc)
    # Get the extent property from
    # the describe object
    #
    extent = desc.extent
    # Extent returns an extent object
    # Print some of the extent
    # properties (others include
    # MMin, MMax, ZMin, ZMax,
    # Height, Depth
    #
    print ("XMin: %f") % extent.xmin
    print ("YMin: %f") % extent.ymin
    print ("XMax: %f") % extent.xmax
    print ("YMax: %f") % extent.ymax
    print ("Width: %f") % extent.width
    # Return a point object for the
    # lowerleft point of the extent
    # (lowerright, upperleft and
    # upperright are also available)
    #
    lowerleftPoint = extent.lowerleft
    # Print the x, y coordinates of
    # the lowerleft extent
    print ("LowerLeft point X: %f")
    % lowerleftPoint.x
    print ("LowerLeft point Y: %f")
    % lowerleftPoint.y





    import arcgisscripting


    gp = arcgisscripting.create()
    fc = r"C:\geometry.gdb\fc"
    # Create a describe object from
    # a feature class
    #
    desc = gp.describe(fc)
    # Get the extent property from
    # the describe object
    #
    extent = desc.extent
    # Extent returns a space-
    # delimited string of coordinates
    #
    print ("Extent: %s") % desc.extent

Expected results XMin: -21465.218971
YMin: 98376.566253
XMax: -3263.051632
YMax: 113087.882679
Width: 18202.167339
LowerLeft point X: -21465.218971
LowerLeft point Y: 98376.566253
XMin: -21465.218971
YMin: 98376.566253
XMax: -3263.051632
YMax: 113087.882679
Width: 18202.167339
LowerLeft point X: -21465.218971
LowerLeft point Y: 98376.566253
Extent: -21465.2189714847 98376.5662526509 -3263.05163221594 113087.882679454


Booleans

For Python scripts with a geoprocessor version of 9.3 or greater, all Booleans are true Python Boolean objects. Boolean objects have two possible values; True and False. For a 9.2-version geoprocessor, any Boolean property will take a Python boolean object but outputs 1 for true and 0 for false.


9.3-version geoprocessor 9.2-version geoprocessor
Boolean example
    import arcgisscripting
    # Use 9.3 for the optional version
    # argument
    #
    gp = arcgisscripting.create(9.3)
    gp.OverWriteOutput = True
    # Overwriteoutput returns a
    # Boolean value
    #
    print gp.OverWriteOutput

    import arcgisscripting


    gp = arcgisscripting.create()
    gp.OverWriteOutput = True
    # Overwriteoutput returns a
    # 1 or 0
    #
    print gp.OverWriteOutput

Expected results True 1


Unicodes versus strings

For Python scripts with a geoprocessor version of 9.3 or greater, the geoprocessor accepts Unicode strings as input to methods and properties. Also, the geoprocessor returns Unicode strings instead of ASCII strings, which was the standard for 9.2. Unicode allows geoprocessing tools and methods to support characters that are not supported in normal strings, such as those found in non-European languages. In Python, Unicode values are shown and set by prefixing a u to a string.

# ASCII string
#
fc = 'river.shp'

# Unicode string
#
fc = u'river.shp'


Learn more about Unicode

Learn more about the differences between geoprocessor versions


Creating the geoprocessor with Perl, VBScript, or JScript


Although Python is the most commonly used scripting language for geoprocessing, geoprocessing scripts can also be created in a number of other script languages such as Perl, VBScript, and JScript. The following examples demonstrate how to create the geoprocessor, set the workspace environment, and run the Clip tool in different languages.

Perl example

use Win32::OLE;
my $gp = Win32::OLE->new('esriGeoprocessing.GPDispatch.1');
$gp->workspace = "c:/Tongass";
$gp->clip ("standb4", "clipcov", "standb4_clip", "POLY", "1.25");


VBScript example
Set gp = WScript.CreateObject("esriGeoprocessing.GPDispatch.1")
gp.workspace = "c:/Tongass"
gp.clip "standb4", "clipcov", "standb4_clip", "POLY", "1.25"


JScript example
var gp = WScript.CreateObject("esriGeoprocessing.GPDispatch.1");
gp.workspace = "c:/Tongass";
gp.clip ("standb4", "clipcov", "standb4_clip", "POLY", "1.25");


Please visit the Feedback page to comment or give suggestions on ArcGIS Desktop Help.
Copyright © Environmental Systems Research Institute, Inc.