I was able to come up with an ArcObjects solution to do this in Python. First thing you'll need is the Snippets module, of which I have a copy on GitHub. Then you can call this function:
def importSymbologyFromLayer(pApp, target_layer, target_field, symbol_layer):
"""imports the symbology from a layer in TOC
pApp -- reference to current app
target_layer -- layer to symbolize
target_field -- name of target field to symbolize
symbol_layer -- symbology layer
"""
import comtypes.gen.esriDisplay as esriDisplay
import comtypes.gen.esriArcMapUI as esriArcMapUI
import comtypes.gen.esriCarto as esriCarto
pDoc = pApp.Document
pMxDoc = CType(pDoc, esriArcMapUI.IMxDocument)
pMap = pMxDoc.FocusMap
# get layer references
targ_lyr = [pMap.Layer(i) for i in range(pMap.LayerCount) if pMap.Layer(i).Name == target_layer][0]
symb_lyr = [pMap.Layer(i) for i in range(pMap.LayerCount) if pMap.Layer(i).Name == symbol_layer][0]
# cast to IGeoFeatureLayer interface and get renderers
targGeo = CType(targ_lyr, esriCarto.IGeoFeatureLayer)
symbGeo = CType(symb_lyr, esriCarto.IGeoFeatureLayer)
symbRenderer = symbGeo.Renderer
# cast to IClassBreaksRenderer
targClassBreaks = NewObj(esriCarto.ClassBreaksRenderer, esriCarto.IClassBreaksRenderer)
symbClassBreaks = CType(symbRenderer, esriCarto.IClassBreaksRenderer)
# set target symbology field
targClassBreaks.Field = target_field
targClassBreaks.BreakCount = symbClassBreaks.BreakCount
# iterate through class beraks from symbol layer and apply to target
for i in range(symbClassBreaks.BreakCount):
print i, symbClassBreaks.Break(i)
targClassBreaks.Break[i] = symbClassBreaks.Break(i)
targClassBreaks.Description[i] = symbClassBreaks.Description[i]
targClassBreaks.Symbol[i] = symbClassBreaks.Symbol[i]
targClassBreaks.Label[i] = symbClassBreaks.Label[i]
targGeo.Renderer = targClassBreaks
pMxDoc.ActiveView.Refresh()
pMxDoc.UpdateContents()
I can push this function to the GitHub repo later, but you can just stick this in it for now. I tested in the ArcMap Python window, passing in arcobjects.GetCurrentApp()
as the pApp parameter and it worked.
ApplySymbologyFromLayer
is different class breaks, you can use arcpy to to adjust these after you apply the symbology.ApplySymbologyFromLayer
with arcpy and thenlyr.symbology.classBreakValues
to adjust the values after the symbology was applied. I'll plan to take a look at your ArcObjects solution this weekend, thanks a bunch!