Arcpy.mapping Get the extent of the map elements.

3510
7
10-13-2015 10:13 AM
GerryGabrisch
Occasional Contributor III

I have a Python script that will automatically;  export the current MXD to a JPG in the same directory as the MXD, name the JPG the same as the MXD with a date added on, store the path to the MXD in a CSV file (including the requester's name), and optionally open the JPG and/or the file location in with Windows Explorer.

I would like to improve the script by ensuring that the output JPG is clipped to all graphic elements.  Exporting the JPG clipped to the graphic extent is an option if you export via the File-Export Map menu but there is not a handy way of getting those values in Arcpy.mapping.

I know I could iterate through all the map elements and identify the xy min/max values, and I am guessing I could clip the output graphic using the Python Image Library.

Does anyone have a arcpy.mapping solution to limiting the export JPG to the same extent as the map elements and not the same size as the page size?

0 Kudos
7 Replies
FreddieGibson
Occasional Contributor III

You should be able to get the position of the element on the page from each element object. For example, if you look on the following page for GraphicElement you'll see that there are elementHeight and elementWidth properties. You could get the anchor for the element from the elementPositionX and elementPositionY, but you'd have to know in advance where the anchor is so that you'd be able to do the needed math to determine the extents of the element on the page.

GraphicElement

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/graphicelement-class.htm

0 Kudos
DanPatterson_Retired
MVP Emeritus

Freddie getting an error with your link... copy this one over

GraphicElement—Help | ArcGIS for Desktop

0 Kudos
FreddieGibson
Occasional Contributor III

Hi Dan,

I've been having some real problems with GeoNET and links lately and I'm also getting an error with the link you provided. I'm going to talk with Timothy Hales​ about it tomorrow, but GeoNET appears to really hate when I answer questions from Microsoft Edge on my Windows 10 machine and Safari on my Mac. Typically I have to write my responses, post them, and then edit them a couple of times to get the links to work.

DanPatterson_Retired
MVP Emeritus

Grief... they are the two browsers I was using.  I will check the html content to see if anything is amiss.  In Safari, I just copied and pasted, In Edge, I usually have to highlight some words, then use the link tool to produce the link...

ADDENDUM

Tried to edit my original link... it is broken... here is the contents, parsed for reading... I took out the enclosing < a and < / a stuff

    href=" http:  //desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/graphicelement-class.htm

and as a link

GraphicElement—Help | ArcGIS for Desktop

TimothyHales
Esri Notable Contributor

I am seeing the same behavior for this link in a test post. I have logged a case with Jive to determine the issue. I think it may have something to do with the added prefix and formatting that Jive does.

https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fdesktop.arcgis.com%2Fen%2Fdesktop%2Fl...

0 Kudos
DanPatterson_Retired
MVP Emeritus

So getting back to you Gerry... did you figure it out through all the distraction?

0 Kudos
GerryGabrisch
Occasional Contributor III

Dan,

I can get the extent of all the elements in an MXD and I can use those values and the Python Image Library to crop the exported image, but for some unknown reason the clipped output has a few pixels trimmed off of the left and bottom edges of the image. I am not sure of the cause without more investigation.  See the example below.

cropTest2.jpg

0 Kudos