Friday, June 10, 2011

The DataFrame Object

The DataFrame object provides access to many of the data frame properties found in a map document.  This object is essentially a gateway to use other mapping functions.  To access the data frame, use the ListDataFrames(), which returns a list.  Iterate through the list to find the correct DataFrame object.  This object allows developers to set the credits, map description, extent, see the map units, set the data frames name, position on page layout, width/height on page layout, and many other.  The complete overview can be found here

Example: Zoom to Selected Features From

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.zoomToSelectedFeatures()
del mxd

This example assumes you have layers already selected in the current map document.

Example: Changing Data Frame's Size and Position

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\temp\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.elementPositionX, df.elementPositionY = 1, 1
df.elementHeight, df.elementWidth = 5, 6.5
mxd.save()
del mxd


Example: Spatial Query by Visible Extent

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = arcpy.mapping.ListLayers(mxd, "", df)
# Extent Polygon
extentPolygon = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft,df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),
df.spatialReference)
# Select all features in current extent
for lyr in layers:
   arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", extentPolygon, "", "NEW_SELECTION")
del mxd


Enjoy