ArcPY Set Current MXD to Full Extent

3029
7
04-20-2017 11:07 AM
BehrouzHosseini
Occasional Contributor

Can you please let me know how we can set current map to Full Extent enter image description here using ArcPy?

import arcpy
import arcpy.mapping 
# Set the Map to be Full Extent 
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.Set-To-Full_Extent

like what the enter image description here is doing on main toolbar

enter image description here

in the ArcMap?

0 Kudos
7 Replies
curtvprice
MVP Esteemed Contributor

The extent is the property of a data frame, not the map.

Unfortunately it appears there is no Zoom To Full Extent method documented in the help for DataFrame, and, furthermore, the property of full extent is not exposed in a property either.  Looks like you need to go to .NET to get at this property.

DataFrame—Help | ArcGIS Desktop 

The equivalent object to get at map extents in Pro, the Camera object, also has no such method or property.

Camera—ArcPy | ArcGIS Desktop 

Maybe something to ask for in ideas?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

From the DataFrame—Help | ArcGIS Desktop documention:

zoomToSelectedFeatures ()

This performs the same ArcMap operation as Selection > Zoom To Selected Features. One difference is that if no features are selected, it will zoom to the full extent of all layers. If you want to zoom to the extent of selected features for a specific layer, try using the Layer.getSelectedExtent method.

0 Kudos
curtvprice
MVP Esteemed Contributor

Cool, nice catch Joshua Bixby.  It should be noted that the world button does this behavior by default only if you haven't modified it by setting the data frame full extent to something besides the extent of all features.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

And, the tricky part is nothing can be selected, so I wouldn't say it is exactly what the OP wants but close.  I was thinking last night that a function could be created that would loop through all of the layers in the active data frame and find the extent needed to display them all.  It wouldn't be that hard to write, I just need to find some time to sit down and tinker.

0 Kudos
curtvprice
MVP Esteemed Contributor

Absolutely, you'd do this with the arcpy geometry objects extent property. 

Note I found the DataFrame ("Map") AreaOfInterest property in ArcObjects and it is only writeable, not readable. How about that.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I got the following custom function to work for me in the interactive Python window:

def zoomToFullExtent(map_document):
    DF = map_document.activeDataFrame
    DF_extent = DF.extent
    for lyr in arcpy.mapping.ListLayers(map_document, data_frame=DF):
        extent = lyr.getExtent()
        DF_extent.XMin = min(DF_extent.XMin, extent.XMin)
        DF_extent.XMax = max(DF_extent.XMax, extent.XMax)
        DF_extent.YMin = min(DF_extent.YMin, extent.YMin)
        DF_extent.YMax = max(DF_extent.YMax, extent.YMax)
    DF.extent = DF_extent
0 Kudos
curtvprice
MVP Esteemed Contributor

I like your method better than geometry objects. Here's a little function that may be handy in other contexts too.

# neat function
def FullExtent(extent_list):
  "full extent from list of extents"
  return arcpy.Extent(min([e.XMin for e in extent_list]), 
                      min([e.YMin for e in extent_list]),
                      max([e.XMax for e in extent_list]), 
                      max([e.YMax for e in extent_list]))
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
exts = [lyr.getExtent() for lyr in arcpy.mapping.ListLayers(mxd, "*", df)]
df.extent = FullExtent(exts)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
arcpy.RefreshActiveView()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos