Show Navigation | Hide Navigation
You are here:
Geoprocessing > Automating your work with scripts > Data properties and access when scripting

Reading geometries

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

Print all topics in : "Data properties and access when scripting"


Related Topics

Note: This topic was updated for 9.3.1.

Each feature in a feature class contains a set of points defining the vertices of a polygon or line or a single coordinate defining a point feature. These points can be accessed using the geometry object, which returns them in an array of point objects. The array object can contain any number of geoprocessing objects such as points, geometries, or spatial references.

Array properties

Count The number of objects in the array
Reset() Resets the array to the first object

NOTE: Reset is used only for non-cursor Arrays. Using reset on a cursor Array will not reset to the first record.

Next() Returns the next object in the array
Add(Object) Adds an object to the array in the last position
Insert(Index, Object) Adds an object to the array in a specific position
Remove(Index, Object) Removes a specific object from the array
RemoveAll() Removes all objects and creates an empty array
Replace(Index, Object) Replaces an object by index position
GetObject(Index) Returns a specific object from the array

Features in a geodatabase or shapefile can have multiple parts. The geometry object's PartCount property returns the number of parts for a feature. The GetPart method returns an array of point objects for a particular part of the geometry if an index is specified. If an index is not specified, an array containing an array of point objects for each geometry part is returned.

Point features return a single point object instead of an array of point objects. All other feature types—polygon, polyline, and multipoint—return an array of point objects or an array containing multiple arrays of point objects if the feature has multiple parts.

Point properties

ID A long value used to uniquely identify the point
X The horizontal coordinate of the point
Y The vertical coordinate of the point
Z The elevation value of the point
M The measure value of the point

A polygon consists of a number of rings if it contains holes. The array of point objects returned for a polygon contain the points for the exterior ring and all inner rings. The exterior ring is always returned first, followed by inner rings, with null point objects as the separator. Whenever a script is reading coordinates for polygons in a geodatabase or shapefile, it should contain logic for handling inner rings if this information is required by the script; otherwise, only the exterior ring is read.

A multipart feature is composed of more than one physical part but only references one set of attributes in the database. For example, in a layer of states, the state of Hawaii could be considered a multipart feature. Although composed of many islands, it would be recorded in the database as one feature.
A ring is a closed path that defines a two-dimensional area. A valid ring consists of a valid path, such that the from and to points of the ring have the same x,y coordinates. A clockwise ring is an exterior ring, and a counterclockwise ring defines an interior ring.

Learn more about writing geometries

The examples below will print the coordinates for all features to the interactive window:


Reading point geometries


Search cursor on a point feature class

# Import native arcgisscripting module
#
import arcgisscripting

# Create the geoprocessor object
#
gp = arcgisscripting.create(9.3)

infc = gp.GetParameterAsText(0)

# Identify the geometry field
#
desc = gp.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = gp.SearchCursor(infc)
row = rows.Next()

# Enter while loop for each feature/row
#
while row:
    # Create the geometry object 'feat'
    #
    feat = row.GetValue(shapefieldname)
    pnt = feat.GetPart()

    # Print x,y coordinates of current point
    #
    print pnt.x, pnt.y
    row = rows.Next()


Point geometry

With the above feature class, the script will return the information below.

2.0 4.0
8.0 10.0
7.0 5.0



Reading multipoint geometries

Search cursor on a multipoint feature class

# Import native arcgisscripting module
#
import arcgisscripting
# Create the geoprocessor object
#
gp = arcgisscripting.create(9.3)

infc = gp.GetParameterAsText(0)

# Identify the geometry field
#
desc = gp.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = gp.SearchCursor(infc)
row = rows.Next()

# Enter while loop for each feature/row
#
while row:
    # Create the geometry object
    #

    feat = row.GetValue(shapefieldname)

    # Print the current multipoint's ID
    #
    print "Feature " + str(row.getvalue(desc.OIDFieldName)) + ":"
    partnum = 0

    # Count the number of points in the current multipart feature
    #
    partcount = feat.PartCount

    # Enter while loop for each point in the multipoint feature
    #
    while partnum < partcount:
        # Get the point based on the current part number
        #
        pnt = feat.GetPart(partnum)
        # Print x,y coordinates of current point
        #
        print pnt.x, pnt.y
        partnum += 1

    row = rows.Next()


Multipoint geometry

With the feature class above, the script will return the information below.

Feature 0:
3.0 8.0
4.0 4.0
6.0 6.0
Feature 1:
5.0 9.0
8.0 10.0
Feature 2:
9.0 5.0



Reading polyline or polygon geometries


Search cursor on a polygon or line feature class

# Import native arcgisscripting module
#
import arcgisscripting

# Create the geoprocessor object
#
gp = arcgisscripting.create(9.3)

infc = gp.GetParameterAsText(0)

# Identify the geometry field
#
desc = gp.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = gp.SearchCursor(infc)
row = rows.Next()

# Enter while loop for each feature/row
#
while row:
    # Create the geometry object
    #
    feat = row.GetValue(shapefieldname)

    # Print the current multipoint's ID
    #
    print "Feature " + str(row.getvalue(desc.OIDFieldName)) + ":"
    partnum = 0

    # Count the number of points in the current multipart feature
    #
    partcount = feat.PartCount

    # Enter while loop for each part in the feature (if a singlepart feature
    # this will occur only once)
    #
    while partnum < partcount:
        # Print the part number
        #
        print "Part " + str(partnum) + ":"
        part = feat.GetPart(partnum)
        pnt = part.Next()
        pntcount = 0

        # Enter while loop for each vertex
        #
        while pnt:
            # Print x,y coordinates of current point
            #
            print pnt.x, pnt.y
            pnt = part.Next()
            pntcount += 1

            # If pnt is null, either the part is finished or there is an 
            #   interior ring
            #
            if not pnt: 
                pnt = part.Next()
                if pnt:
                    print "Interior Ring:"
        partnum += 1

    row = rows.Next()


Polygon geometry

With the feature class above, the script will return the information below. Feature 0 is a single-part polygon, feature 1 is a two-part polygon, and feature 2 is a single-part polygon with an interior ring.

Feature 0:
Part 0:
3.0 8.0
1.0 8.0
2.0 10.0
3.0 8.0
Feature 1:
Part 0:
5.0 3.0
3.0 3.0
3.0 5.0
5.0 3.0
Part 1:
7.0 5.0
5.0 5.0
5.0 7.0
7.0 5.0
Feature 2:
Part 0:
9.0 11.0
9.0 8.0
6.0 8.0
6.0 11.0
9.0 11.0
Interior Ring:
7.0 10.0
7.0 9.0
8.0 9.0
8.0 10.0
7.0 10.0


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