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 individual 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 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.

How to do it…

Follow these steps to learn how to get and set the active data frame and active view ArcMap:

  1. Open c:ArcpyBookCh2Crime_Ch2.mxd with ArcMap.
  2. In the ArcMap Table Of Contents pane, make sure that Crime is the active data frame.
  3. In the Table Of Contents pane, click on the List By Selection button.
  4. Make the Bexar County Boundaries layer unselectable by clicking on the toggle button just to the right of the layer name:
    How to do it…
  5. 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:
    How to do it…
  6. Click on the Python window button from the main ArcMap toolbar.
  7. Import the arcpy.mapping module:
    import arcpy.mapping as mapping
  8. Reference the currently active document (Crime_Ch2.mxd) and assign the reference to a variable:
    mxd = mapping.MapDocument("CURRENT")
  9. Get the active data frame (Crime) and zoom to the selected features:
    mxd.activeDataFrame.zoomToSelectedFeatures()
  10. 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 the zoomToSelectedFeatures() method:
    mxd.activeDataFrame.zoomToSelectedFeatures()
  11. 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.
  12. First, get a reference to the 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]
  13. 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. 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]
  14. 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
  15. 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: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

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset