20

Is there a way to zoom to the extent of a selected feature using the ArcPy module in ArcGIS Desktop 10.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 3, 2010 at 18:42

4 Answers 4

18

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()
answered Sep 3, 2010 at 19:52
2
  • Most scripting functionality that handles document (mxd) management, display, or output is going to use the ArcPy mapping module. help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/… Commented Mar 29, 2011 at 1:16
  • 3
    +1 df.zoomToSelectedFeatures() Is how I do it. You may wish to set scale with df.scale = yourscalehere as well. Commented Mar 29, 2011 at 3:24
12

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()
matt wilkie
28.3k35 gold badges150 silver badges286 bronze badges
answered May 26, 2011 at 21:44
0
7

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

answered Sep 3, 2010 at 18:56
0
1

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
answered Mar 29, 2011 at 0:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.