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!