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.
Any ideas on how I can get around this?
Solved! Go to Solution.
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.
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.
Perfect, thank you!
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...
@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!).
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. . . )
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))