Setting Data frame extent

2953
4
Jump to solution
03-07-2013 07:24 PM
RoryHall
New Contributor III
Hello,
I have a script which updates a layer in a mxd on disc and want to set the extent of the Data Frame to the new layer, so when the mxd is opened the new layer is centered in the frame. I have poured of the web and found to get the extent of the layer to be zoomed to and set the data frame layer to that. ie "df.extent = lyr.getSelectedExtent(). When I run this script I get a RuntimeError DataFrameObject: Error in setting extent.

A simplified extract of my code as follows:
import arcpy

mxd = arcpy.mapping.MapDocument("C:\SRWBA\Braemar1959\Braemar1959.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]

for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == "BenefitArea_Symbology":
            print "found it"
            arcpy.SelectLayerByAttribute_management(lyr)
            print "selected"
            lyr_extent = lyr.getSelectedExtent()           

print df.extent
print lyr_extent

newExtent = df.extent
newExtent.XMin = lyr_extent.XMin
newExtent.YMin = lyr_extent.YMin
newExtent.XMax = lyr_extent.XMax
newExtent.YMax = lyr_extent.YMax

print newExtent
df.extent = newExtent
mxd.save()
del mxd, lyr
print "finished"

I have expanded on the code to try and make sense of what is going on. I get the extents I need, but cant seem to set the Data Frame extent. You help will be very appreciated, this is driving me insane as it must be something very simple I am missing. Thanks in advance.
Cheers ~ Rory
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RoryHall
New Contributor III
That was the problem. I had the document set to Fixed Extent.
One needs to think out side the box (or in this case the "Shell")

my working code:

import arcpy mxd = arcpy.mapping.MapDocument("C:\SRWBA\Braemar1959\test.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0]  for lyr in arcpy.mapping.ListLayers(mxd):         if lyr.name == "TestLayer":                        arcpy.SelectLayerByAttribute_management(lyr)                         lyr_extent = lyr.getSelectedExtent()             arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")  df.extent = lyr_extent df.scale = df.scale * 1.25 mxd.save() del mxd, lyr print "finished"


Thanks gentlemen

View solution in original post

0 Kudos
4 Replies
JeffBarrette
Esri Regular Contributor
One quick thought.

Isn't df = arcpy.mapping.ListDataFrames(mxd, "")[0] looing for a df without a name?

Shouldn't you use:
df = arcpy.mapping.ListDataFrames(mxd)[0]
 


or

df = arcpy.mapping.ListDataFrames(mxd, "actual df name goes here")[0]
 


Jeff
0 Kudos
JimCousins
MVP Regular Contributor
Rory,
I had success running your code (see below), so your simplification for posting eliminated your error source.
Regards,
Jim
[HTML]import arcpy

mxd = arcpy.mapping.MapDocument("F:\TestMap\TestMap.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == "TstResults":
        print "found it"
        arcpy.SelectLayerByAttribute_management(lyr)
        print "selected"
        lyr_extent = lyr.getSelectedExtent()

print df.extent
print lyr_extent

newExtent = df.extent
newExtent.XMin = lyr_extent.XMin
newExtent.YMin = lyr_extent.YMin
newExtent.XMax = lyr_extent.XMax
newExtent.YMax = lyr_extent.YMax

print newExtent
df.extent = newExtent
mxd.save()
del mxd, lyr
print "finished"[/HTML]
RoryHall
New Contributor III
Thank you gentlemen,

I have run this at home and you are right it works just fine, but the mxd I have been working on at work refuses to play ball. All is not lost, I think and I will check on Monday that maybe I have the document set to a fixed extent, which I am hoping is the problem. And to add, that's what I like about python is there are so many ways to do things, gives newbies like myself a bit of slack when experimenting.
I will post again when I check my mxd at work.

Cheers ~ Rory

ps next time I post code I will follow the guidelines
🙂
0 Kudos
RoryHall
New Contributor III
That was the problem. I had the document set to Fixed Extent.
One needs to think out side the box (or in this case the "Shell")

my working code:

import arcpy mxd = arcpy.mapping.MapDocument("C:\SRWBA\Braemar1959\test.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0]  for lyr in arcpy.mapping.ListLayers(mxd):         if lyr.name == "TestLayer":                        arcpy.SelectLayerByAttribute_management(lyr)                         lyr_extent = lyr.getSelectedExtent()             arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")  df.extent = lyr_extent df.scale = df.scale * 1.25 mxd.save() del mxd, lyr print "finished"


Thanks gentlemen
0 Kudos