13

I've been battling with adding layers to the TOC for some time now. I have managed to single layers added but need to loop through a folder and add all layers. Shapefiles I cannot get to add at all. Only layer files. Below is the code of where I'm at:

for layer in shp_List:
mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
outlayer = lyr
layerfile = os.path.join(base_Folder, lyr + ".lyr")
arcpy.MakeFeatureLayer_management(layer, outlayer)
arcpy.SaveToLayerFile_management(outlayer, layerfile, "ABSOLUTE")
addlayer = arcpy.mapping.Layer(layerfile)
arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")
#addLayer = arcpy.mapping.Layer(layer)
#arcpy.mapping.AddLayer(dataFrame, addLayer, "BOTTOM")
#arcpy.RefreshTOC()
#arcpy.RefreshActiveView()
del addlayer, mxd

The above code will add each layer to the TOC, then infuriatingly removes it and adds the next one. When the whole script finishes there is nothing left in the TOC. I've tried adding this script to a model and creating a derived output parameter which is then added as a model parameter with "add to display" checked. I've also checked the geoprocessing options to make sure the box to add to display is checked. I'm running Arc 10 (no service packs). Can anyone help?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Oct 13, 2012 at 17:23

1 Answer 1

12

You need to create the MapDocument and DataFrame objects outside of the loop that runs through your layers. Otherwise you are starting with the original map each time.

so i will correct in this code:

mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
for layer in shp_List:
 outlayer = layer + "_lyr" 
 layerfile = os.path.join(base_Folder, layer + ".lyr")
 arcpy.MakeFeatureLayer_management(layer, outlayer)
 arcpy.SaveToLayerFile_management(outlayer, layerfile, "ABSOLUTE")
 addlayer = arcpy.mapping.Layer(layerfile)
 arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")
 #addLayer = arcpy.mapping.Layer(layer)
 #arcpy.mapping.AddLayer(dataFrame, addLayer, "BOTTOM")
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
 #del addlayer, mxd

To loop in a list of mxd files in the same folder of the script, you can do it like this :

for mxd_name in mxd_names:
 mxd = arcpy.mapping.MapDocument(os.path.dirname(os.path.realpath(__file__))+"\\"+mxd_name+".mxd")
 dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
answered Oct 13, 2012 at 20:07
5
  • 2
    I was looking for @geogeek's remarks but they appear to be absent. In any event, as his corrected code shows, you need to create the MapDocument and DataFrame objects outside of the loop that runs through your layers. Otherwise you are starting with the original map each time. Commented Oct 13, 2012 at 22:58
  • 1
    Thanks for the clarification PolyGeo. I've implemented the changes. However, now just the last file in the list stays in the TOC. All the others appear then disappear. Flummoxed. :) Commented Oct 14, 2012 at 0:16
  • 1
    Got it working! All in the indentation right? The layer names coming out are still a mees but that's just aesthetics. Thanks so much for your help peeps! Commented Oct 14, 2012 at 4:07
  • 1
    geogeek, how do it for several mxd's? Commented Jan 11, 2015 at 12:50
  • 2
    i've made an answer in my edit Commented Jan 12, 2015 at 15:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.