Friday, July 13, 2012

Create a List of Geometries (10.1)

At 10.1, you can use geometries directly in geoprocessing functions like Clip or reading geometry properties.  Here is a tip to copy all the geometries into memory and use it as a list:

Example:
import arcpy
ds = r"c:\temp\sample.gdb\Parcels"
g = arcpy.Geometry()
geomList = arcpy.CopyFeatures_management(ds,g)
# Get the Area (Sq Ft)
area = 0.0
for geom in geomList:
    area += geom.area
print "Total Parcel Area: {0}".format(area)


The CopyFeatures() copies the results as a geometry object to a list and the area property is a standard Geometry object property.

Enjoy

2 comments:

Anonymous said...

It's great that GP tools and read and write geometries. However, for something like this, the following would be easier and faster:

sum([row[0] for row in arcpy.da.SearchCursor("bedrock", "shape@area")])

Andrew said...

For areas and length that is true, but for clipping, intersection, etc.. The other way is optimal.