ArcMap 10.2: Save fixed layer extent and set it for multiple ArcMap Documents

6343
4
12-23-2014 08:48 PM
fd
by
New Contributor II

I often have to copy the same layer extent for different projects in ArcMap. I am using fixed extents and multiple ArcMap documents to create different sets of maps. However with this method I have to copy each position ("Top", "Left", "Right", "Bottom") separetely and then paste it into the other projects (as illustration I have added the specific box in the Data Frame Properties which I am refering to).

Is there a way to copy the whole extent and therefore a faster approach to my workflow? Any help is kindly appreciated.

Kind regards,

4 Replies
MicahBabinski
Occasional Contributor III

Hey there, great question! And an excellent candidate for using the arcpy.mapping module.

The first step would be to create a list of the mxds you want to modify. Something like

import arcpy

arcpy.env.workspace = r"[path to directory containing your MXDs]"
mapDocList = arcpy.ListFiles("*.mxd")

Now that we have that list, you can identify the extent you want to apply to the other MXDs. Try:

mxd_to_copy = arcpy.mapping.MapDocument(r"[path to MXD containing the extent you want to copy]")
df_to_copy = arcpy.ListDataFrames(mxd)[0] # Note: this assumes that there is only one data frame in the mxd. If there are multiple, you can specifiy which to use with a wildcard
extent_to_copy = df_to_copy.extent

Ok, now you have created an extent object that you will apply to the data frames in your other MXDs. So...

for mapDoc in mapDocList:
     # Create an mxd object out of the map doc
     mxd = arcpy.mapping.MapDocument(r"[path to directory containing your MXDs]" + "\\" + mapDoc)
     # Create a data frame object to apply the extent to
     df = arcpy.mapping.ListDataFrames(mxd)[0] # See previous note about multiple data frames
     # Apply the extent
     df.extent = extent_to_copy
     # Save the map document
     mxd.save()
     # Cleanup the variables
     del mxd, df

    

Anyways, the arcpy.mapping module is awesome once you get the hang of it. Esri really hit it out of the park with that one in my humble opinion.

I hope this works!

Warm Regards,

Micah

XanderBakker
Esri Esteemed Contributor

Hate to bring the bad news, but this will not work. If the data has a setting "Fixed Extent", you will not be able to change the extent through a Python script. It would first require to set the Extent to "Automatic", but this property is not exposed to the dataframe object of arcpy...

RuntimeError: DataFrameObject: Error in setting extent

This wold require some ArcObjects coding or setting the extent to Automatic in all your mxd's ...

0 Kudos
MicahBabinski
Occasional Contributor III

You're right Xander, but I was not clear from the question if the target data frames have a fixed extent or just the source data frame.

Assuming the target data frames are set to fixed extent, that sounds like a fun bit of ArcObjects research!

0 Kudos
PraveenBains
New Contributor

Thank you for the suggested workflow ! I found it very helpful.

I wanted to point out a typo in your code, in line 2 in the excerpt below (and written in red). I believe you meant to write "mxd_to_copy" rather than "mxd". 

Just wanted to clear it up in case anyone copies and pastes your code directly. 

Cheers,

Praveen

  1. mxd_to_copy = arcpy.mapping.MapDocument(r"[path to MXD containing the extent you want to copy]")  
  2. df_to_copy = arcpy.ListDataFrames(mxd)[0# Note: this assumes that there is only one data frame in the mxd. If there are multiple, you can specifiy which to use with a wildcard  
  3. extent_to_copy = df_to_copy.extent  

0 Kudos