<?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 Add a multi-layer view to a webmap using add_layer adds layers individually in reverse, not as a group layer in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-multi-layer-view-to-a-webmap-using-add-layer/m-p/1539963#M10683</link>
    <description>&lt;LI-CODE lang="python"&gt;webmap_item = gis.content.get(webmap_id)
view_item = gis.content.get(view_id)
# interrogate webmap_item to get a specific lyr to remove
wm = WebMap(webmap_item)
wm.remove_layer(lyr)
wm.update() # success
wm.add_layer(view_item)
wm.update() # partial success, partial fail
# item is added but as individual layers in reverse order&lt;/LI-CODE&gt;&lt;P&gt;The result of line 7 is that each layer contained in the view gets added to the WebMap individually in reverse order, not as a single GroupLayer.&lt;/P&gt;&lt;P&gt;I would like to add the hosted feature layer view as a&amp;nbsp;&lt;STRONG&gt;single GroupLayer&lt;/STRONG&gt; to the WebMap.&lt;/P&gt;&lt;P&gt;TIA&lt;/P&gt;</description>
    <pubDate>Wed, 18 Sep 2024 14:22:14 GMT</pubDate>
    <dc:creator>Dirk_Vandervoort</dc:creator>
    <dc:date>2024-09-18T14:22:14Z</dc:date>
    <item>
      <title>Add a multi-layer view to a webmap using add_layer adds layers individually in reverse, not as a group layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-multi-layer-view-to-a-webmap-using-add-layer/m-p/1539963#M10683</link>
      <description>&lt;LI-CODE lang="python"&gt;webmap_item = gis.content.get(webmap_id)
view_item = gis.content.get(view_id)
# interrogate webmap_item to get a specific lyr to remove
wm = WebMap(webmap_item)
wm.remove_layer(lyr)
wm.update() # success
wm.add_layer(view_item)
wm.update() # partial success, partial fail
# item is added but as individual layers in reverse order&lt;/LI-CODE&gt;&lt;P&gt;The result of line 7 is that each layer contained in the view gets added to the WebMap individually in reverse order, not as a single GroupLayer.&lt;/P&gt;&lt;P&gt;I would like to add the hosted feature layer view as a&amp;nbsp;&lt;STRONG&gt;single GroupLayer&lt;/STRONG&gt; to the WebMap.&lt;/P&gt;&lt;P&gt;TIA&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 14:22:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-multi-layer-view-to-a-webmap-using-add-layer/m-p/1539963#M10683</guid>
      <dc:creator>Dirk_Vandervoort</dc:creator>
      <dc:date>2024-09-18T14:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi-layer view to a webmap using add_layer adds layers individually in reverse, not as a group layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-multi-layer-view-to-a-webmap-using-add-layer/m-p/1540022#M10685</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/573916"&gt;@Dirk_Vandervoort&lt;/a&gt;.,&lt;/P&gt;&lt;P&gt;Check out the below. I would urge you to make a copy of your WebMap and test before running! Code is commented and taken from multiple blog posts available &lt;A href="https://learn.finaldraftmapping.com/resources/blog/" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;The below adds the layers in reverse order (which is the order you want) and then groups those layers added in the WebMap. Currently no neat function for this but I believe it is on the way. Workaround available &lt;A href="https://learn.finaldraftmapping.com/group-arcgis-online-webmap-layers-using-the-arcgis-api-for-python/" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.mapping import WebMap

## access ArcGIS Online
agol = GIS("home")

## get the Feature Service as an Item object
view_item = agol.content.get("VIEW_ITEM_ID")

## the wm item
wm_item = agol.content.get("WM_ITEM_ID")

## create the webmap object
wm = WebMap(wm_item)

## get a list of the current layers.
## this is a list of dictionaries
current_lyrs = list(wm.layers)

## get the number of layers being added from teh view
num_view_lyrs = len(view_item.layers)

## add the layers in reverse order
for lyr in reversed(view_item.layers):
    wm.add_layer(lyr)

## get the list of dictionaries for the layers just added
lyrs2grp = wm.layers[-num_view_lyrs:]

## define the group layer
## you can set the titke of the group layer to some other text
group_lyr = [{
    "title" : view_item.title,
    "layers" : lyrs2grp,
    "layerType": "GroupLayer",
    "visibilityMode": "independent"
}]

## create a new operationalLayers list
new_ol = current_lyrs + group_lyr

## set the operationalLayers property to be the new_ol list
wm.definition.operationalLayers = new_ol

## update the webmap item object
item_properties = {"text":wm.definition}
wm_item.update(item_properties=item_properties)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 16:32:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-multi-layer-view-to-a-webmap-using-add-layer/m-p/1540022#M10685</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2024-09-18T16:32:16Z</dc:date>
    </item>
    <item>
      <title>Re: Add a multi-layer view to a webmap using add_layer adds layers individually in reverse, not as a group layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-multi-layer-view-to-a-webmap-using-add-layer/m-p/1548597#M10766</link>
      <description>&lt;P&gt;Python API 2.4.0 now supports Group Layers&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edits to your code for this to work in 2.4.0:&lt;/P&gt;
&lt;PRE&gt;webmap_item = gis.content.get(webmap_id)&lt;BR /&gt;view_item = gis.content.get(view_id)&lt;BR /&gt;wm = Map(item=webmap_item)&lt;BR /&gt;wm.content.remove(0) #index of layer to remove&lt;BR /&gt;wm.update()&lt;BR /&gt;wm.content.add(view_item)&lt;BR /&gt;wm.update()&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Oct 2024 10:19:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/add-a-multi-layer-view-to-a-webmap-using-add-layer/m-p/1548597#M10766</guid>
      <dc:creator>NanaeAubry</dc:creator>
      <dc:date>2024-10-15T10:19:28Z</dc:date>
    </item>
  </channel>
</rss>

