Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I used this example to produce printing service. As the result I get map with too many elements in the legend. Is it possible ti show classes that are visible in the current map extent using python script?

share|improve this question
yes, it can done with arcpy, see my answer – newGIS Sep 15 '14 at 10:58
up vote 5 down vote accepted

There is no need to write any additional ArcPy for this. Simply use dynamic legends (which were added at ArcGIS 10.1) when you are authoring your maps.

The Map Extent Options allow you to control how eligible legend items interact with the current map extent. If the Only show classes that are visible in the current map extent check box is checked, a legend item class will only appear in the legend if a feature from that class is in the current extent. If no features from that legend item are in the current extent, the entire item will be removed from the legend.

share|improve this answer
How do i actually write it in arcpy? – newGIS Sep 11 '14 at 8:03
@Y.Y.C That's a new question and not one that I am likely to attempt an answer on when Dynamic Legends work so well. – PolyGeo Sep 11 '14 at 9:02
PolyGeo, I have 40 maps that I should update for a project. I do not want to enter every map and action the dynamic legend option.Instead of that i would like to do this with Python-Is there a way to do this? – newGIS Sep 14 '14 at 6:06
@Y.Y.C Do you mean 40 DDP-enabled maps or 40 Data Driven Pages in a map? The latter just needs a checkbox ticked in that map. For the former could you ask a new question, please? Your precise requirements for that are not clear to me yet. – PolyGeo Sep 14 '14 at 8:01
PolyGeo I have 40 mxd's file to update. the method "use_visible_extent" will be the sulotion,i think,but i don't know how to use it within arcpy? – newGIS Sep 14 '14 at 9:27

i think this code will help:

import arcpy
import arcpy.mapping
import os
import sys
from arcpy import env
import string

env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
    for lyr in legend.listLegendItemLayers():
        legend.updateItem(lyr, use_visible_extent = True)
        print 'updateItem'
del mxd
share|improve this answer

Your Answer


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

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