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()
