Select to view content in your preferred language

Accessing layers within layer groups on web maps

2359
6
Jump to solution
07-13-2022 09:48 AM
Linde_Carmack
Regular 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

6 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
Regular Contributor

Perfect, thank you!

0 Kudos
TracyLove
Emerging Contributor

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
Occasional Contributor

@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
lumbrjak
Emerging Contributor

I just ran up against this same issue, and took a similar approach to above, but dealt with the deeply nested layer structure of my own webmap by recursing through the layer structure to build the layer list. Seems to work for as far as I've tested (which isn't too far yet. . . )

lumbrjak_0-1723657258747.png

 

0 Kudos
DanielDery
Occasional Contributor

This code can be used to list all layers from your web maps:

from arcgis.gis import GIS
from arcgis.mapping import WebMap

gis = GIS("pro")

search_my_contents = gis.content.search(query="owner:YOUR_USERNAME", item_type="Web Map", max_items = 1000)

for webmap_item in search_my_contents:
    webmap_obj = WebMap(webmap_item)

for webmap_item in search_my_contents:
    webmap_obj = WebMap(webmap_item)
    for layer in webmap_obj.layers:
        if "layers" in dir(layer):
            for sub_layer in layer.layers:
                my_string= str(webmap_item.title)+", "+str(sub_layer.title)
        else:
            my_string= str(webmap_item.title)+", "+str(layer.title)
        print(str(my_string))
0 Kudos