Select to view content in your preferred language

Obtain Index of Layer Within a Group Layer in Python

4440
4
02-14-2012 03:55 AM
MichaelVolz
Esteemed Contributor
To All ArcGIS Python Users:

I would like to create a python script where I can remove a layer from an mxd and replace it with a new layer that is placed in the exact same location.  It is essential that this script would be able to replace the layer in a Group Layer as this is how it exists in hundreds of mxds.  I had a VBA script that performed this type of task, but it did not work with Group Layers as I was unable to get the layer index within the Group Layer.  As such I had to perform the Group Layer individual layer replacement manually which was quite time consuming.  Does python give you the ability to obtain the index of a specific layer within a Group Layer?  Any help or hints regarding this topic are greatly appreciated.  Thank you.
Tags (2)
0 Kudos
4 Replies
MathewCoyle
Honored Contributor
0 Kudos
MichaelVolz
Esteemed Contributor
Thank you Mathew, but this solution does not solve my problem.  But you did point me to an area in the Help documentation where the InsertLayer method is found.  I believe the InsertLayer method might work for me as it works with Group Layers, according to the Help documentation, and you can insert it before or after a target layer anywhere in the Group, not just at the top or bottom.  In my scenario I can have a group layer with 5 layers in it and I want to replace either the 2nd, 3rd, or 4th layer.
0 Kudos
BenHolmes
Emerging Contributor
This should do it, but you would need to put it in a loop to go through your mxds

import arcpy

inMXD = r'c:\test.mxd'
outMXD = r'c:\test1.mxd'
rLayer = 'test3' #Name of reference layer
mLayer = 'test4' #Name of layer to be replaced and moved
lyrFile = arcpy.mapping.Layer(r'c:\Test4.lyr') #Layer file used to replace layer

mxd = arcpy.mapping.MapDocument(inMXD)
for df in arcpy.mapping.ListDataFrames(mxd):           #loop data frames
    for lyr in arcpy.mapping.ListLayers(mxd,"",df):    #loop layers
        if lyr.isGroupLayer == 1:                      #Is layer a group layer
            for glyr in arcpy.mapping.ListLayers(lyr): #loop layer in group layer
                if glyr != lyr:                        #Not Group Layer Name
                    if glyr.name.lower() == mLayer:    #Layer to be replaced and moved
                        arcpy.mapping.RemoveLayer(df, glyr) #Remove the layer
                        arcpy.mapping.AddLayer(df, lyrFile) #Add layer based on Layer File

                    if glyr.name.lower() == rLayer:    #Layer to be used to reference position of move layer
                        refLayer = glyr                #Set ref layer

    #Have to loop through layers again as layer is added outside the group layer
    for lyr in arcpy.mapping.ListLayers(mxd,"",df): #Start new loop of layers
        if lyr.name.lower() == mLayer:  #Is move layer
            moveLyr = glyr  #Set move layer

#Move layer to required position
arcpy.mapping.MoveLayer(df, refLayer, moveLyr, "BEFORE") 

mxd.saveACopy(outMXD)
del mxd



-Ben
0 Kudos
MichaelVolz
Esteemed Contributor
Thanks for your reply with code Ben.  I believe that might work, but I think the following Post has a better solution:

replaceDataSource not working in the same python forum

The method that appears to work best is the UpdateLayer method.  This incorporates RemoveLayer and AddLayer in the same method and the task of tracking the index of the layer is bundled up for free with this method.
0 Kudos