<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Accessing layers within layer groups on web maps in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1191968#M7556</link>
    <description>&lt;P&gt;I am working in a hosted notebook in ArcGIS Online.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the following code to get a list of layers on a given web map:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    wm_item = gis.content.get(item_id)
    wm = WebMap(wm_item)
    lyrs = wm.layers&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Linde_Carmack_0-1657730523700.png" style="width: 286px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/45816i2FE8117FDC718803/image-dimensions/286x248?v=v2" width="286" height="248" role="button" title="Linde_Carmack_0-1657730523700.png" alt="Linde_Carmack_0-1657730523700.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any ideas on how I can get around this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jul 2022 16:49:53 GMT</pubDate>
    <dc:creator>Linde_Carmack</dc:creator>
    <dc:date>2022-07-13T16:49:53Z</dc:date>
    <item>
      <title>Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1191968#M7556</link>
      <description>&lt;P&gt;I am working in a hosted notebook in ArcGIS Online.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using the following code to get a list of layers on a given web map:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    wm_item = gis.content.get(item_id)
    wm = WebMap(wm_item)
    lyrs = wm.layers&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Linde_Carmack_0-1657730523700.png" style="width: 286px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/45816i2FE8117FDC718803/image-dimensions/286x248?v=v2" width="286" height="248" role="button" title="Linde_Carmack_0-1657730523700.png" alt="Linde_Carmack_0-1657730523700.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any ideas on how I can get around this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2022 16:49:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1191968#M7556</guid>
      <dc:creator>Linde_Carmack</dc:creator>
      <dc:date>2022-07-13T16:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1192106#M7559</link>
      <description>&lt;P&gt;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&amp;nbsp;&lt;STRONG&gt;wm.layers[i].layers&lt;/STRONG&gt; to see them.&lt;/P&gt;&lt;P&gt;If you just wanted to get all the layers in a single list, whether grouped or not, try something like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 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]&lt;/LI-CODE&gt;&lt;P&gt;I really wanted it to be a one-liner, but it works just the same.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 03:11:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1192106#M7559</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2022-07-14T03:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1192178#M7560</link>
      <description>&lt;P&gt;Perfect, thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 12:52:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1192178#M7560</guid>
      <dc:creator>Linde_Carmack</dc:creator>
      <dc:date>2022-07-14T12:52:05Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1359891#M9406</link>
      <description>&lt;P&gt;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.&lt;BR /&gt;&lt;BR /&gt;why doesn't this do what it says it will do??....&lt;BR /&gt;&lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.layers" target="_blank"&gt;https://developers.arcgis.com/python/api-reference/arcgis.mapping.toc.html#arcgis.mapping.WebMap.layers&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Dec 2023 19:58:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1359891#M9406</guid>
      <dc:creator>TracyLove</dc:creator>
      <dc:date>2023-12-12T19:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1373517#M9577</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/548398"&gt;@TracyLove&lt;/a&gt;&amp;nbsp;I'm a real python newb, so don't quote me on this (and apologies if it's wrong!).&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;so instead of&lt;/P&gt;&lt;PRE&gt;# throw sub-layers from groups into lyrs list
[lyrs.append(k) for l in grp_lyrs for k in l.layers]&lt;/PRE&gt;&lt;P&gt;which I rewrote to make sense in my brain as (sorry can't figure out how to insert code in a reply):&lt;/P&gt;&lt;P&gt;for l in grp_lyrs:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; for k in l.layers:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lyrs.append(k)&lt;/P&gt;&lt;P&gt;It might need to be something like:&lt;/P&gt;&lt;P&gt;for l in grp.layers: # accessing the group layers as group layers individually&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; for k in l.layers: # accessing the sub-group layers individually (which in your case are still group layers?)&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for j in k.layers: # now hopefully accessing the sub-sub-layers you mentioned&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;lyrs.append(j) # appends the list of sub-sub-layers to the first of all non-group layers&lt;/P&gt;&lt;P&gt;which could maybe be prettier as:&lt;BR /&gt;[lyrs.append(j) for l in grp.layers for k in l.layers for j in k.layers]&lt;/P&gt;&lt;P&gt;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!).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2024 16:57:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1373517#M9577</guid>
      <dc:creator>maya_fromstein</dc:creator>
      <dc:date>2024-01-23T16:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1485668#M10122</link>
      <description>&lt;P&gt;This code can be used to list all layers from your web maps:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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))&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 05 Jun 2024 20:48:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1485668#M10122</guid>
      <dc:creator>DanielDery</dc:creator>
      <dc:date>2024-06-05T20:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1521664#M10428</link>
      <description>&lt;P&gt;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. . . )&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="lumbrjak_0-1723657258747.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/112353i6D750728832EE624/image-size/medium?v=v2&amp;amp;px=400" role="button" title="lumbrjak_0-1723657258747.png" alt="lumbrjak_0-1723657258747.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Aug 2024 17:45:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1521664#M10428</guid>
      <dc:creator>lumbrjak</dc:creator>
      <dc:date>2024-08-14T17:45:37Z</dc:date>
    </item>
    <item>
      <title>Re: Accessing layers within layer groups on web maps</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1598103#M11240</link>
      <description>&lt;P&gt;Great solution. Thanks lumbrjak.&lt;BR /&gt;Here is a code snippet that you can copy&lt;/P&gt;&lt;P&gt;```&lt;/P&gt;&lt;P&gt;feat_lyrs=[]&lt;BR /&gt;def listlyrs(inlyr):&lt;BR /&gt;global feat_lyrs&lt;BR /&gt;# if layer is group layer, recurse through any sublayers&lt;BR /&gt;if inlyr.layerType == 'GroupLayer':&lt;BR /&gt;subs=inlyr.layers&lt;BR /&gt;for sub in subs:&lt;BR /&gt;listlyrs(sub)&lt;BR /&gt;&lt;BR /&gt;else:&lt;BR /&gt;print(f"\n{inlyr.layerType} : {inlyr.title}\n")&lt;BR /&gt;df.loc[len(df)] = [inlyr.layerType, inlyr.title]&lt;BR /&gt;feat_lyrs.append(inlyr)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;lyrs = webmap_obj.layers&lt;/P&gt;&lt;P&gt;#build the lists by iterating through nested groups&lt;BR /&gt;for lyr in lyrs:&lt;BR /&gt;listlyrs(lyr)&lt;/P&gt;&lt;P&gt;```&lt;/P&gt;</description>
      <pubDate>Fri, 21 Mar 2025 16:33:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/accessing-layers-within-layer-groups-on-web-maps/m-p/1598103#M11240</guid>
      <dc:creator>TomerShachaf</dc:creator>
      <dc:date>2025-03-21T16:33:28Z</dc:date>
    </item>
  </channel>
</rss>

