1

I work with ArcGIS 10.3.1. I my map i have 200 layers. some of them repeat several times. Is it possible with python code to remove all duplicated layers from table of content, that have the same group layer name?

all the layers are in different group layers- each group layer has different name except the duplicated. I know the "Delete Identical" tool- i have only basic license.

My question differ from Remove Duplicate items in legend because i seek for duplicated group layers (with the same name).

  • Are you only looking for Group Layers and not actual layers? So it doesn't matter what (sub) layers are contained in those groups? – Midavalo Jun 1 '16 at 9:24
  • thats right Midavalo – newGIS Jun 1 '16 at 9:26
  • A picture of such a Table of Contents might help to convey what you are after, and as for all ArcPy questions, can you post your code snippet so far, please? – PolyGeo Jun 1 '16 at 9:44
  • 1
    @Mr.Che The closure is correct due to lack of code attempt. However it's odd you were able to post after it was put on hold - I was half-way through an answer when it was put on hold and I can't post. – Midavalo Jun 1 '16 at 9:53
  • @Midavalo It's not locked, but it is On Hold and thus closed for answers. I'll re-open it so that you can post your answer but I would like to see ArcPy questions always accompanied by a code snippet, as per the close reason, and the Meta Q&A at meta.gis.stackexchange.com/questions/3994/… – PolyGeo Jun 1 '16 at 9:54
4

Something like the following should work. It creates a list of all the layers in your MXD, then loops through the Group Layers and adds them to a list. If the Group Layer already exists in the list (i.e. a duplicate) then it's removed instead.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
allLayers = arcpy.mapping.ListLayers(mxd)

groupLyrList = list()

for layer in allLayers:
    if layer.isGroupLayer:
        if layer.name not in groupLyrList:
            groupLyrList.append(layer.name)
        elif layer.name in groupLyrList:
            arcpy.mapping.RemoveLayer(df, layer)
4

This code will do all the job for you, this code is tested - it works.

import arcpy

mxd = arcpy.mapping.MapDocument(r"G:\!Some_project_delete\!Some_project_delete.mxd")

groups_list = []

for df in arcpy.mapping.ListDataFrames(mxd): # iterate over dataframes
    for lyr in arcpy.mapping.ListLayers(mxd, "", df): # iterate over layers

        if lyr.isGroupLayer: # if object is group layer
            groups_list.append(lyr.name) # add it to list

    for lyr in arcpy.mapping.ListLayers(mxd, "", df): # iterate over layers again

        # count duplicates in lists
        if lyr.isGroupLayer: # if object is group layer
            print lyr.name, '\t', groups_list.count(lyr.name)

            if groups_list.count(lyr.name) >= 2: # if there are 2 or more duplictes
                arcpy.mapping.RemoveLayer(df, lyr)
                index = groups_list.index(lyr.name) # find object's index in list   
                del  groups_list[index] # delete object in list              

mxd.saveACopy(r"G:\!Some_project_delete\!Some_project_delete_NEW.mxd") # save project to new file
del mxd # release mxd file

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.