Accessing layers within layer groups on web maps

1277
4
Jump to solution
07-13-2022 09:48 AM
Linde_Carmack
Occasional Contributor

I am working in a hosted notebook in ArcGIS Online. 

I am using the following code to get a list of layers on a given web map:

 

    wm_item = gis.content.get(item_id)
    wm = WebMap(wm_item)
    lyrs = wm.layers

 

I am running into an issue with web maps created in the new Map Viewer that use the grouped layer functionality. The above code reads the group layers but not the individual layers within the groups. So when run on the map in the screenshot below it returns only 7 'layers', which are actually the group layers.

Linde_Carmack_0-1657730523700.png

Any ideas on how I can get around this? 

 

1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

When I try to replicate this, I actually see all the sub-layers, too. The only difference is that they appear in a nested "layers" object, so you'd have to use wm.layers[i].layers to see them.

If you just wanted to get all the layers in a single list, whether grouped or not, try something like this:

# layers outside of groups as own list
lyrs = [i for i in wm.layers if i.layerType != 'GroupLayer']

# group layers as own list
grp_lyrs = [j for j in wm.layers if j.layerType == 'GroupLayer']

# throw sub-layers from groups into lyrs list
[lyrs.append(k) for l in grp_lyrs for k in l.layers]

I really wanted it to be a one-liner, but it works just the same.

- Josh Carlson
Kendall County GIS

View solution in original post

4 Replies
jcarlson
MVP Esteemed Contributor

When I try to replicate this, I actually see all the sub-layers, too. The only difference is that they appear in a nested "layers" object, so you'd have to use wm.layers[i].layers to see them.

If you just wanted to get all the layers in a single list, whether grouped or not, try something like this:

# layers outside of groups as own list
lyrs = [i for i in wm.layers if i.layerType != 'GroupLayer']

# group layers as own list
grp_lyrs = [j for j in wm.layers if j.layerType == 'GroupLayer']

# throw sub-layers from groups into lyrs list
[lyrs.append(k) for l in grp_lyrs for k in l.layers]

I really wanted it to be a one-liner, but it works just the same.

- Josh Carlson
Kendall County GIS
Linde_Carmack
Occasional Contributor

Perfect, thank you!

0 Kudos
TracyLove
New Contributor II

i realize this is a old question, but the accepted answer actually only works for groups on the base of the web map legend, it does not work for layers nested in groups inside of groups.

why doesn't this do what it says it will do??....
https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.lay...

0 Kudos
maya_fromstein
New Contributor III

@TracyLove I'm a real python newb, so don't quote me on this (and apologies if it's wrong!).

I would think to get the layers nested in groups inside of groups you'd need to add another for loop to the last line of code

so instead of

# throw sub-layers from groups into lyrs list
[lyrs.append(k) for l in grp_lyrs for k in l.layers]

which I rewrote to make sense in my brain as (sorry can't figure out how to insert code in a reply):

for l in grp_lyrs:
        for k in l.layers:
             lyrs.append(k)

It might need to be something like:

for l in grp.layers: # accessing the group layers as group layers individually
      for k in l.layers: # accessing the sub-group layers individually (which in your case are still group layers?)
             for j in k.layers: # now hopefully accessing the sub-sub-layers you mentioned
                     lyrs.append(j) # appends the list of sub-sub-layers to the first of all non-group layers

which could maybe be prettier as:
[lyrs.append(j) for l in grp.layers for k in l.layers for j in k.layers]

That being said, I'm finding not all my "I would think"s based on R translate correctly into Python, but hopefully this helps gets you closer (if you're still working on it)! I share your frustration with the documentation for certain things. I often feel like it's for people who already understand what's going on (to which I don't belong, but aspire to!).

 

 

0 Kudos