3

How do I rename a group layer in ArcGIS Pro using ArcPy?

In ArcMap I can do so thus:

groupLayer = arcpy.mapping.Layer(r"Path to grouplayer.lyr")# empty group layer as template
groupLayer.name = some string
arcpy.mapping.AddLayer(dataframe, groupLayer, add_position='TOP')

I can't find an analogous way to do this in ArcGIS Pro:

glyr = arcpy.mp.LayerFile(r"Path to GroupLayer.lyrx")# empty group layer as template
glyr.name = some string
theMap.addLayer(glyr, add_position='TOP')

While the above code adds the group layer, it does not rename it, and it does not flag an error so I'm not really sure what is going on.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 16, 2022 at 0:55

1 Answer 1

3

Your glyr variable is a LayerFile object, not a Layer. It represents the file your group layer is stored in, not the group layer itself. Note that a layer file can contain multiple layers, eg:

enter image description here

The LayerFile object doesn't have a .name property and doesn't raise an error when setting a non-existent property. You could do this and nothing would happen:

glyr.foo= "bar"
glyr.name = "some string"
glyr.another_nonexistent_property = "nothing"

Your group layer is a Layer in the LayerFile, and you can access it using the .listLayers() method, e.g.

lyr_file = arcpy.mp.LayerFile(r"path to.lyrx") # LayerFile object
group_layer = lyr_file.listLayers("Your template Group Layer name")[0] # Layer object
group_layer.name = "An Updated Name"
answered Nov 16, 2022 at 2:10
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.