How do I set an MXD's full extent programmatically using arcpy?

7501
10
Jump to solution
07-09-2014 06:26 AM
NeilErickson
New Contributor II

I am using arcpy for 10.2 and am able to set the initial extent of the map using the dataframe's extent property. This is the initial extent that the map comes up at when the MXD is loaded.

How do I set the full extent programmatically using arcpy?

This article describes how to do it in ArcGIS Desktop 10.2: ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Also, the full extent shows up right next to the initial extent in ArcGIS Server when an mxd is published as a service. It feels like the setting is so close yet I have spent far too much time trying to decipher ESRI help articles to figure this out.

To be clear again:

Initial Extent: initial zoomed area when an mxd is opened up and a map documents dataframes extent value:

Example:

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

newExtent = df.extent

Full Extent: area that is zoomed to by clicking the Full Extent(globe icon) button in ArcGIS Desktop 10.

Thanks for any help.

Reply
0 Kudos
1 Solution

Accepted Solutions
ChristianWells
Esri Contributor

If I am not mistaken, Neil Erickson‌is looking for a way to set a custom full extent in his data frame (for use in the Full Extent button). This is a property held by the data frame that doesn't change as you pan and zoom. This setting cannot be modified in Python. It is only able to be set through the GUI or ArcObjects.

IActiveView.FullExtent Property:

ArcObjects 10 .NET SDK Help

View solution in original post

10 Replies
DuncanHornby
MVP Frequent Contributor

The following python code will achieve what you want. It cycles through all the layers getting their extents and placing them in a temporary featureclass which you can then get the extent of and zoom to.

import arcpy

# Stop temporary layer being loaded into map

arcpy.env.addOutputsToMap=False

# Create a list of layers in map document

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

list =arcpy.mapping.ListLayers(mapdoc)

# A list of extents

extentList = []

# Cycle through layers grabbing extents, converting them into

# polygons and adding them to extentList

for layer in list:

    if not layer.isGroupLayer:

        desc = arcpy.Describe(layer.dataSource)

        ext = desc.extent

        array = arcpy.Array([ext.upperLeft,ext.upperRight,ext.lowerRight,ext.lowerLeft])

        extentList.append(arcpy.Polygon(array))

# Create a temporary FeatureClass from the polygons

arcpy.CopyFeatures_management(extentList,r"in_memory\temp")

# Get extent of this temporary layer and zoom to its extent

desc = arcpy.Describe(r"in_memory\temp")

ext = desc.extent

mapdoc.activeDataFrame.extent = ext

# Clean up

arcpy.Delete_management(r"in_memory\temp")

del ext, desc, list, array, extentList, mapdoc

Reply
0 Kudos
NeilErickson
New Contributor II

Hi Duncan,

Thanks for the reply. This script looks like it only sets the initial extent and not the full extent. Great code though.

To be clear, the 'intial extent' of a map document is the dataframes extent property which I am aware of how to use. I need the elusive 'full extent'.

Reply
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

The full extent is the extent of all the layers.  If Duncan's code determines the minima and maxima of the associated layers, then that is the full extent.  There is no otherway to obtain said information within arcpy or arcpy.mapping other than to cycle through the data and determine it iteratively

ChristianWells
Esri Contributor

If I am not mistaken, Neil Erickson‌is looking for a way to set a custom full extent in his data frame (for use in the Full Extent button). This is a property held by the data frame that doesn't change as you pan and zoom. This setting cannot be modified in Python. It is only able to be set through the GUI or ArcObjects.

IActiveView.FullExtent Property:

ArcObjects 10 .NET SDK Help

View solution in original post

NeilErickson
New Contributor II

Thanks for the information, even if it's a bummer for me I guess i'll have to work around it. Thanks. Could you put this in as an idea for future versions of arcpy?

Thanks

Reply
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Here is the site where you can do that Esri Arcgis Ideas | Ideas Submission Portal

NeilErickson
New Contributor II

Thanks

Reply
0 Kudos
FrédéricPRALLY
Esri Contributor

Hi,

Based on this threat thread you can now use the tool Modify full extent mxd​ using ArcGIS for Desktop 10.3.

Enjoy,

Fred

DanPatterson_Retired
MVP Esteemed Contributor

Frédéric PRALLY I am assuming you meant thread and not threat  We wouldn't want anyone getting ideas on how to initiate  or expedite enhancement requests