Select to view content in your preferred language

Use Python to zoom into Active Data Frame

748
3
05-03-2018 10:49 AM
RichardDaniels
Frequent Contributor

I'm trying to automate some tasks for unit test. Once of these tasks is zooming into a map a fixed amount and forcing ArcMap to redraw at the new extent. The Data Frame is named Raster Data Sets.

The display is refreshing but it does not appear to honor the panToExtent() method. The AddMessage command shows that the value for the extent class was changed as expected.

ideas?

import arcpy

zoomScale=arcpy.GetParameterAsText(0)


mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Raster Data Sets")[0]
lyr = arcpy.mapping.ListLayers(mxd, 'County Boundaries*', df)[0]
ext = lyr.getExtent()
arcpy.AddMessage("Old xmin, ymin, xmax, ymax: " + str(ext.XMin) + ", " + str(ext.YMin) + ", " + str(ext.XMax) + ", " + str(ext.YMax))

# change extent
temp = ext.XMin + float(zoomScale)
ext.XMin = temp
temp = ext.YMin + float(zoomScale)
ext.YMin =  temp
temp = ext.XMax - float(zoomScale)
ext.XMax = temp
temp = ext.YMax - float(zoomScale)
ext.YMax = temp
arcpy.AddMessage("New xmin, ymin, xmax, ymax: " + str(ext.XMin) + ", " + str(ext.YMin) + ", " + str(ext.XMax) + ", " + str(ext.YMax))

df.panToExtent(ext)
arcpy.RefreshActiveView()

sys.argv[2]=True
arcpy.SetParameter(1,"True")

Tags (1)
0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

So it doesn't recenter?

Pans and centers the MapFrame using a new Extent object without changing the map frame's scale.
0 Kudos
RichardDaniels
Frequent Contributor

It redraws but it does not appear to be resulting in the desired behavior (zoom into the new map extent).

0 Kudos
DanPatterson_Retired
MVP Emeritus

you might have a weird combo of extents... give

zoomToAllLayers ({selection_only}, {symbolized_extent})

from http://pro.arcgis.com/en/pro-app/arcpy/mapping/mapframe-class.htm

there is an equivalent for arcmap's arcpy mapping module