Select to view content in your preferred language

adding layers in a standalone script

5621
11
Jump to solution
04-15-2013 07:27 PM
JonPedder
Deactivated User
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.

# Set enviroment arcpy.env.workspace arcpy.env.scratchGDB arcpy.env.scratchFolder


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.

# 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()
Tags (2)
0 Kudos
11 Replies
by Anonymous User
Not applicable

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

0 Kudos
JeffBarrette
Esri Regular Contributor

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

0 Kudos