PYTHON GIS - Modify symbology of a layer in several ArcGIS files

0

 



In this article you will learn how to modify the symbology of a specified layer stored in one or more MXD files using Python.



It often happens that during the execution of a project in which different graphical outputs and/or maps have been made, for some reason it becomes necessary to modify the symbology used to represent the spatial data (Shapefile, feature Class, among others), either because the end customer, your immediate boss or because there have been changes in the spatial information per-se, among the reasons that I can glimpse at this time. The traditional solution would be the following:


How to modify the symbology of an ArcGIS / ArcMap layer?


Option 1:


1) Open the MXD file.


2) Double click on the layer to which you want to modify the symbology (It is the same as right click [a] and press the “Properties...” button [b]), go to the “Properties...” button [c]), go to the “Properties...” button [d] and click on “Properties...”. [b]), go to the Symbology tab (c).


Se muestra como acceder a la pestaña Symbology en las propiedades de una capa almacenada en un archivo de ArcGIS / ArcMap / MXD


3) Adjust Symbology.


Muestra el cambio de color en la capa cómo ejemplo de ajuste de la simbología


4) Click on the OK button.


Muestra un ejemplo del final de la edición de la simbología


5) Save the MXD file.


6) Repeat the procedure for each MXD file.


A way to do it manually a little more agile, would be the following:


Option 2:


1) Open the MXD file.


2) Double click on the layer to which you want to modify the symbology (It is the same as right click [a] and press the “Properties...” button [b]), go to the Symbology tab [c]), go to the Symbology tab [d] and click on the “Properties...” button [e]). [b]), go to the Symbology tab (c).


Se muestra como acceder a la pestaña Symbology en las propiedades de una capa almacenada en un archivo de ArcGIS / ArcMap / MXD



3) Adjust Symbology.


Muestra el cambio de color en la capa cómo ejemplo de ajuste de la simbología

4) Click on the accept button.


Muestra un ejemplo del final de la edición de la simbología


Create a layer symbology file


5) Save the new layer symbology as a .lyr file; to do this right click on the layer (a), press the “Save As Layer File...” button (b) then locate a directory to save the new file and press the “Save” button (c).


Se muestra cómo puedes guardar la simbología de una capa como archivo .lyr

6) Save the MXD file.


Importing symbology into ArcGIS from .lyr file


7) Open another MXD file, double click on the layer to which you want to modify the symbology (It is the same as right click [a] and press the “Properties...” button [b]), go to the “Properties...” button [c]), go to the “Properties...” button [d] and click on the “Properties...” button [e]. [b]), go to the Symbology tab (c), but instead of modifying the symbology, press the “Import...” button (d), then press the folder icon (e).  


Muestra la ventana Symbology y la sub ventana del botón Import...


8 ) Browse for the .lyr file from step 5 and press the “Add” button (f).


Muestra un ejemplo de selección de archivo .lyr en ArcGIS

9) Press “OK” (g) and press the “OK” button (h), finally save the MXD file.


Muestra los pasos finales al momento de importar la simbología


9) Repeat this step with the remaining map and/or figure files.


This second process indicated from step 7 is the one you will learn how to automate in this article, because although doing it this way is a little better, it can still consume a lot of your time, next I will show you how it can be much more efficient using Python code (Do not worry you do not need to know how to program to do it).


How to batch modify the symbology of an ArcGIS / ArcMap layer using Python?


Step 1: Open the MXD file.


Step 2: Double click on the layer to which you want to modify the symbology, go to the Symbology tab.


Step 3: Adjust the symbology.


Step 4: Click on the “OK” button.


Step 5: Save the new layer symbology as a “.lyr” file.


Note: The steps described so far are illustrated in the previous sections called “Creating a layer symbology file” and “Importing symbology into ArcGIS from .lyr file”.


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


Step 7: Open a blank ArcGIS file.


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


Step 10: Copy into a note blog the following code and replace the text C:\EJEMPLO\EJEMPLO\LayerMod.lyr in line #09 with the name of the .lyr file including the path where it is 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
    for lyr in arcpy.mapping.ListLayers(mxd, "Layer", df):                    #07
        updateLayer = lyr                                                     #08
        sourceLayer = arcpy.mapping.Layer(r"C:\EJEMPLO\EJEMPLO\LayerMod.lyr") #09
        arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)         #10
    mxd.save()                                                                #11
                                                                              #12
del mxd_list                                                                  #13
                                                                     


Step 11: In line #02 of the code replace the path C:\EJEMPLO\EJEMPLO with the path where the MXD files to be modified are stored.


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


Step 13: In line #07 of the code replace Lyr by the name of the layer to be modified.


Step 14: 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 update of the layer symbology 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. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your concerns in the contact page or leave your comment, I will be attentive to respond.


You may like these posts

No comments