Add Dataframe or Group Layer and populate

1488
6
12-04-2011 08:58 AM
JohnWall
Occasional Contributor
I am trying to run a script which will add all created layers to the current .mxd, but grouped together by a common attribute. I have found the following script elsewhere on this forum:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "24000 Scale Data", df)[0]
addLayer = arcpy.mapping.Layer(r"C:\Project\Data\StreetsWithLabels.lyr")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, addLayer

I have modified it as follows:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:/gis/gis540/Project/test.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
targetGroupLayer = [0]
addLayer = arcpy.mapping.Layer(r"C:/gis/gis540/Project/firehydrants/add6.shp")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")
mxd.saveCopy(r"C:/gis/gis540/Project/test2.mxd")
del mxd, addLayer

However, I keep getting an Assertion Error. I cannot figure out what the problem is with the modified code.
Tags (2)
0 Kudos
6 Replies
ScottOatley
New Contributor II
targetGroupLayer = [0]
addLayer = arcpy.mapping.Layer(r"C:/gis/gis540/Project/firehydrants/add6.shp")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")


Is targetGroupLayer correct? Looks to me like you're setting it to a list with a single element of 0. The AddLayerToGroup second parameter is supposed to be a layer.

Or am I missing something??

Scott
0 Kudos
JohnWall
Occasional Contributor
Hi Scott,
Thank you for the response!

I managed to modify the code that I posted to:
mxd = arcpy.mapping.MapDocument(r"C:/gis/gis540/Project/test.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
addLayer1 = arcpy.mapping.Layer(r"C:/gis/gis540/Project/firehydrants/add6.shp")
arcpy.mapping.AddLayer(df, addLayer1)
mxd.save()
del mxd, addLayer

You mentioned that AddLayer should add a .lyr file, but adding a .shp file seems to work. I'm not exactly sure why. Possibly it's a quirk?

I have tried to expand this code to:
mxd = arcpy.mapping.MapDocument(r"C:/gis/gis540/Project/test.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
addLayer1 = arcpy.mapping.Layer(r"test.lyr")
arcpy.mapping.AddLayer(df, addLayer1)
targetGroupLayer = arcpy.mapping.ListLayers(mxd, addLayer1, df)[0]
addLayer2 = arcpy.mapping.Layer(r"C:/gis/gis540/Project/firehydrants/add7.shp")
print df, targetGroupLayer, addLayer2
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer2) #, "BOTTOM")
mxd.save()
del mxd, addLayer1, addLayer2

Where test.lyr is meant to be an empty Layer Group which will be populated by add7.shp However, when I use test.lyr I end up getting "ValueError: Object: CreateObject Layer invalid data source" then when I put an actual file in there such as "C:/gis/gis540/Project/firehydrants/add6.shp" I end up with an "AssertionError."

To be honest, I'm somewhat new to programming Python using arcpy. But, I would have thought there is a way to create a blank group layer and then add other layers to it.
Thanks again,
John
IanLeinwand
New Contributor II
Did you ever get this to work... I"m struggling with a similar issue.

How ever I'm trying to add a data from a geodatabase... I keep getting the following error.

Object: CreateObject Layer invalid data source
0 Kudos
JohnWall
Occasional Contributor
Did you ever get this to work... I"m struggling with a similar issue.

How ever I'm trying to add a data from a geodatabase... I keep getting the following error.

Object: CreateObject Layer invalid data source


Ian,
I was unable to get a dataframe to be added.
-John
0 Kudos
JeffBarrette
Esri Regular Contributor
Data frames can not be dynamically added to a map document via Python.  They must be authored ahead of time in the map document.

Jeff
0 Kudos
GraemeBrowning
Occasional Contributor III
Someone else seems to be having a similar AddLayerToGroup problem at Stack Exchange GIS.

I think it may be a bug worth investigating but have provided a workaround there which I think should work for some/most use cases.
0 Kudos