ArcGIS Pro: map.MoveLayer not working as expected

493
2
Jump to solution
09-01-2023 01:28 AM
lbd_bayern
New Contributor II

Hi everyone.

I am trying to move a Group Layer (A) next to another Group Layer (B). B is the child of a third Group Layer (C). When I try to move the A next to B with the method map.MoveLayer, A is not placed on the same hierarchy level as B but on the same hierarchy level as C.

Intended result:

C

∟ B

 A

Actual result:

C

∟ B

A

 

To make this more concrete, please see my example code starting from an empty map. In the code, the respective Group Layers correspond to the following variable names:

A = group_layer_to_move_next_to_child_layer

B = child_group_layer

C = parent_group_layer

Here is my code:

 

##### SCRIPT VARIABLES TO ADJUST #####
PATH_LYRX_FILE = r'C:\Projects\ArcGISPro\Test_GroupLayer_below_GroupLayer6\all.lyrx'  # TODO: adjust to your project

##### ACTUAL SCRIPT #####
current_project = arcpy.mp.ArcGISProject("CURRENT")
map = current_project.activeMap
lyrx_file = arcpy.mp.LayerFile(PATH_LYRX_FILE)

# add parent_group_layer to the map
parent_group_layer = map.addLayer(lyrx_file)[0]
parent_group_layer.name = 'Parent_Group_Layer'

# add child_group_layer to the map ... 
child_group_layer = map.addLayer(lyrx_file)[0]
child_group_layer.name = 'Child_Group_Layer'
# ... and move it below parent_group_layer
map.addLayerToGroup(parent_group_layer, child_group_layer)
map.removeLayer(child_group_layer)

# add group_layer_to_move_next_to_child_layer to the map ...
group_layer_to_move_next_to_child_layer = map.addLayer(lyrx_file)[0]
group_layer_to_move_next_to_child_layer.name = 'Group_Layer_to_Move_Next_to_Child_Layer'
# ... and try to move it next to child_group_layer
map.moveLayer(child_group_layer, group_layer_to_move_next_to_child_layer, "AFTER")

 

So initially, my map and the table of contents are empty:

MoveLayer_01.png

Then, my script adds C (or parent_group_layer)  (line 10). Afterwards, B (or child_group_layer)  is added (line 14) and moved below C (line 17-18). In the next step, A (or group_layer_to_move_next_to_child_layer) is added to the map (line 21).

The table of contents now looks as follows:

MoveLayer_02.png

Finally, I want to move A next to B (line 24). However, A is not placed as child of C and on the same hierarchy level as B, but on the same hierarchy level as C:

MoveLayer_03.png

My intended result would look as follows:

MoveLayer_04.png

How can I achieve my goal by using the map.moveLayer method?

I am running my script in the ArcGIS Pro Python Window.

0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

Try addLayerToGroup() instead?

 

From the documentation: The addLayerToGroup method is the only way to add a layer or collection of layers into an existing, empty group layer in a map.

View solution in original post

2 Replies
AlfredBaldenweck
MVP Regular Contributor

Try addLayerToGroup() instead?

 

From the documentation: The addLayerToGroup method is the only way to add a layer or collection of layers into an existing, empty group layer in a map.

lbd_bayern
New Contributor II

Hi @AlfredBaldenweck,

thanks for your reply, and good to know that this restriction is explicitly stated in the documentation. I was able to solve my problem with addLayerToGroup()