2

I'm trying to create an mxd that can dynamically create and name group layers, and populate them with various feature layers. I've seen some examples of how to do this, such as : Creating empty group layer within existing group layer using arcpy.mapping?

The basics seem to be: reference an empty layer file:

groupLayer = arcpy.mapping.Layer(r"D:\Test\GroupTest.lyr")

Add it to the map:

arcpy.mapping.AddLayer(df, groupLayer, "BOTTOM")
listedGroupLayer = arcpy.mapping.ListLayers(mxd, "GroupTest", df)[0]

Then add some layer to it:

addLayer = arcpy.mapping.layer(r"D\Test\Rivers.lyr")
arcpy.mapping.AddLayerToGroup(df, listedGroupLayer, addLayer, "BOTTOM")[0]

I'm getting stuck on the first step: I need to dynamically create an empty layer file (based off of group layer names in a spreadsheet), before referencing it.

Any examples of how to create an empty layer file (rather than reference one), within python?

Kadir Şahbaz
78.6k57 gold badges260 silver badges407 bronze badges
asked Aug 20, 2015 at 15:30
1
  • It's not possible to create an empty group layer using arcpy. It is possible using python if you use arcobjects via the comtypes module. See gis.stackexchange.com/q/80 Commented Aug 21, 2015 at 11:52

2 Answers 2

2

Another option is to have a generic group layer saved as *.lyr file. Then when you load the layer change the name property and add it to the map document. You may be able to rename it after adding it to the map document. Then save the map document to retain the changes.

lyr = arcpy.mapping.Layer("grouplayer.lyr")
lyr.name = "Test Name"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.mapping.AddLayer(df,lyr,"BOTTOM")
answered Apr 15, 2016 at 12:54
1

As commented by @Luke:

It's not possible to create an empty group layer using arcpy. It is possible using python if you use arcobjects via the comtypes module. See Accessing ArcObjects from Python?

answered Apr 15, 2016 at 3:30

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.