[GIS] Using ArcGIS Desktop to zoom to next selected feature

arcpy

How could I adapt the following code to enable me to zoom to each individual selected Geometry one after the other?

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()

Best Answer

I have a visit tool (in C#, converted from VB.net, which was upgraded from VB6 which was influenced by a tool I wrote in AML)... anyway, the key is to use the Envelope of the geometry in the case of polygon, polyline or multipoint and recentre the map extent.

Here is a very basic start for you:

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
Envelopes = [] # store extents here

# find the selection set
SelLayer = arcpy.mapping.ListLayers(mxd,data_frame=df)[0] # first layer
fidSet   = arcpy.Describe(SelLayer).FIDSet

if len(fidSet) == 0:
    arcpy.AddMessage("Nothing selected")
else:
    # now cursor through it an get the geometries
    # storing their extents into a list
    with arcpy.da.SearchCursor(SelLayer,"SHAPE@") as SCur:
        for feat in SCur:
            # I'm going to assume polygon/polyline
            Envelopes.append(feat[0].extent) # grab the envelope 

    df.extent = Envelopes[0] # first extent
    arcpy.RefreshActiveView()

First you need to check there is a selction against the layer, if there is cursor through the records (remember that if there is a selection against a layer only the selected records are returned) using SHAPE@ to get the geometry object which has an extent as a property, store these in a list and then calling the first one to set the current dataframe extent (also a property).

Note that once the list is made the selection can be changed, this may or may not be what you want. In my case I wanted the extents to remain and be able to edit as I went through the list.