Select to view content in your preferred language

Python cannot set extent correctly

2011
10
06-09-2011 04:51 AM
RichardKaufholz
Deactivated User
Hi there,

I am using the script from David Spriggs that prints a map to PDF. I am having a problem with the code "applying" the new extent to the map before printing.

The code is:
arcpy.AddMessage("Input extent pts: %s %s %s %s" % (newPntLL.X, newPntLL.Y, newPntUR.X, newPntUR.Y))

myExtent = dataFrame.extent
arcpy.AddMessage("Original extent pts: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))
arcpy.AddMessage("... setting my extent xmin to: %s" % newPntLL.X)
myExtent.XMin = newPntLL.X
arcpy.AddMessage("... setting my extent ymin to: %s" % newPntLL.Y)
myExtent.YMin = newPntLL.Y
arcpy.AddMessage("... setting my extent xmax to: %s" % newPntUR.X)
myExtent.XMax = newPntUR.X
arcpy.AddMessage("... setting my extent ymax to: %s" % newPntUR.Y)
myExtent.YMax = newPntUR.Y
arcpy.AddMessage("New extent pts: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))
dataFrame.extent = myExtent

arcpy.AddMessage("Final MAP extent pts: %s %s %s %s" % (dataFrame.extent.XMin, dataFrame.extent.YMin, dataFrame.extent.XMax, dataFrame.extent.YMax))


And the output is:
Input extent pts: 3448672.2243 -3490621.8401 3449675.4603 -3490056.9226
Original extent pts: 2055276.37123 -4018755.96024 2055518.36111 -4018583.26583
... setting my extent xmin to: 3448672.2243
... setting my extent ymin to: -3490621.8401
... setting my extent xmax to: 3449675.4603
... setting my extent ymax to: -3490056.9226
New extent pts: 2055518.36111 -4018583.26583 3449675.4603 -3490056.9226
Final MAP extent pts: 2055518.36111 -4251785.42335 3449675.4603 -3256854.76508


Baffling! Why does myExtent.XMin not show the 3448672 value? Its being explicitly defined there. Whats more confusing, is that it DOES show myExtent.XMax correctly...

Hopefully, its something obvious in there that I am missing,. Any help would be greatly appreicated.

Thanks in advance,
Richard.
Tags (2)
0 Kudos
10 Replies
ChrisMathers
Deactivated User
Are you sure that is the same aspect ratio as the dataframe? You cant change to an extent that doesnt fit the frame. It looks like the dataframe object took your xmax and just calculated the other three to fit. Not that thats a useful function. Try changing two or three attributes and letting the others be generated to see if you get the proper extent.
0 Kudos
RichardKaufholz
Deactivated User
Hi Chris.

Thanks for your thoughts. I tested with your idea by manually inputting the extent values - which I collected manually by viewing an extent within the map layout template itself (i.e. definitely fits).

modified code:
arcpy.AddMessage("Input extent pts: %s %s %s %s" % (newPntLL.X, newPntLL.Y, newPntUR.X, newPntUR.Y))

myExtent = dataFrame.extent
arcpy.AddMessage("Original extent pts: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))
arcpy.AddMessage("... setting my extent xmin to: 3449977")
#myExtent.XMin = newPntLL.X
myExtent.XMin = 3449977
arcpy.AddMessage("... setting my extent ymin to: -3492834")
#myExtent.YMin = newPntLL.Y
myExtent.YMin = -3492834
arcpy.AddMessage("... setting my extent xmax to: 3450982")
#myExtent.XMax = newPntUR.X
myExtent.XMax = 3450982
arcpy.AddMessage("... setting my extent ymax to: -3492116")
#myExtent.YMax = newPntUR.Y
myExtent.YMax = -3492116
arcpy.AddMessage("New extent pts: %s %s %s %s" % (myExtent.XMin, myExtent.YMin, myExtent.XMax, myExtent.YMax))
dataFrame.extent = myExtent

arcpy.AddMessage("Final MAP extent pts: %s %s %s %s" % (dataFrame.extent.XMin, dataFrame.extent.YMin, dataFrame.extent.XMax, dataFrame.extent.YMax))


And the modified output:
Input extent pts: 3448672.2243 -3490621.8401 3449675.4603 -3490056.9226
Original extent pts: 2055276.37123 -4018755.96024 2055518.36111 -4018583.26583
... setting my extent xmin to: 3449977
... setting my extent ymin to: -3492834
... setting my extent xmax to: 3450982
... setting my extent ymax to: -3492116
New extent pts: 2055518.36111 -4018583.26583 3450982.0 -3492116.0
Final MAP extent pts: 2055518.36111 -4253281.1636 3450982.0 -3257418.10223


So, now with confirmed coordinates, the values are still not sticking to the extent variables...

Richard.

Why cant computers just do what they are told?
0 Kudos
ChrisMathers
Deactivated User
That wouldnt be any fun though 😉

It could also be that once an extent object is created the properties are read only. http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Extent/000v0000006r000000/ You were trying to edit a static object.

try:
myExtent=arcpy.Extent(3449977,-3492834,3450982,-3492116)
dataFrame.extent = myExtent
0 Kudos
RichardKaufholz
Deactivated User
Yes, "fun" is one way to put it!

Here are three, very compelling, reasons why your suggestion will not work:

1. The original code for this section is taken (unedited) from a working sample posted by David Spriggs at ESRI
2. It has worked in the past (cant really say under which circumstances it did work unforatuntely...)
3. The "readonly" variables for xmax and ymax are set correctly, but the xmin and ymin are not

And yet, despite that very clear evidence, your suggestion has worked.

Sometimes, I really dont know why I do this! But thanks a million for your help - lifesaver!

Richard.
0 Kudos
DavidHollema
Deactivated User
I saw very similar results in following the Dave Spriggs example for an export to PDF service we're running here.  It worked for weeks and then just stopped working about 3 weeks ago and as far as I can tell, no one upgraded/modified ArcGIS (Desktop, Server) anything on our server.  I tried the workaround of creating a new arcpy.Extent object and all seems fine with the new extent again.  Can't figure out why the published Spriggs example stopped working.

Thanks for the help.
0 Kudos
GraemeBrowning
Frequent Contributor
Thanks from me too Chris

I thought I was going crazy when I seemed to lose control over what I thought should be a very straightforward extent change.

- Graeme
0 Kudos
ChrisFox3
Frequent Contributor
I believe this is what is causing the issue. First the extent of the data frame is defined as:

XMIN: 2055276.37123
YMIN:-4018755.96024
XMAX: 2055518.36111
YMax: -4018583.26583

The first thing the code tries to do is set the XMIN of the extent to 3448672.2243 which would give you the following extent:

XMIN: 3448672.2243
YMIN:-4018755.96024
XMAX: 2055518.36111
YMax: -4018583.26583

The problem is that now you have an extent where the XMIN is greater than the XMAX and this is not a valid extent so the software automatically changes XMIN to the closest valid value for the extent, 2055518.36111.

When you try to set XMAX to 3450982 it works fine because it is larger then XMIN. This code will work sometimes, but only in the events when you attempt to set a new XMIN or YMIN that is less than the XMAX or YMAX.

The better way to approach this would be like the option discussed by Chris Mathers to create a new extent object from your extent values and assign it to the extent of the dataframe.

myExtent=arcpy.Extent(3449977,-3492834,3450982,-3492116)
dataFrame.extent = myExtent
0 Kudos
DerivenC
Deactivated User
Incidentally, this situation also happens in ArcObjects.NET (which makes sense actually).  My YMIN would not hold -- probably because of what Chris_Fox states about the MAX value being less than the MIN value.

Creating a new envelope and assigning it does not work for me.  But changing the extent's xmin, ymin, xmax, and ymax (in that order) twice in a row did.

IEnvelope currentExtent = mapDoc.ActiveView.Extent.Envelope;
currentExtent.XMin = xmin;
currentExtent.YMin = ymin;
currentExtent.XMax = xmax;
currentExtent.YMax = ymax;
// And repeat...
currentExtent.XMin = xmin;
currentExtent.YMin = ymin;
currentExtent.XMax = xmax;
currentExtent.YMax = ymax;


I must have burned a couple of hours trying to blame this on my code.  Glad I finally found this post.
0 Kudos
ChrisFox3
Frequent Contributor
So you find the following code doesn't work for you?

IEnvelope newExtent = new EnvelopeClass();
newExtent.XMin = xmin;
newExtent.XMax = xmax;
newExtent.YMin = ymin;
newExtent.YMax = ymax;
mapDoc.ActiveView.Extent.Envelope = newExtent


The reason I ask is because I still see potential problems in your code in the scenario where the value you have in xmin or ymin is greater than the xmax or ymax of the current ActiveView Envelope.
0 Kudos