Has anyone successfully accomplished this? Seriously I’m not being sarcastic.
What I presumed to be a very simple task has me flummoxed. Simply adding a featureclass as a layer to an mxd.
All commands work perfectly when run in a geoprocessing python session, yet they fail when running in a standalone script. Is it maybe something in my environment settings? I’ve tried to cover them all here.
I’ve tried writing the layers to disk then reading them in, adding with AddLayer and AddtoGroup, insertLAyer etc. I can get a layer to add when pulling from disk yet it won’t display in the TOC (you can see it's there if I iterate the layers). I have RefreshTOC and RefrenceActiveView
Here’s a test script I’ve been playing with.
What I presumed to be a very simple task has me flummoxed. Simply adding a featureclass as a layer to an mxd.
All commands work perfectly when run in a geoprocessing python session, yet they fail when running in a standalone script. Is it maybe something in my environment settings? I’ve tried to cover them all here.
Code:
# Set enviroment
arcpy.env.workspace
arcpy.env.scratchGDB
arcpy.env.scratchFolder
Here’s a test script I’ve been playing with.
Code:
# 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()