Total Count of features in layers in a mxd

After converting a lot of data from CAD and formatting it, I appended the four feature classes I had into a template feature class using field mapping. I wanted to do a quick check to determine if the number of features in the individual feature classes matched up with the number in the target dataset.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
n = 0 #cumulative total of all features in the mxd

for lyr in arcpy.mapping.ListLayers(mxd):
    #Use Get Count to return a Result containing the number of rows in the layer.
    #Cast the output to an int
    i = int(arcpy.GetCount_management(lyr).getOutput(0))
    #Get a cumulative total of all the features in the mxd.
    n += i
    #print the result
    print "{0} has {1} features. Total feature count = {2}".format(lyr, i, n)

The sum total of the features of the individual feature classes was different to those in the appended dataset, so I wanted to check how big the discrepancies were in terms of number missing per feature class.

fc = arcpy.mapping.ListLayers(mxd, "myLayer")[0]
myField = "my_field"

for u in set([row[0] for row in arcpy.da.SearchCursor(fc,myField)]):
    sql_clause = """ "{0}" = '{1}' """.format(myField,u)
    arcpy.SelectLayerByAttribute_management(fc, "NEW_SELECTION",sql_clause)
    i = int(arcpy.GetCount_management(fc).getOutput(0))
    print "There are {0} features in {1}".format(i,u)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.