0

I am looking for an ArcPy command to get the number of features in map layer.

There are posts on how to check the number of features in the source of the map layer with

arcpy.GetCount_management(lyr.dataSource)

but how to take into account any Definition Queries set for that layer and get the number of displayed features?

0

2 Answers 2

1

arcpy.GetCount_management(lyr)

It will honour definition queries and return a count of features in the layer.

If there are any selected features, it will return a count of the selected features only.

If you set the arcpy.env.Extent environment, it will honour that as well and only return a count of features (or selected features) that are within or intersect the given Extent.

0
1

The reason your code didn't work is that you provided the layer source (lyr.dataSource), not the layer (lyr). Functions which accept layers will honor a source, by converting it to a layer, with default selection set of "all features" and default definition query of "no query".

This is a fundamental concept in effective use of ArcGIS applications. If you saw code examples with "arcpy.GetCount_management(lyr.dataSource)", it's probably because the desire was to capture the total number of possible features, as in:

print("{:d} of {:d} features selected".format(
        int(arcpy.GetCount_management(lyr)[0]),      
        int(arcpy.GetCount_management(lyr.dataSource)[0]))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.