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

0

 



In this article you will learn how to add 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 include an additional layer to represent the spatial data (Shapefile, feature Class, among others), either by decision of the end customer, your immediate boss or because it was omitted in the beginning, among the reasons that I can glimpse at this time. The traditional solution would be the following:


How to add an ArcGIS / ArcMap layer?


Option 1:


1) Open the MXD file.


2) Right click on the Dataframe (a) in which the layer will be added and press the Activate button (b) of the displayed submenu.


3) Click on the Add Data button (c) of the Standard toolbar.


4) Browse to the file (d) that contains the spatial information of interest (You may have to press the Connect Folder button to access the location [e]). Finally press the Add button (f).



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



5) Save the MXD file.


Option 2:


1) Open the MXD file.


2) Right click on the Dataframe in which the layer will be added and press the Add data... button (a) of the displayed submenu. (a) of the displayed submenu. 


3) Browse to the file (b) containing the spatial information of interest (You may need to press the Connect Folder button to access the location [c]).



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



4) Save the MXD file.


Option 3 (Only valid for some file types)


1) Open the MXD file.


2) Locate the file in the folder (a) in the explorer of the computer and drag it to the desired Dataframe (b).


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



3) Save the MXD file.


Note: the tested files that can be dragged are .shp, .png, .tiff, .ecw. However, you can always try this option.


Option 4


1) Open the MXD file.


2) Press the Catalog button (a) of the Standard toolbar.


3) Browse to the file containing the spatial information of interest (b) in the Catalog window (You may need to press the Connect Folder button to access the location [c]).


4) Once the file is located in the Catalog window, drag the desired file to the target Dataframe (d).


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



5) Save the MXD file.


For all the options so far presented, in case the correction must be made to one or two files it is not a big deal, but if the correction must be made to dozens of images and/or maps this can consume a lot of your time, and this without counting that the layer symbology must be fixed. Considering the symbology adjustment I present you option 5.


How to drag several files from the Catalog (ArcCatalog) ArcGIS / ArcMap window?


Note: To drag more than one file from the Catalog window, press the “Toggle Contents Panel” button (a), which will display a box at the bottom of the window (b); select the folder, database, or dataset at the top (c), and in the new lower box, you can select several files by pressing Shift+click (left click), same files that you can drag once shaded (d) to the table of contents of ArcMap (e). I owe this knowledge to a great friend of DV initials. To return to normal, press the “Toggle Contents Panel” button a couple more times.


Descripción gráfica del procedimiento para agregar varias capas desde ArcCatalog (Catalog)



Option 5


1) Open the MXD file.


2) Right click on the Dataframe where the layer will be added and press the Add data... button of the displayed submenu.


3) Adjust the symbology and save a .lyr file as seen in my article “PYTHON GIS - Modifying the symbology of a layer in several ArcGIS files” under the subtitles “How to modify the symbology of an ArcGIS / ArcMap layer” and “Creating a layer symbology file”


4) Save the MXD file


5) For the next MXD files, just open them and drag the .lyr file (a) from the operating system explorer to the ArcMap table of contents (b).






Since option 5 is the most complete option, it is the one that we will automate from step 5 onwards since it is useful to add a layer and to have the desired symbology without having to make later adjustments, we will do this automation using Python code (You do not need to know how to program to do it). 


How to batch add layers in several ArcGIS / ArcMap files using Python?


Step 1: Open the MXD file.


Step 2: Right click on the Dataframe where the layer will be added and press the Add data... button of the displayed submenu.


Step 3: Adjust the symbology and save a .lyr file as seen in my article “PYTHON GIS - Modifying the symbology of a layer in several ArcGIS files” under the subtitles “How to modify the symbology of an ArcGIS / ArcMap layer” and “Creating a layer symbology file”


Step 4: Close the MXD file (no need to save changes).


Step 5: Open a blank MXD file.


Step 6: 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 7: Backup the original MXD files.


Step 8: Copy the following code into a notepad and in line #02 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
arcpy.env.workspace = ws = r"C:\EJEMPLO\EJEMPLO"                       #02
mxd_list = arcpy.ListFiles("*.mxd")                                    #03
for mxd in mxd_list:                                                   #04
    mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))             #05
    df = arcpy.mapping.ListDataFrames(mxd, "DATAFRAME")[0]             #06
    addLayer = arcpy.mapping.Layer(r"C:\EJEMPLO\EJEMPLO\LayerMod.lyr") #07
    arcpy.mapping.AddLayer(df, addLayer, "TOP")                        #08
    mxd.save()                                                         #09
                                                                       #10
del mxd_list                                                           #11
                                                                     


Step 9: In line #06 of the code replace DATAFRAME with the name of the dataframe where the layer will be stored (It is a good practice that if there is more than one dataframe within a MXD file, to name each one uniquely).


Step 10: In line #07 of the code replace the text C:\EJEMPLO\EJEMPLO\LayerMod.lyr with the location and name of the .lyr file created in step 3.


Step 11: Optionally, replace the word TOP in line #08 of the code with BOTTOM, this will tell the script to add the layer below all existing layers, otherwise it will add it above. 


Step 12: 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 the layer added in each MXD file.


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