[bug?]Layers with the same name are not distinguished

303
3
07-28-2024 10:42 PM
-_-
by
Regular Contributor

I created a code that gets multiple layers from the parameters and makes them visible.

 

import arcpy

selectLayerLists=arcpy.GetParameterAsText(0) #LayerList

aprx=arcpy.mp.ArcGISProject("CURRENT") 
lay=aprx.activeView #Layout 
mf=lay.listElements("mapframe_element","*")[0] 
map=mf.map 
Lyr_list=selectLayerLists.split(";") 
arcpy.AddMessage(Lyr_list) 

for lyr in Lyr_list:
    map.listLayers(lyr)[0].visible=True

 

Layer structure:
ryoho
ryoho/L02
ryoho/L01
L02
L01

The layer selected as the parameter:
ryoho
L02
L01

Code execution results:

タイトルなし.png

 

 

 

 

 

 

As shown in the image, the group layer and layers with the same name within the group have been switched to display.
How can you get the results you want with the parameters?

As an experiment, I retrieved the parameters with "GetParameter" and set the retrieved layer.visible=True, but the layer did not appear.
In 3.2.2, layers with the same name were distinguished as "Layer1:1" and "Layer1:2", so this code worked without any problems, but after updating to 3.3.1, the following situation occurred.

Due to the nature of my work, there are many layers with the same name, and changing the layer names for the sake of creating legends is not a good idea.
I would appreciate any ideas on how to solve this problem.
I would also like to know if this problem is only occurring in my environment.

0 Kudos
3 Replies
TonyAlmeida
Frequent Contributor

Maybe specify the group layer name?

 

import arcpy

# Define the path to the project and the map
aprx_path = r'C:\path\project.aprx'
map_name = 'Map'
group_layer_to_turn_on = 'YourGroupLayerName'  # Specify the name of the group layer

# Open the project
aprx = arcpy.mp.ArcGISProject(aprx_path)

# Get the map
m = aprx.listMaps(map_name)[0]

# Function to turn on a specific group layer and its sub-layers recursively
def turn_on_specific_group_layer(layer, group_layer_name):
    if layer.isGroupLayer and layer.name == group_layer_name:
        layer.visible = True
        print(f"Turned on visibility for group layer: {layer.name}")
        for sub_layer in layer.listLayers():
            turn_on_sub_layers(sub_layer)  # Recursively check sub-layers

# Function to turn on all sub-layers of a specific group layer
def turn_on_sub_layers(layer):
    if layer.isGroupLayer:
        layer.visible = True
        print(f"Turned on visibility for sub-group layer: {layer.name}")
        for sub_layer in layer.listLayers():
            turn_on_sub_layers(sub_layer)  # Recursively check sub-layers
    else:
        layer.visible = True
        print(f"Turned on visibility for sub-layer: {layer.name}")

# Loop through top-level layers in the map to find the specific group layer
for lyr in m.listLayers():
    turn_on_specific_group_layer(lyr, group_layer_to_turn_on)

# Save the project
#aprx.save()
#print("Project saved.")

# Clean up
#del aprx
0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Give longName a shot, that will include if it's in a group layer or not. You'll probably just have to do it as a check of some sort, like

"if '\\'in lay.longName"
0 Kudos
-_-
by
Regular Contributor

Thank you for the idea.

However, since I need to get the layer name as a parameter, it's a pain to specify the group name each time.

So I decided to temporarily process it using the index number until it returns to normal.

 

import arcpy

selectLayerLists=arcpy.GetParameterAsText(0) #レイヤリスト

aprx=arcpy.mp.ArcGISProject("CURRENT")

lay=aprx.activeView

mf=lay.listElements("mapframe_element","*")[0]

map=mf.map

allLayerList=map.listLayers()
allLayerList_index=[]

Lyr_list=selectLayerLists.split(";") #レイヤ名をリストに分割


for i in range(0,len(allLayerList)):
    allLayerList_index.append([i,map.listLayers()[i].longName])

arcpy.AddMessage(allLayerList_index)

for n in range(0,len(allLayerList_index)):
    arcpy.AddMessage(allLayerList_index[n][1])
    if allLayerList_index[n][1] in Lyr_list:
        targetNumber=allLayerList_index[n][0]
        map.listLayers()[targetNumber].visible=False

 

 

 

0 Kudos