More ArcGIS Geoprocessing Concepts – Enumeration Objects (Part 3)

Posted on February 8, 2008. Filed under: ESRI, Geoprocessing | Tags: , , , , |

Other posts in this series on scripting your ArcGIS geoprocessing tasks:
Scripting Your ArcGIS Geoprocessing Tasks (Part 1)

Scripting Your ArcGIS Geoprocessing Tasks with Cursors (Part 2)

We all make lists in our daily lives.  Perhaps we create a grocery or shopping list, a “to do” list, a list of goals, or wish lists.  Whatever the case may be we are all familiar with the concept of lists.  Enumeration objects in the geoprocessing framework are essentially the same concept in that these objects contain lists of objects.  These may be lists of feature classes, fields, indexes, rasters, toolboxes, and many others.  As you can see below the GpDispatch/arcgisscripting object contains a number of methods that can be used to generate these enumeration objects.

We’ll start first with the ListFields and ListIndexes methods which return specific enumeration objects. 

ListFields Method
The ListFields method on GpDispatch takes three arguments as you can see below.

  • InputValue – a string representing the path to a feature class or table
  • wildCard – an optional parameter used to limit the fields returned.  If no value is given, all fieds are returned.  Using a combination of * and characters can be used to limit the fields returned
  • fieldType – an optional parameter used to limit the fields returned by data type (i.e. integer, string, date)

A Fields object is returned by the ListFields( ) method.

The Fields object is a type of enumeration which contains a list or collection of individual Field objects.  What you’ll notice about each of the enumeration objects is the existence of the Next and Reset methods.  These methods allow you to navigate the structure of the enumeration, and return the objects contained within as seen in the diagram below.

 

 Below you will see a visual depiction of the process for navigating an enumeration.  Although we are depicting the navigation of a Fields enumeration, the same process occurs in any other enumeration object.  In this example, we’re using the ListFields method to return an object named lstFields.  This object contains a list of individual Field objects for the shapefile that we specified in the ListFields method.  Each enumeration object contains a pointer that is used to access individual members of the collection.  This pointer is initially set just above the first object in the collection.  The first call to the Next method return the first Field object in the collection.  In this case, we’re returning a field named Pop1990.  A second call to the Next method will return the Pop2000 Field object.  Each successive call on the Next method will return the next Field until the end of the collection is reached at which point a None reference will be returned.  These are forward moving enumerations meaning that you can’t move back through the list.  You can however use the Reset method to move the pointer back to the top of the list.

Once the individual Field objects are retrieved from the enumeration you have read only access to the field information.  Notice on the figure below that each of the properties have a single sided barbell indicating that they are read only properties.  You can obtain a lot of information about the individual field objects including the name, aliasname, domain, length, type, and several others.

ListIndexes Method
The concepts that we discussed above in relation to the ListFields method and the Fields enumeration object returned as a result also apply to the ListIndexes method.  Instead of returning a Fields object we get an Indexes object which is also an enumeration object with the same Next and Reset methods.

 

The only difference in the method signatures between ListIndexes and ListFields is that ListIndexes does not have the optional dataType parameter. 

As you can see above the Indexes enumeration object contains individual Index objects which you can access from the collection.  Each index has a number of read only properties including the name, ascending and unique status, and the Fields on which the index operates.

Other Enumeration Methods
The remaining enumeration methods on GpDispatch return a generic enumeration object containing objects of type featureclass, raster, table, dataset, workspace, environment, toolbox, or tool.  One important thing to note on these methods is that you will need to specify the workspace before calling this methods.

import arcgisscripting
gp = arcgisscripting.create()
gp.toolbox = “analysis”

# Set the workspace
gp.workspace = “c:\\Houston\\CrimeData”

# Call ListFeatureClasses
fcs = gp.ListFeatureClasses( )

#loop through the feature classes and buffer
fcs.reset()
fc = fcs.next()

while fc:
     outName = “buffered_” + fc
     gp.buffer(fc, outName, ‘500 Feet’)
     fc = fcs.next()

More Information
We will be presenting a new Virtual GIS Classroom course, “GIS Programming 101: Mastering Python for Geoprocessing in ArcGIS” starting March 24th and ending April 18th.   This class has a maximum size of 10 students and is filling up quickly.  We do offer a 15% discount for government, educational, and non-profit entities.

We also offer an e-learning course entitled “Introduction to Geoprocessing with Python” which contains even more detailed information on ArcGIS geoprocessing with approximately 200 slides of audio and video lectures, visual software demonstrations, exercises and data.

Make a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: