Reyalino Posted December 26, 2017 Report Share Posted December 26, 2017 Hi All, Using arcpy mapping module I am trying to add multiple shapefiles import arcpy arcpy.env.workspace = "D:/Directory/Map" mxd = arcpy.mapping.MapDocument("D:/Directory/Map/layout.mxd") listFC = arcpy.ListFeatureClasses() for fc in listFC: df = arcpy.mapping.ListDataFrames(mxd)[0] layer = arcpy.mapping.Layer(fc) arcpy.mapping.AddLayer(df,layer,"TOP") When I run those script via jupyter notebook only 1 shapefile is added to the arcgis. But when I run it through Python Window on ArcGIS it adds all the shapefiles but also sometimes only add 1 shapefile. Not sure why this happen. Could someone give me a clue? thanks ( I am using ArcGIS 10.5 Not the original :-D) regards Quote Link to comment Share on other sites More sharing options...
yousef2233 Posted December 26, 2017 Report Share Posted December 26, 2017 dear Reyalino, put df = arcpy.mapping.ListDataFrames(mxd)[0] before "for loop", import arcpy arcpy.env.workspace = "D:/Directory/Map" mxd = arcpy.mapping.MapDocument("D:/Directory/Map/layout.mxd") listFC = arcpy.ListFeatureClasses() df = arcpy.mapping.ListDataFrames(mxd)[0] for fc in listFC: layer = arcpy.mapping.Layer(fc) arcpy.mapping.AddLayer(df,layer,"TOP") 2 Quote Link to comment Share on other sites More sharing options...
Reyalino Posted December 26, 2017 Author Report Share Posted December 26, 2017 thanks for your fast response yousef233. I have put the dataframelist before the loop it works but after the second trial it only added 1 single feature. I have made several trials by run the code via python window and jupyter notebook. I am just wondering, why the code sometime running properly but not in the other day. Quote Link to comment Share on other sites More sharing options...
yousef2233 Posted December 27, 2017 Report Share Posted December 27, 2017 do you work with layout.mxd or "current" ? do you save it after adding layers? works fine here import arcpy arcpy.env.workspace = r"C:\Users\Default.gdb" mxd = arcpy.mapping.MapDocument(r"D:\Untitled.mxd") listFC = arcpy.ListFeatureClasses() df = arcpy.mapping.ListDataFrames(mxd)[0] for fc in listFC: layer = arcpy.mapping.Layer(fc) arcpy.mapping.AddLayer(df,layer,"TOP") mxd.save() del mxd Quote Link to comment Share on other sites More sharing options...
Reyalino Posted December 27, 2017 Author Report Share Posted December 27, 2017 I am working with "layout.mxd". I have tried on another desktop and it works. But not in my desktop. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.