Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have the following code which is supposed to add a one group layer to each of the maps. In this case the group layer is always the same.

Desired output:

Map1 = 1 group layer added
Map2 = 1 group layer added
Map3 = 1 group layer added

However this is the result I am getting:

Map1 = 1 group layer added
Map2 = 2 group layers added
Map3 = 3 group layers added

Can someone tell me why/how the results are being compounded?

Note: I used to have this code within a "for" loop. Split it out so could try to figure out why not working correctly.

    SDESurveyed = (r"M:\projects\SDE Surveyed Layers")

    #This grouplayer is blank. Used because Python currently cannot create a grouplayer on it's own
    ClientLayer = arcpy.mapping.Layer(r"F:\TOOLS\PythonTools\ClientSurveyedData.lyr")

    #Look for group layer the matches Client name
    for filename in os.listdir(SDESurveyed):
        if fnmatch.fnmatch(filename[:-4], Client):
            surveyedFile = SDESurveyed + "\\" + filename
            #Create a layer file to the path
            SDElayer = arcpy.mapping.Layer(surveyedFile)


    #SDEkeys = ["AC_","CN_","AM_"]
   # for item in mxd:
        #if item in SDEkeys:
            #imxd = mxd[item]
    imxd = mxd["CN_"]
    print imxd
    df = arcpy.mapping.ListDataFrames(imxd,"Project Area")[0]
    for lyr in arcpy.mapping.ListLayers(imxd, "*", df):
        if lyr.name == "ClientSurveyedData":
            arcpy.mapping.RemoveLayer(df, lyr)
            print("PREVIOUS CLIENT DATA LAYER REMOVED...")
    print("ADDING NEW CLIENT DATA LAYER...")

    targetClientLayer = arcpy.mapping.ListLayers(imxd, "COMPANY SURVEYED DATA", df)[0]

    # Add the layer to the layer group
    arcpy.mapping.AddLayerToGroup(df, targetClientLayer, ClientLayer, "BOTTOM")

    #Target Layer to where layer will open 
    targetGroupLayer = arcpy.mapping.ListLayers(imxd, "ClientSurveyedData", df)[0]

    # Add the layer to the layer group
    arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, SDElayer, "BOTTOM")
    print "LOOPED"
    imxd.save()
    #del targetClientLayer
    #del targetGroupLayer

    imxd = mxd["AM_"]
    print imxd
    df = arcpy.mapping.ListDataFrames(imxd,"Project Area")[0]
    for lyr in arcpy.mapping.ListLayers(imxd, "*", df):
        if lyr.name == "ClientSurveyedData":
            arcpy.mapping.RemoveLayer(df, lyr)
            print("PREVIOUS CLIENT DATA LAYER REMOVED...")
    print("ADDING NEW CLIENT DATA LAYER...")

    targetClientLayer = arcpy.mapping.ListLayers(imxd, "COMPANY SURVEYED DATA", df)[0]

    # Add the layer to the layer group
    arcpy.mapping.AddLayerToGroup(df, targetClientLayer, ClientLayer, "BOTTOM")

    #Target Layer to where layer will open 
    targetGroupLayer = arcpy.mapping.ListLayers(imxd, "ClientSurveyedData", df)[0]

    # Add the layer to the layer group
    arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, SDElayer, "BOTTOM")
    print "LOOPED"
    imxd.save()

    imxd = mxd["AC_"]
    print imxd
    df = arcpy.mapping.ListDataFrames(imxd,"Project Area")[0]
    for lyr in arcpy.mapping.ListLayers(imxd, "*", df):
        if lyr.name == "ClientSurveyedData":
            arcpy.mapping.RemoveLayer(df, lyr)
            print("PREVIOUS CLIENT DATA LAYER REMOVED...")
    print("ADDING NEW CLIENT DATA LAYER...")

    targetClientLayer = arcpy.mapping.ListLayers(imxd, "COMPANY SURVEYED DATA", df)[0]

    # Add the layer to the layer group
    arcpy.mapping.AddLayerToGroup(df, targetClientLayer, ClientLayer, "BOTTOM")

    #Target Layer to where layer will open 
    targetGroupLayer = arcpy.mapping.ListLayers(imxd, "ClientSurveyedData", df)[0]

    # Add the layer to the layer group
    arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, SDElayer, "BOTTOM")
    print "LOOPED"
    imxd.save()
share|improve this question
    
Are your RemoveLayers really needed? It seems like you are just trying to empty your group layer which could be brought in afresh from your empty layer file. I suspect that in trying to do this your mxd.saves are keeping it in each successive map. –  PolyGeo Oct 1 '12 at 22:42
    
The RemoveLayers is there because say the program is run once and the layer is added to the map. If the program is run again I don't want it to add another layer if it exists. I have tried doing a arcpy.mapping.exists if statement, but found the removeLayer and add fresh worked better in my situation. –  TForward Oct 1 '12 at 23:35
    
You could perhaps delete the MapDocument object and then create it afresh rather than trying to empty it. –  PolyGeo Oct 1 '12 at 23:47
add comment

1 Answer

up vote 1 down vote accepted

I ending up getting it to work by doing this...

However I feel like this about my code http://imgur.com/gallery/x0ml8

SDEkeys = ["AC_","CN_","AM_"]

    addSDE = True
    for item in mxd:
        if item in SDEkeys:
            imxd = mxd[item]
            print imxd
            df = arcpy.mapping.ListDataFrames(imxd,"Project Area")[0]
            for lyr in arcpy.mapping.ListLayers(imxd, "*", df):
                if lyr.name == "ClientSurveyedData":
                    arcpy.mapping.RemoveLayer(df, lyr)
                    print("PREVIOUS CLIENT DATA LAYER REMOVED...")
            print("ADDING NEW CLIENT DATA LAYER...")

            targetClientLayer = arcpy.mapping.ListLayers(imxd, "COMPANY SURVEYED DATA", df)[0]

            # Add the layer to the layer group
            arcpy.mapping.AddLayerToGroup(df, targetClientLayer, ClientLayer, "BOTTOM")

            #Target Layer to where layer will open 
            targetGroupLayer = arcpy.mapping.ListLayers(imxd, "ClientSurveyedData", df)[0]

            # Add the layer to the layer group

            if addSDE == True:
                arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, SDElayer, "BOTTOM")
                addSDE = False
            else:
                pass
            print "LOOPED"
            imxd.save()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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