Working with geometry in Python
All simple feature classes require a geometry field, typically (but not always) named Shape. The Describe function can be used to retrieve the name of geometry field from a feature class, using the shapeFieldName property. The value of the geometry field is a geometry object, and a geometry object has a number of properties that describe the feature. The example below shows how to create a geometry object for each line feature in a feature class and sum their length:
import arcpy
inFeatures = "d:/base/data.gdb/roads"
# Create search cursor
#
rows = arcpy.SearchCursor(inFeatures)
# Calculate the total length of all roads
#
length = 0
shapeName = arcpy.Describe(inFeatures).shapeFieldName
# For each row, tally the length of the feature
#
for row in rows:
feat = row.getValue(shapeName)
length += feat.length
# Print the total length of all roads
#
print length
|
Property |
Explanation |
|---|---|
|
area |
The area of a polygon; empty for all other feature types |
|
centroid |
The true centroid if it is within or on the feature; otherwise, returns the label point (returns a point object) |
|
extent |
Returns an extent object |
|
firstPoint |
The first coordinate of the feature; returns a point object |
|
hullRectangle |
The coordinate pairs of the convex hull rectangle |
|
isMultipart |
True, if the number of parts for this geometry is more than one |
|
labelPoint |
The point at which the label is located; returns a point object (The labelPoint is always located within or on a feature. |
|
lastPoint |
The last coordinate of the feature; returns a point object |
|
length |
The length of the linear feature; empty for point, multipoint feature types |
|
partCount |
The number of geometry parts for the feature |
|
pointCount |
The number of point objects for the current part of geometry |
|
trueCentroid |
The center of gravity for a feature; returns a point object |
|
type |
Polygon, polyline, point, multipoint, multipatch, dimension, annotation |
|
Method |
Explanation |
|---|---|
|
getPart({index}) |
Returns an array of point objects for a particular part of geometry or an array containing a number of arrays, one for each part |