Accessing Geometries using the Geoprocessor is an easy task when one uses Python. All you need is a cursor to a feature class and you are there. Some terminology may help, before we dive into the code.
Geometry: A geometry in this case is the geographical representation of an object as a point, or line, or polygon.
Feature Class: Many people get confused by the term. In essence, if you are in the shapefile world, this would be your shapefile. If you are using geodatabases, a feature class is a collection of objects. In both cases, it is a collection of features.
Feature: A feature is the digital representation of an object in the real world using a vector-based data type. For example, a tree can be a point, a road can be a line, countries would be polygons. Each tree would be a feature, as would each roads, etc. Features have both a spatial representation as well as an attribute representation (think of the object in the world and its properties).
Cursor: A cursor is an object that provides us access to a feature class. In other worlds, it is our way to communicate with a feature class through our programming language (Python). There are three types of cursors: Search (only reads feature classes), Insert (only adds features) and Update (can update or delete a feature).
Accessing geometries then, from the above definitions, would require us to (1) create a cursor to a feature class, (2) find the feature we want to read the geometry of, and (3) read the actual geometry. In reverse order, you may want to think of it this way: a geometry belongs to a specific feature, which belongs to a feature class, which is accessible through a cursor. Similarly, our Python code would do something similar, as shown below (only reads a geometry object):
import arcgisscripting # Import the geoprocessor capabilities gp = arcgisscripting.create(9.3) # Create the geoprocessor object featureClassToRead = r"path\to\file" # Define the feature class to read searchCursor = gp.SearchCursor(featureClassToRead) # Create a SEARCH cursor feature = searchCursor.Next() # Grab the first feature (row) from the feature class featureGeometry = feature.shape # Grab the feature's geometry object
After importing (line 1) and initializing (line 2) the geoprocessor, we create a search cursor (line 4) that pointed to our feature class. Within the cursor, we requested the first feature (line 5) and proceeded retrieving the feature’s geometry (line 6).
All other cursors will use a very similar approach. The difference will be line 4, in which we run the method that creates a cursor. If there is a need for an Insert cursor, you would run the InsertCursor command, or the UpdateCursor if you need the update cursor. With each cursor, you grab the first feature (row) and then manipulate it by accessing its individual variable (one of which is the geometry, called Shape).
Another important element is how one accesses the geometry. While there is no problem accessing the geometry by requesting the .shape variable present in each feature, this changes depending on your use of a shapefile or a geodatabase. To solve this problem, you can use the feature class description, and retrieve its .ShapeFieldName parameter. This is shown below:
import arcgisscripting # Import the geoprocessor capabilities gp = arcgisscripting.create(9.3) # Create the geoprocessor object featureClass = "path\to\file" # Define the feature class to describe description = gp.Describe(featureClass) # Call the Describe function shapeFieldName = description.ShapeFieldName # Grab the parameter and save it searchCursor = gp.SearchCursor(featureClass) # Create a new search cursor feature = searchCursor.Next() # Grab the first feature featureGeometry = feature.GetValue(shapeFieldName) # Grab the feature's geometry
You would now receive the geometry object, as with the sample code above, independent of the type of file you are reading.
Of course, we rarely need to read only the very first feature of a feature class. An easy method to read each feature is using a while loop, as follows:
import arcgisscripting # Import the geoprocessor capabilities gp = arcgisscripting.create(9.3) # Create the geoprocessor object featureClass = "path\to\file" # Define the feature class to describe description = gp.Describe(featureClass) # Call the Describe function shapeFieldName = description.ShapeFieldName # Grab the parameter and save it searchCursor = gp.SearchCursor(featureClass) # Create a new search cursor feature = searchCursor.Next() # Grab the first feature while feature: # Create a loop that runs while we have features # Work with the feature here feature = searchCursor.Next() # Grab the next feature in feature classs
This take advantage of the fact that the geoprocessor will return a None object when we reach the end of the feature class, which would let the loop know we are done.
Related posts:

(3 votes, average: 4.00 out of 5)
[...] to any spatial data using Python (to see more about Accessing Geometries using the Geoprocessor click here). Also, as Python is a general use scripting language, you can find reusable code by other users [...]