make Python do Pan/Zoom?

1112
1
05-20-2014 05:41 AM
MartinHvidberg
Occasional Contributor
Dear Forum

I need to make a small tool that will automatically Pan and Zoom around in ArcMap.
The overall purpose is to time how long time it takes for the screen to redraw after each Pan/Zoom.
First I made a script that jumped from Bookmark to Bookmark, but it didn???t wait for the re-draw to complete, before jumping to the next bookmark ??? I guess this would often be preferable, but in my case it was undesirable behavior.
I have now tried the following code, which opens a feature class (arcpy.GetParameterAsText(0)) that holds 5 polygons. It???s intended to jump to the extent of each of the polygons, in turn. But it seems to jump to some enormous extent many times larger than my entire data extent.
Can any one suggest a good way to make ArcMap jump from place to place, like if you were jumping from bookmark to bookmark, but using Python, and allowing the re-draw to be timed by Python?

import arcpy

strPolygons = arcpy.GetParameterAsText(0)

# Init
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
xtnInit = df.extent # remember the original extent

# Build list of OIDs
lstOIDs = list()
with arcpy.da.SearchCursor(strPolygons, ['OID@', 'SHAPE@AREA']) as cursor:
    for row in cursor:
        lstOIDs.append(row[0])

# Run through OIDs
arcpy.MakeFeatureLayer_management (strPolygons, "Boxes")
for ID in lstOIDs:
    strSelector = '"OBJECTID" = '+str(ID)
    arcpy.SelectLayerByAttribute_management("Boxes", "NEW_SELECTION", strSelector)
    df.zoomToSelectedFeatures()
    arcpy.RefreshActiveView()

# closing up ...
df.extent = xtnInit
arcpy.RefreshActiveView()


Best regards
Martin
Tags (2)
0 Kudos
1 Reply
XanderBakker
Esri Esteemed Contributor
Hi Martin,

You can directly read the extent of a polygon and assign it to the dataframe extent:

import arcpy
from datetime import datetime, timedelta

strPolygons = arcpy.GetParameterAsText(0)

# Init
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
xtnInit = df.extent # remember the original extent

# Build list of SHAPEs
lst_shapes = [row[0] for row in arcpy.da.SearchCursor(strPolygons, ['SHAPE@'])]

# Loop through polygons
lst_drawtimes = []
for shape in lst_shapes:
    tm_before = datetime.now()
    df.extent = shape.extent
    arcpy.RefreshActiveView()
    tm_after = datetime.now()
    duration = tm_after - tm_before
    lst_drawtimes.append(duration)

# closing up ...
df.extent = xtnInit
arcpy.RefreshActiveView()

# list stats
print "Number of draws      : {0}".format(len(lst_drawtimes))
print "Total time of draws  : {0}".format(sum(lst_drawtimes, timedelta()))

if len(lst_drawtimes)> 0:
    print "Average time of draws: {0}".format(sum(lst_drawtimes, timedelta())/len(lst_drawtimes))


Kind regards,

Xander