Quick & Dirty arcpy: Autopan ArcMap using arcpy

Question: How do I get ArcMap to automatically pan through an area.

As I mentioned in a previous post, I recently had the need to have ArcMap automatically pan through a project area. My first attempt was to print a series of data-driven pages (using a fishnet polygon layer as the index) this but that did not accomplish what I needed so I switched to arcpy, which made the task simple enough. Nothing special or tricky about this code, but just did not find it anywhere else.

The one thing to note is that I have a 1 second pause between pans–this was to allow image tiles to download. You will need to adjust the delay to meet your needs. The toolbox and code can also be downloaded.

import sys,arcpy,datetime
inLayer = sys.argv[1]

def printit(inMessage):
    print inMessage
    arcpy.AddMessage(inMessage)

mxd = arcpy.mapping.MapDocument("CURRENT")

arcpy.MakeFeatureLayer_management(inLayer, "indexLayer")
cur=arcpy.SearchCursor("indexLayer")

df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = df.extent

iCount = 0
iTotal = (arcpy.GetCount_management("indexLayer").getOutput(0))

for row in cur:
    thisPoly = row.getValue("Shape")
    newExtent.XMin, newExtent.YMin = thisPoly.extent.XMin, thisPoly.extent.YMin
    newExtent.XMax, newExtent.YMax = thisPoly.extent.XMax, thisPoly.extent.YMax
    df.extent = newExtent
    time.sleep(1)
    iCount+=1
    printit("Panned to feature {0} of {1}".format(iCount,iTotal))

del row
del cur

6 thoughts on “Quick & Dirty arcpy: Autopan ArcMap using arcpy”

  1. Holy crap, you’re my hero of the day. I had a request to do exactly what you did here, just adding an option to export the image between each pause.

    Thanks!

  2. This is excellent and exactly what I was trying to do last week. Thanks for the code. You’re my hero of the day!

  3. Mainly using to grab higher res images that can be mosaiced and then used by the water resources/transportation folks for their use in stand alone hydrology and traffic analysis software.

  4. terrific. I found that the Create Fishnet tool (under Data Management > Feature Class in Arcmap 10) was perfect for cutting my study area rectangle into a 550 squares (650m across) that forced the window to zoom to the right scale as it panned across. In my case Zoom 17 was achieved at :3200.

Whatcha Think?