PYTHON GIS - How to turn off a layer in several ArcGIS files automatically (ArcMap / MXD)

0

 


In this article you will learn how to turn off a layer in several MXD files using Python.


It often happens that during the execution of a project in which you have been making different graphical outputs and/or maps that for some reason it becomes necessary to remove the display of a layer used to represent certain spatial information (Shapefile, feature Class, among others), either by decision of the end customer, your immediate boss, because it was added in the beginning without being necessary, among the various reasons that I can glimpse at this time, however you do not want to get rid of the layer because it could be used in the future. The traditional solution might be one of the following: 


How to turn off an ArcGIS / ArcMap layer?


Option 1:


1) Open the MXD file.


2) Click on the checkbox (a) of the layer to turn off (box to the left of the layer), which disables the checklist sign (b).



Descripción gráfica del procedimiento para apagar capas según la opción número 1


3) Save the MXD file.  


4) Repeat the procedure for each MXD file.


Option 2:


1) Open the MXD file.


2) Click on the layer to turn off so that it is shaded, once the layer is selected press the space bar (a), which deactivates the checklist sign (b).



Descripción gráfica del procedimiento para apagar capas según la opción número 2


3) Save the MXD file.  


4) Repeat the procedure for each MXD file.


Option 3: (Switching off several layers without a consecutive order)


1) Open the MXD file.


2) Click on the layers to turn off while holding down the Ctrl key, once all layers are selected press the space bar (a), which deactivates the checklist sign (b).



Descripción gráfica del procedimiento para apagar capas según la opción número 3




3) Save the MXD file.   


4) Repeat the procedure for each MXD file.


Option 4: (Switch off several layers in consecutive order)


1) Open the MXD file.


2) Click on the first of the layers to turn off (a) while holding down the Shitf key and press the last of the layers (b), once all layers are selected press the space bar, which deactivates the checklist sign (c). It works even if you reverse the order of layer selection during steps (a) and (b).



Descripción gráfica del procedimiento para apagar capas según la opción número 4





3) Save the MXD file. 


4) Repeat the procedure for each MXD file.


In case the correction must be done on one or two files it is not a big deal, but if the correction must be done on dozens of images and/or maps in MXD format this can consume a lot of your time, next, I will show you how it can be much more efficient using Python code (Don't worry you don't need to know how to program to do it).



How to batch turn off layers in multiple ArcGIS / ArcMap files using Python?


Step 1: Open a blank MXD file.


Step 2: Open the Python window in the Standard toolbar.



Muestra la ubicación de la ventana Python dentro de la barra de herramientas Standard (estándar)


Step 3: Backup the original MXD files.


Step 4: Copy the following code into a notepad and in line #05 of the code replace the path C:\EJEMPLO\EJEMPLO with the path where the MXD files to be modified are stored.



import arcpy, os                                            #01
import sys                                                  #02
reload(sys)                                                 #03
sys.setdefaultencoding('utf-8')                             #04
arcpy.env.workspace = ws = r"C:\EJEMPLO\EJEMPLO"            #05
mxd_list = arcpy.ListFiles("*.mxd")                         #06
for mxd in mxd_list:                                        #07
    mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))  #08
    layers = arcpy.mapping.ListLayers(mxd)                  #09
    for lyr in layers:                                      #10
        if lyr.name == "LAYER":                             #11
            lyr.visible = False                             #12
    mxd.save()                                              #13
del mxd_list                                                #14
                                                                     


Step 5: In line #11 of the code replace LAYER with the name of the layer to shut down.


Step 6: Copy the modified code into the Python window and press the Enter key twice.


After a few seconds you will see how the modification date of the ArcMap files starts to update, and when you open it you will notice that the display of the layer in each MXD file has been turned off.


I hope you enjoyed this article, soon I will upload a video explaining the procedure that you can consult in this blog, as well as another article where I will explain how to add layers within a particular layer group. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your questions in the contact page or leave your comment, I will be attentive to respond.  


You may like these posts

No comments