Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am attempting to change the name of the layer. The following code changes the name of the layer but then removes the layer from the map, and I am not sure why.

Note: I don't have any remove layer statements.

for lyrs in arcpy.mapping.ListLayers(mxd, "*", df):
    if lyrs.name == "project_ws":
        lyrs.name = "project_prow"

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Oh, you're doing this in a geoprocessing tool. GP tools try to protect the TOC from manipulation (such as suppressing the outputs of tools called within tools from going to the TOC), so you're going to need to tell it explicitly that you mean to keep this layer around. "But I haven't added a layer!" -- it goes by name, not by reference, so as far as it's concerned "project_prow" is some new rogue layer.

Make a new output parameter of type Feature Layer, and use SetParameterAsText to set its value to the lyrs.name.

share|improve this answer
    
Yes in a geoprocessing tool, ekk forgot to mention that :( –  TForward Jun 21 '12 at 23:48
    
How could you tell he was doing it in a geoprocessing tool? Because it wasn't working? –  valveLondon Jun 25 '12 at 22:13
    
Because of the behavior of how the TOC was being manipulated, yes. The script tool thought the "new" layer was intermediate output after it finished and clobbered it. –  Jason Scheirer Jun 25 '12 at 23:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.