2

For many of my projects I use a single mxd to create multiple maps using a python script to switch on/off various raster layers before exporting to pdf. Before beginning the exporting process I currently have to open the legend properties window, go to the items tab, individually double click on each raster layer (often 50+ raster grids) and untick the "Show Layer Name" checkbox for each in order to display it correctly in the legend. As this is a highly manual process I almost always accidentally miss doing this for at least one raster in the list and have to spend time working out which layers to switch on/off before manually exporting.

Is there a way to automate removing the layer name line of each raster in the legend using python? i.e. leaving just the "Heading" and "Labels" boxes ticked.

Screenshot showing the location of the layer name I'm looking to remove and the tickbox to do so below. See red/yellow arrows.

http://gyazo.com/abe72eb654f08845e6f2cfe087ebc613

  • Your legend is dynamic, not a graphic? – artwork21 Dec 17 '14 at 13:05
2

Have you looked at the arcpy.mapping LegendElement? Not sure that will do what you want, but is one way of manipulating legend elements.

  • Thanks recurvata. I did see that but from what I could see it was just to alter the position/size of the legend or add/remove whole items. I'm hoping to alter the format of a particular item within the legend. I've added a hyperlink to a screenshot that should help explain to the original question. – davehughes87 Dec 17 '14 at 13:17
  • Well, you may be able to find a style that fits your needs, one that doesn't display layer names, then set your layers to that. The update_legend_item_style_item in the updateItem method in my link above has a link to ListStyleItem resources.arcgis.com/en/help/main/10.2/index.html#//…. It looks like you may be able to use that to set the style programmatically, once you know the location of the style you want. Never tried it myself. Edit - looks like @Chris R beat me to it. – recurvata Dec 17 '14 at 13:33
  • Cheers - will give it a go. – davehughes87 Dec 17 '14 at 13:34
  • Looks like that was it - there's a default style I hadn't seen in there that works just fine. Thanks! – davehughes87 Dec 17 '14 at 13:37
  • KHAAAAANNN!!!;) – landocalrissian Dec 17 '14 at 14:09
2

I can think of a few ways to accomplish this, if you want to automate the process you would use a similar approach to this example in the ArcGIS help

LegendElement example 4: The following script updates all layers in the legend to use a custom legend item style item called MyNewStyle.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
styleItem = arcpy.mapping.ListStyleItems("USER_STYLE", 
                                         "Legend Items", 
                                         "MyNewStyle")[0]
for lyr in legend.listLegendItemLayers():
     legend.updateItem(lyr, styleItem)
del mxd

Not sure if you would need to author a custom style or use an existing StyleItem also if you only want to change certain layers to that style instead of all layers you would need to use an index described in the help link.

Another approach which isn't really automated but may be easier than writing a script:

In the legend properties instead of selecting each raster one by one you can select all or hold Ctrl and select all the items you need to change then press the "Style" button at the bottom. enter image description here

Once in the Style window you can select the style to just show headings and labels(or any style you find appropriate).

enter image description here

  • Thanks Chris. I'll give the first idea a go this afternoon and let you know how I get on. Unfortunately, selecting all the layers at once and clicking the "Style..." button brings up the window you show but the properties button to remove the layer name is disabled. – davehughes87 Dec 17 '14 at 13:34
  • @davehughes87 it is greyed out then, but you don't necessarily need it. Just pick a predefined style in the list (e.g. the highlighted in the image above) or create your own. That you can do. – Martin Dec 17 '14 at 14:37
0

If the legend is dynamic, you may use the Layer (arcpy.mapping) class, using a combination of the isRasterLayer property and VISIBLE parameter to turn on or off raster layers, which should hide or show that item in the legend.

  • Thanks artwork21 - though I'm not trying to completely hide the item from the legend, just the title of the raster layer. I've added a screenshot explaining what I mean to the original question. – davehughes87 Dec 17 '14 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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