Select to view content in your preferred language

Label map with feature count

2424
4
Jump to solution
11-25-2019 08:23 AM
CCWeedcontrol
Regular Contributor

I have a map that i need to label the total number of features(feature count) for each layer. I would like to be able to create a label expression but i can't seem to find out how to do this, the only thing that comes to mind is count[field] but that doesn't work. Similar to right clicking on a field, statistics - count but i want to able to label that layer with that count on a map on the fly. I would appreciate any help, thanks.

0 Kudos
1 Solution

Accepted Solutions
ChelseaRozek
MVP Regular Contributor

Sorry about that, sounded like an Arcade question. I wasn't sure, so I just messed around in ArcMap for a bit and this worked for me:

- Set Text String as Expression (not a field)

- Set Parser to Python

- Check Advanced so it's ON

- below the def FindLabel(SOMETHING):

put these 2 lines of text, making sure they have one space in front of them so the first line ("def") is the furthest left.

mxd = arcpy.mapping.MapDocument("CURRENT")
return int(arcpy.GetCount_management(arcpy.mapping.ListLayers(mxd)[0]).getOutput(0))

I don't know if this is the most efficient way of writing it, but it worked for me. It didn't work while I was editing the layer, but once I saved and stopped editing, the labels would update with the new number. Change the 0 to the index of your layer in the table of contents (first layer = 0, second layer = 1, third layer = 2, etc.)

Hope that works for you!

View solution in original post

0 Kudos
4 Replies
ChelseaRozek
MVP Regular Contributor

It looks like that function isn't available in the labeling context. I only see $layer (to use as Count($layer) to total the # of features) as a global variable under Adding an Arcade expression under Configure Popup. Unfortunately then it cannot be used in symbology or labeling.

More information: Profiles | ArcGIS for Developers 

Does it need to just be a webmap, like using it in Collector? Might be nice to put it in a dashboard and then use an indicator widget to display the count.

0 Kudos
CCWeedcontrol
Regular Contributor

Thanks for the replay, I should have mentioned that i am working in ArcMap.

0 Kudos
ChelseaRozek
MVP Regular Contributor

Sorry about that, sounded like an Arcade question. I wasn't sure, so I just messed around in ArcMap for a bit and this worked for me:

- Set Text String as Expression (not a field)

- Set Parser to Python

- Check Advanced so it's ON

- below the def FindLabel(SOMETHING):

put these 2 lines of text, making sure they have one space in front of them so the first line ("def") is the furthest left.

mxd = arcpy.mapping.MapDocument("CURRENT")
return int(arcpy.GetCount_management(arcpy.mapping.ListLayers(mxd)[0]).getOutput(0))

I don't know if this is the most efficient way of writing it, but it worked for me. It didn't work while I was editing the layer, but once I saved and stopped editing, the labels would update with the new number. Change the 0 to the index of your layer in the table of contents (first layer = 0, second layer = 1, third layer = 2, etc.)

Hope that works for you!

0 Kudos
CCWeedcontrol
Regular Contributor

I came up with this but your code also works but with changing the index number or in my case the layer name, thanks for the code. I do have to check "Remove duplicate labels" in the label other options.

It would be nice to not have to put in the index number because I have about 30 layers on my map.

def FindLabel ([OBJECTID] ):
  mxd = arcpy.mapping.MapDocument("CURRENT")
  lyr = arcpy.mapping.ListLayers(mxd,"1N3W")[0]
  with arcpy.da.SearchCursor(lyr, ["OBJECTID"]) as cursor:
    rows = {row[0] for row in cursor}

    count = 0
    for row in rows:
        count += 1
  return "%s "  % count‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos