Dear All!
I'm trying to add a legend to layer in modelbuilder. Help pages say that I need to use "Apply Symbology From Layer" tool. I have saved my Symbology Layer but it doesn't consist my legend (as I understood a legend can be saved only in Map file). Could you please explain me how to do this correctly?
If I understood correctly, you want to add legend with model builder.Apply Symbology From Layer extracts Symbology from a layer ( which is a layer you prepared before) to another layer which does not have symbology.
As far as I know , there is no tool to include legend in model builder but you could create a script "to add legend" in python and make a tool with that script. Then you can add it to your model. Here same code you can use to create layer from shp and add sybology.
import arcpy
symbologyLayer=r"C:\.....\sembology.lyr" # if you want to assign same layer format all the time this can be a path to your symbology
LabelExpression="[Fieldyouwantolabelwith]" #if you want label you can make it parameter if not you do not have to use this label parts
K_shp= arcpy.GetParameterAsText(0) ### your shp file you want to assign symbology
## those above will be your parameters
mxd = arcpy.mapping.MapDocument(r"C:\......\my.mxd")
#or mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.MakeFeatureLayer_management(K_shp,"Layername")
lyr1= arcpy.mapping.Layer("Layername")
arcpy.ApplySymbologyFromLayer_management (lyr1, symbologyLayer)
lyr1.labelClasses[0].expression = LabelExpression
lyr1.showLabels = True
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
legend.autoAdd = True
arcpy.RefreshActiveView()
arcpy.mapping.AddLayer(df, lyr1, "BOTTOM")
Ok I created tool and here how you do it.
This is the tool view below
Here the script for it
import arcpy,os
symbologyLayer=arcpy.GetParameterAsText(2)
LabelExpression=arcpy.GetParameterAsText(1)
try:
for idx, item in enumerate(LabelExpression.split()):
if 'VISIBLE'==item:
num=idx
arcpy.AddWarning(LabelExpression.split()[idx-1])
LabelExpression="["+LabelExpression.split()[num-1]+"]"
except:
arcpy.AddError("Choose only one field name for label expession")
K_shp=arcpy.GetParameterAsText(0)
add_legend= arcpy.GetParameterAsText(3)
LayerName=os.path.split(K_shp)[1][:-4]+"_lyr"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.MakeFeatureLayer_management(K_shp,LayerName)
lyr1= arcpy.mapping.Layer(LayerName)
arcpy.ApplySymbologyFromLayer_management (lyr1, symbologyLayer)
lyr1.labelClasses[0].expression = LabelExpression
lyr1.showLabels = True
arcpy.RefreshActiveView()
arcpy.mapping.AddLayer(df, lyr1, "BOTTOM")
if add_legend=='true':
#styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", "Legend Items", "NewDefaultLegendStyle")[0]
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]
#legend.updateItem(lyr1, styleItem)
legend.autoAdd = True
del mxd
Above is the tool parameters you need to arrange..
Only thing that I could not figure out how to add legend itself with arcpy, so you need to manully create and empty legend before you run the script.
Hi,
if this is just for symbology... right click in the model element (final output) -> properties -> layer symbology tab
Actually, I think the question is about adding legend by using model builder to automate the process instead of doing it manually.. not sure how to add legend in model builder but you can create legend items after adding symbology in arcpy.. If you have any idea about how to add legend with model or script, I can also utilize from that..