2

Assuming that I have an idealized .mxd TOC structure with group layers that contain group and subgroup layers which have the same name like in the following example:

Layers

  • GroupLayer1
    • SubGroupLayer1
      • Raster1.tif
      • Raster33.tif
    • SubGroupLayer2
      • Raster44.tif
  • GroupLayer2
    • SubGroupLayer1
      • Raster2.tif
      • Raster4.tif
    • SubGroupLayer2
      • Raster7.tif

Is it possible to add data using an 'absolute' layer path with ArcPy?

For example, if I want to add a raster layer SampleRaster.tif to the following place GroupLayer2/SubGroupLayer2 : how could I do this in ArcPy?

I know how to add a group layer and then add the specific layer (e.g. solution in this question Adding new group layer with ArcPy?) but have not found a solution how to add it if two layers have the same name.

My code that I have so far is pasted below

 # add rasters to .mxd project
    import arcpy

    arcpy.env.workspace = r'D:\Data\Rasters'
    rasters = arcpy.ListRasters("*", "TIF")

    for raster in rasters:
        # add raster to .mxd
        mxd = arcpy.mapping.MapDocument(r"D:\Data\Test.mxd")
        df = arcpy.mapping.ListDataFrames(mxd)[0]

        # here I need to list the layers in the subgroup "SubGroupLayers2", but what to do when the subgroup layers names are equal? Is there a way to indicate an absolute path (e.g. Layers/GroupLayer2/SubGroupLayer2 here?)
        targetGroupLayer = arcpy.mapping.ListLayers(mxd, "SubGroupLayer2", df)[0]

        # I am then using another layer to apply the symbology to the raster and update the layer
        newlayer = arcpy.mapping.Layer(raster)
        newlayer.visible = False
        arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, newlayer, "BOTTOM")
        updateLayer = arcpy.mapping.ListLayers(mxd, raster, df)[0]
        sourceLayer = arcpy.mapping.Layer(r"D:\Data\Colormap.lyr")
        arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True) 
        mxd.save()
3
+50

You can get the group/subgroup path using the longName attribute:

...

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "SubGroupLayer2", df)[0]
print targetGroupLayer.longName

...

So you could loop through your targetGroupLayer list and create an if/then statement, based on the .longName, to find and insert layers to the specific group/subgroup.

Example code:

#Instead of using the index 0, return the whole list. 
grouplyrs = arcpy.mapping.ListLayers(mxd, "SubGroupLayer2", df)
#Then loop through that list. 
for grplyr in grouplyrs: 
    if grplyr.longName == "GroupLayer2\SubGroupLayer2": # add your desired full path here, from examples 
        targetGroupLayer = grplyr
# return to your code in progress.
newlayer = arcpy.mapping.Layer(raster)
newlayer.visible = False
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, newlayer, "BOTTOM")
...

Additional resources:

  • Could you give me a working example with the dummy dataset above, how would you incorporate the if statement? – redshoe Apr 2 at 12:34
  • 2
    Instead of using the index 0, return the whole list. Then loop through that list. e.g. grouplyrs = arcpy.mapping.ListLayers(mxd, "SubGroupLayer2", df); for grplyr in grouplyrs: if grplyr.longName == "": # add your desired full path here, from examples targetGroupLayer = grplyr – smiller Apr 2 at 13:26
  • 2
    Added this and some additional resources to the answer. – smiller Apr 2 at 13:29

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.