Looping Through Layers in Mxd and Saving Mxd Changes Extent

4475
23
05-21-2013 02:14 PM
MichaelVolz
Esteemed Contributor
To All Python Users:

I have a script that loops through all the layers in an mxd and then saves the mxd.  I have found that when I open up the saved mxd the extent of the map in the data view has changed even though I did not write code for the extent to change.  Has anyone come across this phenomenon before?

The problem I am finding is that the layout view, that could be setup for a print job, has changed.  Now the user has to go back and fix the extent for the layout to be printed correctly.  I am eventually looking to run this script on many mxds in batch and I do not want to disrupt all the mxds where the endusers need to go back and fix the extent.

Why would the map extent change if all I am doing is looping through the layers in a python script?
Tags (2)
0 Kudos
23 Replies
T__WayneWhitley
Honored Contributor
Just curious, have you read the df extent at the beginning if the run, then re-read the df extent at the end of the run where you've completed your layer manipulations in order to check for a difference?

If nothing else you may be able to 'reset' the df extent back to the original using the extent obj read at the beginning of the run.


Enjoy,
Wayne
0 Kudos
MichaelVolz
Esteemed Contributor
Wayne:

I will try that, but it just seems like the extent should not change if the operations you are performing on the mxd have no connection to the extent.

I added the following code for checking the extent before and after looping through the layer:

df = arcpy.mapping.ListDataFrames(mxd)[0]
oldExtent = df.extent
print("The old extent = " + str(oldExtent)) - Prints the dataframe extents before looping through layers

Now I loop through layers and save mxd

df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = df.extent
print("The new extent = " + str(newExtent)) - Prints the dataframe extents before looping through layers

The extents that are printed out are identical, but the map has been zoomed out to almost double its intial extent.
0 Kudos
T__WayneWhitley
Honored Contributor
hmmm, could be a bug... I don't know, but if you're at 10.1, a possible workaround to try may be to save the extent to a bookmark, then when manipulating the layers, set the df.extent to the previously saved bookmark extent as shown in the help ref example from the link below.  I did not test this......but based on what you said it sounds like the extent is somehow being overridden by something else...

ListBookmarks (arcpy.mapping)
Desktop » Geoprocessing » ArcPy » Mapping module » Functions
http://resources.arcgis.com/en/help/main/10.1/index.html#//00s300000060000000


A slight aside - I have set text elements X and Y page coordinates, and all would go well during testing, then the wheels would come off and the X coord set would take on some wild values --- I could still read the values, but something wasn't quite right about consistently writing the values.  So I tested a clunky workaround to loop on reading/writing until the read matched the write vals I intended --- not at all the 'safe' way to write code, but it worked.  It hasn't blown up, but if I remember correctly it would have to make the set twice in some instances to get it right.  Not saying that's related to your quirk, but something is interfering with your extent and you may not be able to trust the return vals.  What about the save process alone?--if you open the mxd, and simply save it again with arcpy, is the extent affected?

Hope you figure it out...
Wayne
0 Kudos
JeffBarrette
Esri Regular Contributor
If this is truely a problem, please report this to support services.  They'll need data and repro steps.

Thanks,
Jeff
0 Kudos
LorindaGilbert
Frequent Contributor
Hi All,
Any idea on this?  I've noticed it as well and can't deploy a script that is supposed to fix things to have it break something else.

I've noticed that it resets your printer back to your default windows printer.  So if we had a mxd with a layout set to 36x68 on our plotter or a pdf writer, then after changing the source data, the layout is now set to our default laser printer at 8.5x11.  Not cool.  We are using Arc 10.0.

As the orginal poster noted, why does the printer settings get changed when all you are changing is the source of the data?  Especially when this data is a duplicate of the previous data source?

Thanks.
0 Kudos
MichaelVolz
Esteemed Contributor
Lorinda:

There is a bug for this issue NIM092164 that ESRI has classified with a low severity (I don't agree with that assessment).  An ESRI Technical Support supervisor told me that only 6 users has identified this as an issue.  Please contact technical support about this issue so they might have more of an impetus to fix the bug.  I need to run my script in v10.0 before upgrading to v10.2, but the bug may still exist in v10.2 (I do not know).  What version of ArcGIS Desktop are you running python with?
0 Kudos
LorindaGilbert
Frequent Contributor
Good Morning,
Thanks for the reply.  Using V10.0 SP2.  I'm testing whether changing your default printer makes a difference, but that is an added step for the users.  Some of my users are not as computer/ESRI savvy as the rest of us and I'm just attempting to make the server switch easier for them (which there will be more in the future).  This is NOT accomplishing this goal.

I have reported as a bug and related it to the issue NIM092164 too.  Thank you for letting me know that it had been reported and needed more reports to maybe get them off the stick and get it fixed.  I too will be upgrading to the newer releases, but luckily, this will be after this server change.

I agree that it is a bug that SHOULD be fixed.  There is ABSOLUTELY NO REASON for the printer and layout settings to be changed if you are not touching the layout window or doing anything with printing.  Or there should be a way of finding the printer and layout settings in Python to be able to be retained during updates.

The test for changing the default printer was minimially productive.  Most of our maps are either printed to a large size plotter or to Cute pdf writer.  If you use this pdf writer as the default printer in the windows system, then it will keep the pdf writer as the default, but still changes it to be the 8.5x11 size paper, but does retain the custom size the Map Page Size.  Interestingly, if you set the default to your plotter or to Adobe pdf printer, it changes the extent to 8.5x11 for both the paper size and the map paper size.  GRRRR

Thanks.
0 Kudos
MichaelVolz
Esteemed Contributor
Lorinda:

Does your python script work on updating the datasources for annotation feature classes in SDE?  I thought mine was processing this type of layer, but it is not.
0 Kudos
LorindaGilbert
Frequent Contributor
That's a good question, I'm not sure.  We don't have that many layers of annotation.  The one mxd that I know has annotation is coming from a data source that wasn't changed.  I'll have to look through some of the 200+ mxds to see if any have annotation.  I'll do it the 'easy way' by looking at the text file that I had written out to show what was changed and if any broken links in the mxd.  I did notice that if there was a very old data source listed that no longer exists, it was less likely to change them and reported them as broken links.

I'll get back with an update in a bit.
0 Kudos