setting data frame extent does not work in select cases

534
1
08-15-2012 09:57 AM
PaulPadegimas
New Contributor
I have a python script that effectively creates a series of maps by iterating over a set of geodatabases and replaces data sources in a template file.  Part of the the script loops over the layers in the sole data frame and sets the extent of the data frame and the spatial reference based on those of a layer within.  After the loop I change the scale by using a multiplier, set the data frame spatial reference, and refresh my active view.  Example of code is below:

        # define data frame
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        # set the utm spatial reference and zoom to layer
        sr = ""
        for lyr in arcpy.mapping.ListLayers(mxd):
            if lyr.name == "neat_contour_55":
                print lyr.name
                df.extent = lyr.getSelectedExtent()
                sr = arcpy.Describe(lyr).spatialReference
        df.scale = df.scale * 1.25
        df.spatialReference = sr
        arcpy.RefreshActiveView()

I have used this portion of script before to change the data frame extent and it has worked successfully.  Recently I have run into an issue where about 5 percent of the maps I am attempting to create do not change extent.  The only thing that has changes in my system is that I have since upgraded to 10.1.  In the results, the spatial reference does change, and the data is properly replaced, but the extent upon which the map is focused remains where the data in the template resided.  For example, my template is based on Atlanta and I am mapping part of Seattle, and while the data is there, the map remains centered on Atlanta.  There is no error that occurs, the extent seems to be correct based on what I had printed during execution of my script, but the final map and associated pdf are incorrect and simply display the background states layer in the area of Atlanta.

If anyone has had any similar issues, or can offer any advice, it would be much appreciated.

Thank you!

Paul P.
Tags (2)
0 Kudos
1 Reply
markdenil
Occasional Contributor III
Thre was a bug in 10.0 that required one to set the dataframe extent twice, just to make sure it worked.
I had a similar problem, where it worked sometimes and then not.

To fix it, after I set the extent I would then create a new instance of the data frame object and set the extent on that one too.
That seemed to catch most all of the missed settings.

##      set extent (values in a list)
newExtent = df.extent
newExtent.XMin = extent[0]
newExtent.YMin = extent[1]
newExtent.XMax = extent[2]
newExtent.YMax = extent[3]
df.extent = newExtent
del newExtent

##      set extent a second time - v10 bug workaround"
df2 = arcpy.mapping.ListDataFrames(mxd, theFrame)[0]
anotherExtent = df2.extent
anotherExtent.XMin = extent[0]
anotherExtent.YMin = extent[1]
anotherExtent.XMax = extent[2]
anotherExtent.YMax = extent[3]
df2.extent = anotherExtent
del anotherExtent
 
##      set rotation and scale
df.rotation = rotation
df.scale = scale
0 Kudos