5

I'm working on a script in ArcMap 10.1 that takes a screenshot and I need to know how to extinguish (turn off) a layer in my table of contents.

  • 1
    By extinguish, do you mean turn off or remove completely from TOC? Also, what version of ArcMap are you using? Please edit your question to clarify. – Paul Oct 4 '13 at 15:59
  • I am using arcmap 10.1'm doing a build script makes certain escreeshot but he does not need specific layers so would turn the layer to not see – user22547 Oct 4 '13 at 18:50
17

The Layer object has a read/write property, visible, that can be used to turn on/off layers.

This is some basic code that should get you started:

#A list of layer names that you want to be turned off.
names = [x,y,z,etc]

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(, mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)

for layer in layers:
  if layer.name in names:
    layer.visible = False

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
  • 2
    Don't forget to add arcpy.RefreshActiveView() to the end of your script to ensure that the data frame is refreshed as well, or the layer would still be visible in the map view, but it would be checked off in the TOC. – RyanDalton Oct 4 '13 at 19:35
  • How can i turn off all layers without the argument names? – newGIS Dec 15 '14 at 6:50
  • 1
    @Y.Y.C it's pretty simple: just remove the if statement checking for names and iterate over all the items in the layers list. – Paul Dec 19 '14 at 4:52
  • I tried to create a standalone script in IDLE with this but it did not work. Please could you provide a demonstration? I have a related question here: gis.stackexchange.com/questions/158465/… – ndthl Aug 16 '15 at 7:16
2

I wanted to add something I hope is helpful from my experience. The above answer works great, though if someone does further analysis with the layer than this example, order of use of the .visible property becomes important.

For example, trying to add many "turned off" layers to dataframe as follows:

for lyr in layerlist:
     addlayer = arcpy.mapping.Layer(os.path.join(basepath, lyr))
     arcpy.mapping.AddLayer(your_dataframe, addlayer, "BOTTOM")
     addlayer.visible = False #Puts the property after the .AddLayer analysis- won't turn off the layer
# Do further analysis

Won't turn off the layers as you add them. But, the following will:

for lyr in layerlist:
     addlayer = arcpy.mapping.Layer(os.path.join(basepath, lyr))
     addlayer.visible = False # Moves the property before the .AddLayer analysis turns off layer
     arcpy.mapping.AddLayer(your_dataframe, addlayer, "BOTTOM")
# Do further analysis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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