
Zooming in to selected features
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 inpidual layer.
Getting ready
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 inpidual 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.
How to do it…
Follow these steps to learn how to get and set the active data frame and active view ArcMap:
- Open
c:\ArcpyBook\Ch2\Crime_Ch2.mxd
with ArcMap. - In the ArcMap Table Of Contents pane, make sure that
Crime
is the active data frame. - In the Table Of Contents pane, click on the List By Selection button.
- Make the Bexar County Boundaries layer unselectable by clicking on the toggle button just to the right of the layer name:
- Click on the List By Source button in the Table Of Contents pane. Using the Select Features tool, drag a box around the cluster of burglaries inside the Northside ISD boundary. This should select the boundaries of a specific school district along with some burglaries as shown in the following diagram:
- Click on the Python window button from the main ArcMap toolbar.
- Import the
arcpy.mapping
module:import arcpy.mapping as mapping
- Reference the currently active document (
Crime_Ch2.mxd
) and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
- Get the active data frame (
Crime
) and zoom to the selected features:mxd.activeDataFrame.zoomToSelectedFeatures()
- If no records have been selected, a call to
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 thezoomToSelectedFeatures()
method:mxd.activeDataFrame.zoomToSelectedFeatures()
- Now, we'll zoom to the extent of the selected features in a specific layer. Using the Select Features tool, drag a box around the cluster of burglaries inside the Northside ISD boundary.
- First, get a reference to the
Crime
data frame. Calling theListDataFrames()
function and passing in a wildcard ofCrime
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]
- Now, we'll get a reference to the
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. TheListLayers()
function returns a Python list and we use[0]
to pull out the first and only layer containing the wordBurglaries
:layer = mapping.ListLayers(mxd,"Burglaries*",df)[0]
- Finally, we'll set the extent of the data frame by getting the extent of the selected features in the layer:
df.extent = layer.getSelectedExtent
- The complete script for zooming to the selected features of a layer should appear as follows or you can consult the solution file at
c:\ArcpyBook\code\Ch2\ZoomSelectedExtent.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
How it works…
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
.