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?
1 Answer 1
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]
-
2I 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.2012年10月13日 22:58:01 +00:00Commented Oct 13, 2012 at 22:58
-
1Thanks 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– Oliver Burdekin2012年10月14日 00:16:47 +00:00Commented Oct 14, 2012 at 0:16
-
1Got 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– Oliver Burdekin2012年10月14日 04:07:45 +00:00Commented Oct 14, 2012 at 4:07
-
1geogeek, how do it for several mxd's?newGIS– newGIS2015年01月11日 12:50:22 +00:00Commented Jan 11, 2015 at 12:50
-
2i've made an answer in my editgeogeek– geogeek2015年01月12日 15:11:30 +00:00Commented Jan 12, 2015 at 15:11
Explore related questions
See similar questions with these tags.