# Set enviroment arcpy.env.workspace arcpy.env.scratchGDB arcpy.env.scratchFolder
# Add layers to the map mxd = arcpy.mapping.MapDocument(r"C:\Users\mymap.mxd") # List data frame and layers frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] refFeatureLayer = arcpy.mapping.ListLayers(mxd,'*New_Layer*',frame)[0] # create a new layer from an existing featureclass travel_route_1 = "Travel_Routes" tempLayer = arcpy.MakeFeatureLayer_management(travel_route_1,'my test layer') # Save layer to disk arcpy.SaveToLayerFile_management (tempLayer, r'C:\Users\test layer.lyr') # List data frame and layers - just to refresh the list, shouldn't need to do this. frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] refFeatureLayer = arcpy.mapping.ListLayers(mxd,'*test*',frame)[0] # Get the layer addLayer = arcpy.mapping.Layer(r'C:\Users\test layer.lyr') # Add the layer to the dataframe arcpy.mapping.AddLayer(frame, addLayer, "BOTTOM") # List data frame and layers - just to refresh the list, shouldn't need to do this. frame = arcpy.mapping.ListDataFrames(mxd,'Dataframe')[0] refFeatureLayer = arcpy.mapping.ListLayers(mxd,'*test*',frame)[0] # Finally move the layer movelayer = arcpy.mapping.ListLayers(mxd,'*my*',frame)[0] arcpy.mapping.MoveLayer(frame,refFeatureLayer,movelayer,"AFTER") arcpy.RefreshTOC() arcpy.RefreshActiveView()
Solved! Go to Solution.
Jeff,
Thanks so much for getting back to me. I've been running into dead ends on
this. I'm running 10.2.2 so it's my understanding that I shouldn't have the
AddLayer bug (is that correct?).
Your suggestion worked! I couldn't be happier or more confused as
to why this works but the other (ESRI resources suggested) way doesn't.
Here's what I ended up running successfully:
precipLayer = arcpy.mapping.Layer(r"D:\Active\ArcPY\10x\ScrumWorks\Data\Drive_A\basin_utmcopy.shp")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
#arcpy.mapping.AddLayer(df, precipLayer.getOutput(0), "TOP") # GOT ERROR: getOutput(0) doesn't exist
arcpy.mapping.AddLayer(df, precipLayer, "TOP")
I'll update the other post with your suggestion.
Thank you very much!
Madeline
I’m glad my suggestion helped. I can’t explain why you are seeing what you are seeing. One difference is that I’m on 10.3 (not released yet) but I’m not aware of changes. This issue may be a core arcpy issue and not specific to arcpy.mapping (my area of development). I’ll forward this onto someone on that team to see if there was a known issue that was addressed.
Anyway, I hope you can continue forward,
Jeff