Select to view content in your preferred language

Moving a layer to a SUB-group

293
1
10-03-2024 06:00 AM
Shane_EU
Occasional Contributor

Can anyone post the code to move a layer to a SUB-group-layer?

The following code (courtesy of https://gis.stackexchange.com/questions/475584/move-an-existing-layer-into-a-group-layer-using-arcpy...) works fine for copying the layer to a top-level group-layer, but I can't get it working for a group layer within another group layer e.g. MyGroupLayer/SubGroupA

 

Thanks,

Shane

 

lyr_to_move = mp.listLayers("My Layer Name")[0]
group_layer = mp.listLayers("MyGroupLayer")[0]

# Use this to move layers (but not into a group layer)
#mp.moveLayer(group_layer, lyr_to_move, "AFTER")

# Use this to move a layer into a group layer
mp.addLayerToGroup(group_layer, lyr_to_move, "AUTO_ARRANGE")

 

0 Kudos
1 Reply
JakeSkinner
Esri Esteemed Contributor

Hi @Shane_EU, the following should work:

aprx = arcpy.mp.ArcGISProject("CURRENT")
mp = aprx.listMaps("Map")[0]

lyr_to_move = mp.listLayers("Airports")[0]
group_layer = mp.listLayers("SubGroup")[0]

# Use this to move a layer into a group layer
mp.addLayerToGroup(group_layer, lyr_to_move, "AUTO_ARRANGE")

# Remove layer from map
mp.removeLayer(lyr_to_move)

Before script:

JakeSkinner_0-1727961755364.png

After script:

JakeSkinner_1-1727961779266.png