0

I need to update 200 MXD files by adding three layers with a specific symbology for each layer. I use ArcMap 10.8 All the layers are in the same folder. All the .mxd files are located in one folder which in diferent sub-folders and sub-sub-folders (not my choice) All the .lyr file are in one folder. All the folders are in different path. I am not familiar with Python environment or ArcPy but I am willing to try to write a script to automate the integration of the 3 layers with associated symbology in all the MXD.

I don't know if it's possible. I checked in French speaking forum with no luck so far. I found two scripts I am not sure on how to adapt them to my target. I tried the following as a total beginner...

import arcpy, os
import arcpy.mapping as mapping

for root, dirs, files in os.walk (r"path to the file containing all the MXD"):
    for f in files:
        if f.endswith(".mxd"):
            mxd = root + '\\' + f
            mxd_doc = mapping.MapDocument(mxd)
            df = mapping.ListDataFrames(mxd_doc)[0]
    addLayer = arcpy.mapping.Layer(r"path_to_the_shapefile_1")
    arcpy.mapping.AddLayer(df ,addLayer ,"TOP")
addLayer = arcpy.mapping.Layer(r"path_to_the_shapefile_2")
arcpy.mapping.AddLayer(df ,addLayer ,"TOP")
addLayer = arcpy.mapping.Layer(r"path_to_the_shapefile_3")
arcpy.mapping.AddLayer(df ,addLayer ,"TOP")
 mapdoc.save()
del mxd, addLayer, mapdoc

How do I apply the .lyr symbology to the layer in the MXD?

4
  • This is nearly a pure Python task. Your indentation is wrong, which is a crucial error in Python. You may need to spend more time learning Python before attempting this script.
    – Vince
    Commented Jul 12, 2022 at 10:46
  • The proper way to code this is to break out the learn Python and learn ArcPy layer management tasks by using a function to do all the MXD manipulation. Then you can work the layer addition and .lyr application as a single focused task (saving a backup before you start breaking anything) and get the rather arcane os.walk usage correct in isolation (don't forget to ignore the backups).
    – Vince
    Commented Jul 12, 2022 at 11:24
  • You can use Update Layer tool to update symbology based on a lyr file desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/… based on your workflow it would probably be better to export each shapefile as a lyr file and just add the lyr file to each mxd instead. Commented Jul 12, 2022 at 13:54
  • Thank you, I wish I had more time, but unfortunatly not as it's a last minute request and I have nobody doing anything about python... Commented Jul 12, 2022 at 14:29

1 Answer 1

1

As per my comment above, if you export the shapefiles with symbology already assigned as lyr files, add the root folder, and paths to your lyr files in the script below. Otherwise you can add the shapefiles and use the Update Layer tool. Please note, I havent used ArcMap in a number of years and the below is some extracts from older workings that I used successfully in the past to do a similar task. You might need to tinker here and there. I recommend copying some MXDs to test folder before running on a production environment. As well as the Add Layer tool you can also use the Insert Layer tool for more control over where the layers get inserted. From memory, if you want the layers added to the legend, further coding is required.

import arcpy
import arcpy.mapping as m
import os

root_folder = r"path\to\root\folder"

add_lyr_1 = r"path\to\file1.lyr"
add_lyr_2 = r"path\to\file2.lyr"
add_lyr_3 = r"path\to\file3.lyr"

## create list of lyr files
add_lyrs = [add_lyr_1, add_lyr_2, add_lyr_3]

mxd_paths = []

## add all mxd paths to a list
for root, dirs, files in os.walk (root_folder):
    for f in files:
        if f.endswith(".mxd"):
            mxd_paths.append("{0}\\{1}".format(root, f))

## print to test
#print(mxd_paths)

for mxd_doc in mxd_paths:
    mxd_name = mxd_doc.rsplit("\\")[-1]
    arcpy.AddMessage(mxd_name)

    ## access the mxd
    mxd = m.MapDocument(mxd_doc)

    ## only access the desired dataframe
    ## you might want to use a dataframe name / wildcard to
    ## add layers to correct dataframe if you have multiple
    df = m.ListDataFrames(mxd)[0]

    ## add each layer file, one on top of the other.
    for lyr_file in add_lyrs:
        add_lyr = m.Layer(lyr_file)
        m.AddLayer(df, add_lyr ,"TOP")

    mxd.save()
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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