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'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?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

from your code i have some remarks, which 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
share|improve this answer
1  
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. –  PolyGeo Oct 13 '12 at 22:58
    
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. :) –  Oliver Burdekin Oct 14 '12 at 0:16
    
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! –  Oliver Burdekin Oct 14 '12 at 4:07
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.