Reading geometries

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.

Property

Explanation

count

The number of objects in the array.

Array properties

Method

Explanation

add(value)

Adds an Array or Point object to the array in the last position.

append(value)

Appends an object to the array in the last position.

clone(point_object)

Clone the point object.

extend(items)

Extend the array by appending elements.

getObject(index)

Returns a specific object from the array.

insert(index, value)

Adds an object to the array in a specific position.

next()

Returns the next object in the array.

remove(index)

Removes a specific object from the array.

removeAll()

Removes all objects and creates an empty array.

replace(index, value)

Replaces an object by index position.

reset()

Resets the array to the first object.

NoteNote:

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

Array methods

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, if the feature has multiple parts, an array containing multiple arrays of point objects.

Property

Explanation

ID

The shape ID of 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

Point properties

If a polygon contains holes, it will consist of a number of rings. The array of point objects returned for a polygon contains 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 between rings. 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:

Reading point geometries

Search cursor on a point feature class

import arcpy

infc = arcpy.GetParameterAsText(0)

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

# Create search cursor
#
rows = arcpy.SearchCursor(infc)

# Enter for loop for each feature/row
#
for row in rows:
    # Create the geometry object 'feat'
    #
    feat = row.getValue(shapefieldname)
    pnt = feat.getPart()

    # Print x,y coordinates of current point
    #
    print pnt.X, pnt.Y
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 arcpy

infc = arcpy.GetParameterAsText(0)

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

# Create search cursor
#
rows = arcpy.SearchCursor(infc)

# Enter for loop for each feature/row
#
for row in rows:
    # Create the geometry object
    #
    feat = row.getValue(shapefieldname)

    # Print the current multipoint's ID
    #
    print "Feature %i:" % row.getValue(desc.OIDFieldName)

    # For each point in the multipoint feature,
    #  print the x,y coordinates
    for pnt in feat:
        print pnt.X, pnt.Y
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 arcpy

infc = arcpy.GetParameterAsText(0)

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

# Create search cursor
#
rows = arcpy.SearchCursor(infc)

# Enter for loop for each feature/row
#
for row in rows:
    # Create the geometry object
    #
    feat = row.getValue(shapefieldname)

    # Print the current multipoint's ID
    #
    print "Feature %i:" % row.getValue(desc.OIDFieldName)
    partnum = 0

    # Step through each part of the feature
    #
    for part in feat:
        # Print the part number
        #
        print "Part %i:" % partnum

        # Step through each vertex in the feature
        #
        for pnt in feat.getPart(partnum):
            if pnt:
                # Print x,y coordinates of current point
                #
                print pnt.X, pnt.Y
            else:
                # If pnt is None, this represents an interior ring
                #
                print "Interior Ring:"
        partnum += 1     
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

Related Topics


12/15/2011