Analyze Map Extent for Visible Layers

554
1
05-08-2014 11:06 AM
MikeMacRae
Occasional Contributor III
is there a pythonic way of determing if a layer is visible within the dataframe extent in arcpy 10.1? Not unlike how the option in a legends property dialogue box--> Items tab--> Map Extent Options--> 'Only show classes that are visible in the current map extent' analyzes the map and toggles on and off legend items. I would like to analyze the current map extent and turn on and off layers.
Tags (2)
0 Kudos
1 Reply
MattEiben
Occasional Contributor
For something like this, you can use the arcpy.Extent object.  Specifically, the "contains" and "overlaps" methods it has.  These methods receive geometries though, no other extents.

The approach I would take, is to create a rectangular geometry that mimics your feature class's extent.  Whenever you want to test if that feature class is visible, check and see it it's extent geometry overlaps the dataframe extent.

Example below.

# Get your Feature Layer extent
layer = arcpy.mapping.Layer("Your Feature Layer")
extent = arcpy.Describe(layer).extent

# Create a Polygon object from that extent
extentArray = arcpy.Array(i for i in (extent.lowerLeft,
                                      extent.lowerRight,
                                      extent.upperRight,
                                      extent.upperLeft,
                                      extent.lowerLeft))

extentPolygon = arcpy.Polygon(extentArray, arcpy.SpatialReference(4326))
del extentArray

# Get reference to your dataframe
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# Whenever appropriate, test if the layerframe overlaps the extent geometry of your feature layer
if df.extent.overlaps(extentPolygon) == False:
    layer.visible = False
    arcpy.RefreshActiveView()
else:
    layer.visible = True
    arcpy.RefreshActiveView()


If you have several layers you want to test with this, you can use
arcpy.mapping.ListLayers(mxd)
to get references to all the layers in your mxd and loop through them with this test.

Hope this helps!

Matt
0 Kudos