Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

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

The answer to this may be a simple yes or no, but I'm hoping that someone is aware of a way to zoom to the extent of a selected feature using the ArcPy modules in ArcGIS 10.

share|improve this question
up vote 6 down vote accepted

I'd say yes. The layer class has a getSelectedExtent method, and the Dataframe has an extent property. Haven't tried it though.

share|improve this answer
+1 and answer. Thanks for the pointer in the right direction. – Zachary Sep 3 '10 at 19:58

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]
share|improve this answer
Most scripting functionality that handles document (mxd) management, display, or output is going to use the ArcPy mapping module.… – mindless.panda Mar 29 '11 at 1:16
+1 df.zoomToSelectedFeatures() Is how I do it. You may wish to set scale with df.scale = yourscalehere as well. – Nathanus Mar 29 '11 at 3:24

As you've already surmised,


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

or this:

df.extent = lyr.getSelectedExtent()
df.scale = 12000 # 1:1,000
share|improve this answer
Hi Erik, welcome to :) Nice answer. Inline code formatting can be done with backticks - `code` - or just select phrase in question and slap [ctrl-k] (same keys for blocks too). – matt wilkie May 26 '11 at 23:05

So, to add to this, I've been needing to keep track of the broadest extent across multiple layers with no features selected. The following code will track the furthest extent in each direction. extent_object stays constant across all calls to the function and should be initialized to one of the layers you are including. The argument "layer" to track_extent is an arcpy.Mapping.Layer object. When you are ready to save your map, just set your dataframe's extent via something like data_frame.extent = extent_object

extent_object = initial_layer.getExtent()

def track_extent(extent_object,layer):

    l_properties = layer.getExtent()

    # each of these essentially says that if this layer is further out to one direction than the current setting, change the setting
    if l_properties.XMin < extent_object.XMin:
        extent_object.XMin = l_properties.XMin
    if l_properties.YMin < extent_object.YMin:
        extent_object.YMin = l_properties.YMin
    if l_properties.XMax > extent_object.XMax:
        extent_object.XMax = l_properties.XMax
    if l_properties.YMax > extent_object.YMax:
        extent_object.YMax = l_properties.YMax
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.