Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I create various layers programmatically using ArcPy.

I can adjust how the layers are sequenced using the arcpy.mapping.MoveLayer command.

However, I want to have the legend reflect a different order. I have yet to figure out how to do this in Python.

I have tried using legend.autoAdd=True or False. I think I am close in using:

legend=arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT","My Legend") [0]

and then legend.items.something.

I have four legend items A, B, C, and D which appear incorrectly as D, C, A, B.

Can anyone provde a link to code examples or better documentation?


This question was originally posed for ArcGIS 10.0 but another question (Moving legend items up and down with arcpy) using ArcGIS 10.2 has been merged into it so answers using any version from and including 10.0 should be acceptable.

share|improve this question

3 Answers 3

As near as I can tell (from working with the mapping class a couple of days ago), legend items themselves are created only using auto-add. I have found no methods for manipulating layer names and positions in the legend directly. The mapping class is not all-powerful in my experience, and is generally only as useful as the .mxd it is combined with. Most fine-tuning seems to be best left to ArcMap's UI.

That being said, you can order your elements by adding them to your map in the same order they are to appear in your legend.

(This is taken mostly from arcpy's documentation):

df = arcpy.mapping.ListDataFrames(mxd, "New Data Frame")[0]  
lyrA = arcpy.mapping.Layer(r"C:\Path\LayerA.lyr")  
lyrB = arcpy.mapping.Layer(r"C:\Path\LayerB.lyr")  
lyrC = arcpy.mapping.Layer(r"C:\Path\LayerC.lyr")  
lyrD = arcpy.mapping.Layer(r"C:\Path\LayerD.lyr")  
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0]  
legend.autoAdd = True  
arcpy.mapping.AddLayer(df, lyrA, "BOTTOM")  
arcpy.mapping.AddLayer(df, lyrB, "BOTTOM")  
arcpy.mapping.AddLayer(df, lyrC, "BOTTOM")  
arcpy.mapping.AddLayer(df, lyrD, "BOTTOM")  
mxd.save()  
del mxd
share|improve this answer
    
I didn't have time to test an example, but I was wondering if you can change the index value for a layer to change the order. I am guessing not, but it might be possible. –  DavidF Mar 8 '11 at 14:34
    
AFAIK, you cannot index the elements inside the legend itself. However, changing the layer orders within the ToC might do it. –  Nathanus Mar 8 '11 at 17:16

With the AddLayer method you cannot be more precise than "TOP" or "BOTTOM" as shown above.

However, if you use the InsertLayer method (ESRI Help Pages), you can specify a reference layer (or multiple reference layers) and add your new layer in reference to that.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
refLayer = arcpy.mapping.ListLayers(mxd, "Lakes", df)[0]
insertLayer = arcpy.mapping.Layer(r"C:\Project\Data\Rivers.lyr")
arcpy.mapping.InsertLayer(df, refLayer, insertLayer, "BEFORE")
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, insertLayer
share|improve this answer

The legend will always reflect the draw order of the map, AFAIK. You can change the draw order using arcpy.mapping.MoveLayer. There is a code example from the ESRI help

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0] for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name.lower() == "rivers": moveLayer = lyr if lyr.name.lower() == "lakes": refLayer = lyr arcpy.mapping.MoveLayer(df, refLayer, moveLayer, "BEFORE") mxd.saveACopy(r"C:\Project\Project2.mxd") del mxd

share|improve this answer
    
The ordering and visibility of individual layers can be overridden in the legend properties, so this is not always true. –  blah238 Dec 3 at 23:19
    
@blah238 Are you thinking of something like gis.stackexchange.com/a/124407 or a more palatable workaround/procedure? –  PolyGeo Dec 4 at 7:16
    
@blah238 Interesting, I didn't know this. Can you point me in the direction of how to do this so I can update my answer? –  Dowlers Dec 4 at 17:56
    
I meant that the user can override those properties through the user interface, so they might have set up a legend that you can't reorder by reordering TOC layers. I don't think there is a way to work with those properties with arcpy, but it's probably possible with ArcObjects. –  blah238 Dec 4 at 21:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.