Creating selection sets in ArcMap is a common task. Selection sets are often created as the result of an attribute or spatial query, but they can also occur when a user manually selects features and sometimes, under some additional circumstances. To better visualize selection sets, users often zoom to the extent of the selected feature. This can be accomplished programmatically with Python in several ways. In this recipe, you will learn how to zoom to all the selected features in a data frame as well as an individual layer.
The DataFrame.zoomToSelectedFeatures
property zooms to the extent of all the selected features from all the layers in the data frame. Essentially, it performs the same operation as the Selection | Zoom to Selected Features operation. One difference is that it will zoom to the full extent of all the layers if no features are selected.
Zooming to the extent of selected features in an individual layer requires you to use the Layer
object. The Layer
object includes a getSelectedExtent()
method that you can call to zoom to the extent of the selected records. This returns an Extent
object, which you can then use as a parameter that is passed into the DataFrame.panToExtent()
method.
Follow these steps to learn how to get and set the active data frame and active view ArcMap:
c:ArcpyBookCh2Crime_Ch2.mxd
with ArcMap.Crime
is the active data frame.arcpy.mapping
module:import arcpy.mapping as mapping
Crime_Ch2.mxd
) and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
Crime
) and zoom to the selected features:mxd.activeDataFrame.zoomToSelectedFeatures()
zoomToSelectedFeatures()
will zoom to the extent of all the records in the data frame. Clear the selected features by navigating to Selection | Clear Selected Features. This will clear the selection set. Now, execute the same line of code again to see how this affects the operation of the zoomToSelectedFeatures()
method:mxd.activeDataFrame.zoomToSelectedFeatures()
Crime
data frame. Calling the ListDataFrames()
function and passing in a wildcard of Crime
will return a Python list containing a single item. We pull this item out using [0]
, which returns the first and only item in the list:df = mapping.ListDataFrames(mxd, "Crime")[0]
Burglaries
layer, which contains some selected features. The following code uses a wildcard *
to search for the Burglaries in 2009 layer within the data frame that we referenced in the last line of code. The ListLayers()
function returns a Python list and we use [0]
to pull out the first and only layer containing the word Burglaries
:layer = mapping.ListLayers(mxd,"Burglaries*",df)[0]
df.extent = layer.getSelectedExtent
c:ArcpyBookcodeCh2oomSelectedExtent.py
:import arcpy.mapping as mapping mxd = mapping.MapDocument("CURRENT") df = mapping.ListDataFrames(mxd, "Crime")[0] layer = mapping.ListLayers(mxd,"Burglaries*",df)[0] df.extent = layer.getSelectedExtent
In this recipe, you learned how to zoom to the extent of all the selected records from all the layers in a data frame as well as how to zoom to the extent of all the selected records from a specific layer in a data frame. Zooming to the extent of all the selected records from all the layers in a data frame simply requires that you get a reference to the active data frame and then call zoomToSelectedFeatures()
.
Zooming to the extent of the selected records within a specific layer requires a little more coding. After importing the arcpy.mapping
module and getting a reference to the map document, we then got a reference to the Crime
data frame. Using the ListLayers()
function we passed in a reference to the data frame as well as a wildcard that searched for the layers that begin with the text Burglaries
. The ListLayers()
function returned a Python list and since we knew that we only had one layer that matched the wildcard search, we pulled out the first layer and assigned it to a variable called layer
. Finally, we set the extent of the data frame using layer.getSelectedExtent
.