Check whether a group layer is empty or not

1983
3
01-10-2014 02:15 AM
RanaMahmoud
Occasional Contributor
Hello,

I have an mxd file with around 100 layers and group layers. I'm trying to loop through all group layers and find out if they're empty or not (contains Feature class(es) or not). If a group layer is empty, I want to remove that group layer from the map.

I found the script to remove the group layer, but I couldn't figure out how to check if the group layer is empty or not

Anyone can help?
Thank you
Tags (2)
0 Kudos
3 Replies
GeraldineEGGERMONT
Deactivated User
Hi Rana,

You can use this code to remove empty group layers:

# Remove empty group layers
ListLyr = arcpy.mapping.ListLayers(df)
if ListLyr:
    for lyr in ListLyr:
        count = 0
        if lyr.isGroupLayer:
            for sublyr in lyr:
                count = count + 1

            if count == 0:
                arcpy.mapping.RemoveLayer(df, lyr)
                arcpy.AddMessage("Removed layer " + lyr.name)

mxd.save()


Geraldine
0 Kudos
RanaMahmoud
Occasional Contributor
Thank you Geraldine, looks like it's gonna work. I'll test it as soon as I go back to work after the week-end
0 Kudos
RanaMahmoud
Occasional Contributor
Thank you Geraldine, it works 🙂
Is there any way to loop through all levels of the group layers?
0 Kudos