map scripting

535
2
03-14-2020 11:34 AM
CNRFGN
by
New Contributor II
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

As with all homework questions, .... can you provide us with what you have read and tried so far?  

It will save people tracking down the obvious help file links and code snippets you have probably already looked at.

0 Kudos
DavidPike
MVP Frequent Contributor
#import modules

import arcpy
import os

##parameters##

#change this but keep the r'' enclosing the path
mxd_path = r'c:\desktop\mymxd.mxd'

#what dataframe do you want to copy the symbology of
symbol_dataframe_name = "Miami"
#dont change this bit
symbol_layer_name = symbol_dataframe_name + "Transit"

#make a list of the cities you want to update symbology of
#a list in python is enclosed in square brackets
#e.g. [Birmingham, Boston, Chicago, Dallas]
update_dataframe_list = []



##main##

#get the map document object from your mxd path
map_doc = arcpy.mapping.MapDocument(mxd_path)

#make a list of the dataframes in the mxd
df_list = arcpy.mapping.ListDataFrames(map_doc)

#find the dataframe which matches the city name
#eg Maimi
template_df = ''
for df in df_list:
    if df.name == symbol_dataframe_name:
        template_df = df
    else:
        print("Not Found")

#for the matching dataframe, look through its layers
#and find the layer which matches Transit
#e.g if layer_name = "MaimiTransit" 
template_df_layers = arcpy.mapping.ListLayers(template_df)
template_layer = ''
for layer in template_df_layers:
    if layer.name == symbol_layer_name:
        template_layer = layer
    else:
        print("Not Found")

#get all the layers to be updated using the template_layer
#using the dataframe list we created earlier
#excluse our template dataframe, != is syntax for not equal to
#make a list of lists containing layer and dataframe objects
layers_to_update_df_list = []
for df in df_list:
    if df.name != symbol_dataframe_name:
        for layer in arcpy.mapping.ListLayers(df):
            if layer.name in update_dataframe_list:
                layers_to_update_df_list.append([layer, df])

#use update layer to change symbology of layers in
#layers_to_update_list using template_layer
for layer in layers_to_update_df_list:
    arcpy.mapping.UpdateLayer(layer[1], layer[0], template_layer, True)

#save to a new mxd
map_doc.saveACopy((mxd_path[:-4])+"UpdatedSymbology.mxd"))

del map_doc
        
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos