1

So I am trying to add a layer to a group, but running into problems most likely because of the file is a .tif. I have tried quite a few different iterations of this and looked at a few solutions but nothing seems to work, any ideas? I have also tried entering a [0] behind the listlayers command

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "15MinMaps", df)
for row in cursor:

       Sel = (row.getValue(field))
       for dirpath, dirnames, filenames in arcpy.da.Walk(r"Z:/IMAGES/MAPS/15_Min_Thematic_Maps",topdown=True, followlinks=True, type="TIF"):
           for filename in filenames:
               if Sel in filename.upper():
                print filename
                fullpath = (dirpath + "/" + filename)
                print fullpath
                Fname = (dirpath[36:] + " " + filename)
                result = arcpy.MakeRasterLayer_management(fullpath, Fname)
                layer = result.getOutput(0)
                arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, layer, "BOTTOM")
               else:
                   pass

I keep on receiving this error.

Traceback (most recent call last): File "C:\Users\sorrell\AppData\Local\ESRI\Desktop10.2\AssemblyCache{DE572FA2-3589-48E1-8D3E-6E2C0CB31129}\NewOMSIUA_addin.py", line 117, in onMouseDownMap arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, layer, "BOTTOM")

File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw)

File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 88, in AddLayerToGroup assert isinstance(target_group_layer, Layer) and target_group_layer._arc_object.isGroupLayer

AssertionError

It will add in the layer but will not add the layer into the group. Edit: I just sliced off the .tif at the end of the name just to make sure the .tif was not messing anthing up in the name. It did not work

  • This won't work because targetGroupLayer is currently a list object, not a layer object. It needs to be a group layer object. targetGroupLayer = arcpy.mapping.ListLayers(mxd, "15MinMaps", df)[0] should work if the 15MinMaps wildcard only finds your group layer, and mxd and df are set correctly. – XIY Nov 18 '14 at 23:12
1

Ok, I solved it myself. The problem was that it could not read the name of the layer to be added to group.

for dirpath, dirnames, filenames in arcpy.da.Walk(r"Z:/IMAGES/MAPS/15_Min_Thematic_Maps",topdown=True, followlinks=True, type="TIF"):
           for filename in filenames:
               if Sel in filename.upper():
                print filename
                fullpath = (dirpath + "/" + filename)
                print fullpath
                Fname = (dirpath[36:] + " " + filename[:-4])
                mxd = arcpy.mapping.MapDocument("Current")
                df = arcpy.mapping.ListDataFrames(mxd)[0]
                targetGroupLayer = arcpy.mapping.ListLayers(mxd, "15MinMaps", df)[0]
                arcpy.MakeRasterLayer_management(fullpath, Fname)
                arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, arcpy.mapping.Layer(Fname), "BOTTOM")
                arcpy.mapping.RemoveLayer(df, arcpy.mapping.Layer(Fname))
               else:
                   pass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.