current extent in geographic coordinates using python

1874
6
Jump to solution
11-19-2012 04:07 PM
BryanTaylor
New Contributor
Here is my problem:  I have been using the maptokml tool extensively.  My map documents are not in geographic coordinates, nor are the other users map documents that I support.  When I use the maptokml tool, I am constantly having to MANUALLY change the display coordinates to decimal degrees and manually enter them in the extent box every time I want to export the current view and then change the data frame display coordinates back.  This is terribly annoying, time consuming, and error-prone.  I would like a simple method or routine to get the geographic coordinates of the current map view so that I can run them through the maptokml tool in a script to automate this process. 

For the record, I wasted the better part of a day writing a script to get the current view extent, create a polygon, put it into a temp feature class, reproject that feature class, create a cursor, read the extent of the polygon feature (or read the extent of the feature class) and then plug those into the maptokml tool.  It was painful, full of errors (a lot of errors from bad syntax in ESRI help), frustrating, and is still not working correctly.  It never reprojected the coordinates for the polygon despite creating a new spatial reference and creating the polygon with that reference.  I'm just burned out.....anybody have a working tool or suggestion that I might run with?  Should I just try to change the current dataframe projection?  Reproject a polygon/feature class and then query the extent?  Read the display coordinates (if that is possible)??  HHHEEELLPPPPP!!!!

Thanks in advance....
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MikeHunter
Occasional Contributor
You wanted simple: This is about as simple as it gets with this kind of stuff:

import arcpy  mxd = arcpy.mapping.MapDocument("Current") #Check for layout view if mxd.activeView == "PAGE_LAYOUT":     arcpy.AddWarning( "Does not work in " +         "layout view, please change to data view")     sys.exit() #or whatever      else:     dataframe_string = mxd.activeView     dataframe = arcpy.mapping.ListDataFrames(mxd, dataframe_string)[0]     oldspatialref = dataframe.spatialReference      spatialref = 'c:/work/arcview10/misc/WGS1984.prj'      #set to wgs84     dataframe.spatialReference = spatialref      #get coors of extent center in new coordinate system     x = (dataframe.extent.XMin + dataframe.extent.XMax)/2     y = (dataframe.extent.YMin + dataframe.extent.YMax)/2      # set dataframe spatial ref back     dataframe.spatialReference = oldspatialref      #now do what you need to with the wgs84 coors


Note the hard coded path to the wgs84 projection file, you will have to change that to suit your situation.

If this is not exactly what you want, hopefully it will give you some ideas and you can get your work done.

good luck,
Mike

View solution in original post

0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Bryan,

You can try using the following GP tool which will copy the coordinates in decimal degress to your clipboard:

http://epro.maps.arcgis.com/home/item.html?id=e68ca4b6880f477ca73b3557546dc49b
0 Kudos
BryanTaylor
New Contributor
Hi Bryan,

You can try using the following GP tool which will copy the coordinates in decimal degress to your clipboard:

http://epro.maps.arcgis.com/home/item.html?id=e68ca4b6880f477ca73b3557546dc49b


The tool works to copy the coordinates for the entered points (which I don't want to do, I want to read the current view extent).  But it only copies the coordinates in the current coordinate system, which in my case, isn't decimal degrees, it's a state plane projection.  The tool solution requires more steps (and additional functionality) than just manually changing things and copying them.  But thanks for trying....
0 Kudos
JakeSkinner
Esri Esteemed Contributor
The tool should create the coordinates in decimal degrees.  I tested against a feature class added to ArcMap (both the feature class and data frame were set to State Plane Feet).  I zoomed to the extent I wanted, added a point to the lower left corner, then to the upper right (which will give me the extent).  After pasting the results from the clipboard to notepad, the coordinates were in decimal degrees.
0 Kudos
MikeHunter
Occasional Contributor
You wanted simple: This is about as simple as it gets with this kind of stuff:

import arcpy  mxd = arcpy.mapping.MapDocument("Current") #Check for layout view if mxd.activeView == "PAGE_LAYOUT":     arcpy.AddWarning( "Does not work in " +         "layout view, please change to data view")     sys.exit() #or whatever      else:     dataframe_string = mxd.activeView     dataframe = arcpy.mapping.ListDataFrames(mxd, dataframe_string)[0]     oldspatialref = dataframe.spatialReference      spatialref = 'c:/work/arcview10/misc/WGS1984.prj'      #set to wgs84     dataframe.spatialReference = spatialref      #get coors of extent center in new coordinate system     x = (dataframe.extent.XMin + dataframe.extent.XMax)/2     y = (dataframe.extent.YMin + dataframe.extent.YMax)/2      # set dataframe spatial ref back     dataframe.spatialReference = oldspatialref      #now do what you need to with the wgs84 coors


Note the hard coded path to the wgs84 projection file, you will have to change that to suit your situation.

If this is not exactly what you want, hopefully it will give you some ideas and you can get your work done.

good luck,
Mike
0 Kudos
BryanTaylor
New Contributor
The tool should create the coordinates in decimal degrees.  I tested against a feature class added to ArcMap (both the feature class and data frame were set to State Plane Feet).  I zoomed to the extent I wanted, added a point to the lower left corner, then to the upper right (which will give me the extent).  After pasting the results from the clipboard to notepad, the coordinates were in decimal degrees.


I am using the same data frame projection (state plane) and all my layers are state plane also.  I had to load the script into the tool, then I didn't have the pywin32 module loaded either, so I installed that.  When I ran the tool, I did get all the coordinates, but they were also in state plane and not geographic.  With that said, the clipboard info that your answer provided will be extremely handy going into the future, so thanks.
0 Kudos
BryanTaylor
New Contributor
You wanted simple: This is about as simple as it gets with this kind of stuff:

Note the hard coded path to the wgs84 projection file, you will have to change that to suit your situation.

If this is not exactly what you want, hopefully it will give you some ideas and you can get your work done.

good luck,
Mike


This IS exactly what I was looking for.  I was going down this path today before I left work, but only after wasting time tweaking and undoing what I did yesterday.  Thanks for the simple and effective answer....
Bryan
0 Kudos