10

I'm using ArcGIS 10. In ArcMap's table of contents (TOC), is there a way to have the "Layer Name" automatically show a count of the total number of features in each layer?

I was thinking the TOC would look something like this:

  • Roads (27)
  • Streams (100)
  • Parcels (12)

I found this option for Unique Value renders, but:

  1. I am not an ArcObjects guy, and
  2. I want to work with just the Single Value renderer.

The "List By Selection" tab sort of has this capability, but only when there are selected features.

8
  • Are you looking to do this for a single layer in your map (that you have the name of), or having it applied to all layers on your map by default?
    – CHenderson
    Commented Sep 19, 2013 at 22:48
  • All layers in the TOC, preferable by default, and preferably updated when the counts of a layer changes (for example, when a feature is added or deleted). Commented Sep 19, 2013 at 22:57
  • 2
    You could probably do this with a Python addin that listens for start/end of an edit session.
    – Paul
    Commented Sep 19, 2013 at 23:50
  • 1
    I think it may be doable in ArcGIS 10.1 and 10.2 (but not 10.0) using a Python Add-In (Extension) that runs GetCount on each layer and updates the name property of each layer to include that bracketed number at each refresh. If you find/submit an ArcGIS Idea to have this option OOTB in ArcGIS Professional I would vote for it.
    – PolyGeo
    Commented Sep 19, 2013 at 23:51
  • 2
    I have done this using a script in the mxd, so I pop the code into the python window and run it to get a print of each layer with feature count. As @PolyGeo says, that could be incorporated into a Python Add-In if you want it to happen automatically (at 10.1 as mentioned). Commented Sep 20, 2013 at 4:59

1 Answer 1

7

As @Paul & @PolyGeo suggested, I think trying to make this a Python Add-in makes the most sense, and I will pursue that idea later.

In the meantime, I put together code that will Add/Update the TOC Name of user-defined layers in an MXD with feature counts. For my purposes, I just created this as a GP tool that would accept individual layers via a multivalue input that accepts "Layers" in the script tool. That allows me to update multiple layers "on-demand", just updating the feature counts of those layers of interest.

I haven't come up with a way to have this run automatically, however in doing some testing of old MXD's, that may not even be desirable. If you have a lot of layers with a lot of features, it could be a slow process.

Inputbox

import arcpy

LayerInput = arcpy.GetParameterAsText(0)

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):

    #Skip over group layers, as they have no values to count
    if lyr.isGroupLayer:
        continue

    #Determine basename of the layer, without the feature count
    name = str(lyr.name)

    #Determine if the layer is in the user-defined list
    if name not in LayerInput:
        continue

    #Determine if the layer name already includes a COUNT
    if "[" in name and "]" in name:
        lpos = name.find("[")
        basename = name[:lpos-1]
    else:
        basename = name
    print "    Updating feature count in TOC name for layer: " + str(basename)
    arcpy.AddMessage("    Updating feature count in TOC name for layer: " + str(basename) )

    # In 10.1, you may be able to use arcpy.da.SearchCursor to increase the speed.
    #http://gis.stackexchange.com/questions/30140/fastest-way-to-count-the-number-of-features-in-a-feature-class
    #fcount = 0
    #cursor = arcpy.SearchCursor(lyr)
    #for row in cursor:
    #    fcount += 1
    #del cursor

    #Get the feature count
    fcount = int(arcpy.GetCount_management(lyr).getOutput(0))

    #Update the lyr.name property
    lyr.name = basename + " [n=" + str(fcount) + "]"
    del fcount

arcpy.RefreshTOC()

#Garbage collection
del mxd
3
  • GetCount will be faster than a cursor. What made you draw the opposite conclusion?
    – blah238
    Commented Sep 20, 2013 at 15:52
  • My initial testing of small shapefiles showed it was faster. however, after testing on larger RDBMS layers, you are correct, GetCount was faster. I've updated the code above. Commented Sep 20, 2013 at 16:46
  • Nice little tool, you should share that on the ESRI code gallery?
    – Hornbydd
    Commented Sep 21, 2013 at 15:59

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