Select to view content in your preferred language

Python API add all sublayers from hosted feature layer and maintain layer order

230
1
4 weeks ago
AFackler_NAPSG
Frequent Contributor

Hello all,

I am trying to work on a notebook that will pull in a specified feature layer and add it to an existing web map. So far the script I have will add all the data as expected, however it is never in the same order as it is in the hosted layer, and the group that layers are out into is not named the same things as the hosted layer (always being named Custom Group Layer). The layers should be in the opposite order to my screenshot below (waypoints first, then worksites, etc with Incident Area at the bottom of the list). I have tried a few different methods of adding layers and all of them produce the same exact result. Does anyone have any guidance on this?

AFackler_NAPSG_0-1775585957517.png

from arcgis.gis import GIS

gis = GIS("home")

layer_item_id = "<Layer ID>"
webmap_item_id = "<Map ID>"

layer_item = gis.content.get(layer_item_id)
webmap_item = gis.content.get(webmap_item_id)

layer_collection = layer_item.layers
    
wm = gis.map(webmap_item)
wm.content.add(layer_collection)
wm.update()

 

 

0 Kudos
1 Reply
Katie_Clark
MVP Regular Contributor

Have you tried looping through the layer collection and adding them in reverse index order? Assuming Waypoints is layer id 0, it's adding them in ascending order of layer id, with each new one added on top. So reverse it and it should be correct.

Best,
Katie

If this post helped you, please consider giving a kudos and/or marking as the accepted solution. Thanks!
0 Kudos