Using ArcPy to zoom to selected feature?
I've got a piece of code that works. I found it here on the ESRI website. Add it as a script to a model, then connect the output of a select by attribute tool to it. It does exactly what I want.
import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()
As you've already surmised,
df.zoomToSelectedFeatures()
will change the extents of the data frame to all selected features in the map frame. If you're interested in just zooming to a selection set for a specific layer then use lyr.getSelectedExtent()
. I also adjust the map scale factor so my code either looks like this:
df.extent = lyr.getSelectedExtent()
df.scale *= 1.5
arcpy.RefreshActiveView()
or this:
df.extent = lyr.getSelectedExtent()
df.scale = 12000 # 1:1,000
arcpy.RefreshActiveView()
I'd say yes. The layer class has a getSelectedExtent method, and the Dataframe has an extent property. Haven't tried it though.