23

Does anyone now how to add a group layer with Python in ArcGIS Desktop 10?

I can use arcpy.mapping.AddLayer but surely this is only for an actual layer as opposed to a group layer.

so far I have this

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.listdataframes(mxd, "layers")[0]
arcpy.mapping.AddLayer
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 24, 2010 at 11:46

3 Answers 3

20

I do not think ArcPy provides anything to create group layer directly, but there is a simple workaround. You can create a .lyr file which contains a single empty group layer and add it to your map:

groupLayer = arcpy.mapping.Layer(r"EmptyGroupLayer.lyr")
arcpy.mapping.AddLayer(dataFrame, groupLayer, "BOTTOM")

Then you can use the AddLayerToGroup function to add another layers under your group layer.

answered Dec 24, 2010 at 13:19
3
  • I have a couple of layer groups but within them further groups. Let's call them parent and child. the parents are all unique however the children within each parent are the same. EG Incidentals>Birds and Transects>Bird. Is there anyway to avoid the conflict of the two bird group layers having the same name? Commented Nov 26, 2012 at 18:57
  • This gave me an error when I called AddLayerToGroup. I found a solution here: geonet.esri.com/thread/105771#comment-393442 groupLayer = arcpy.mapping.ListLayers(mxd, "AAAA", dataFrame)[0] Commented May 24, 2016 at 2:28
  • For ArcGIS Pro readers that may found this question. As of ArcGIS Pro 3.0, you have access to createGroupLayer (name, group_layer) in the Map class. If you are still on 2.x, this answer is still valid but arcpy.mp.LayerFile is now arcpy.mapping.Layer. Commented Mar 2, 2023 at 9:31
4

Save an empty group layer as a .lyr file. Then you can use AddLayer to add it to your map and then add new layers from there.

answered Dec 26, 2010 at 4:02
0
0

For ArcGIS 10.2 and 10.3, you cannot add a Layer to that group. You should refer to the layer added in the TOC and not the one on the drive. Finish with the steps described in the following post : AddLayertoGroup

newlyrGr = arcpy.mapping.ListLayers(df)[0]

Then, all works!

arcpy.mapping.AddLayerToGroup(df, newlyrGr, feat_layer)

answered Jan 6, 2017 at 8:26

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.