arcpy change data frame coordinate system

8420
7
08-25-2010 01:02 PM
RyanKelley
New Contributor II
I have a tool that allows a user to zoom to a township/range for anywhere in the Western US... My data frame is in GCS NAD1983, but depending on where the user zooms, I want the data frame to reproject to a specific UTM Zone depending on where the township/range is located.

I've played with the spatialReference class, but no luck yet.

Any ideas?

Thank you in advance!!
0 Kudos
7 Replies
RyanKelley
New Contributor II
I guess I posted my question too soon...

prjFile = prjPath + "/" + "NAD 1983 UTM Zone 10N.prj"
sr = arcpy.SpatialReference(prjFile)
for DF in MAP.ListDataFrames(MXD):
    DF.spatialReference = sr
0 Kudos
ScottOatley
New Contributor II
I'm getting a runtime error when trying this.

prjFile = prjPath + "/" + "NAD 1983 UTM Zone 10N.prj"
sr = arcpy.SpatialReference(prjFile)  <--- runtime error 999999
for DF in MAP.ListDataFrames(MXD):
DF.spatialReference = sr 


I'm just entering this one line at a time in the python command line window as follows:

import arcpy
prjPath = "C:/Program Files .../"
prjFile = prjPath + "NAD 1983 UTM Zone 10N.prj"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
sr = arcpy.SpatialReference(prjFile)

Then get the runtime error.

Am I missing something, or does this not work with "CURRENT", or by entering one line at a time?

Thanks,
Scott
0 Kudos
ScottOatley
New Contributor II
Hello,

This is the path created by our Arc install:

prjPath = "C:/Program Files(x86)/ArcGIS/Desktop10.0/Coordinate Systems/Projected Coordinate Systems/UTM/NAD 1983/"

prjFile = prjPath + "NAD 1983 UTM Zone 10N.prj"
sr = arcpy.SpatialReference(prjFile)

The last statement causes a run-time error. I recreated that path as:

"C:/My Program Files(x86)/ArcGIS/Desktop10.0/Coordinate Systems/Projected Coordinate Systems/UTM/NAD 1983/"

and it works fine. The only difference I can think of is that the original "Program Files(x86)" folder is read-only where my "My Program Files(x86)" folder is read-write.

Any ideas?

Thanks,
Scott
0 Kudos
RyanKelley
New Contributor II
I get this error from time to time for a variety of tasks within a script... but I only seem to get it when running something in the ArcPy window in ArcMap.

Usually I just re-start ArcMap and everything is totally fine.  Why this happens... ???
0 Kudos
ScottOatley
New Contributor II
Hello,

This is the path created by our Arc install:

prjPath = "C:/Program Files(x86)/ArcGIS/Desktop10.0/Coordinate Systems/Projected Coordinate Systems/UTM/NAD 1983/"

prjFile = prjPath + "NAD 1983 UTM Zone 10N.prj"
sr = arcpy.SpatialReference(prjFile)

The last statement causes a run-time error. I recreated that path as:

"C:/My Program Files(x86)/ArcGIS/Desktop10.0/Coordinate Systems/Projected Coordinate Systems/UTM/NAD 1983/"

and it works fine. The only difference I can think of is that the original "Program Files(x86)" folder is read-only where my "My Program Files(x86)" folder is read-write.

Any ideas?

Thanks,
Scott


Figured this out. I missed a space in the original path. It's between "Files" and "(x86)."

Scott
0 Kudos
MarkJames
New Contributor II
I'm getting a runtime error when trying this. 

prjFile = prjPath + "/" + "NAD 1983 UTM Zone 10N.prj"
sr = arcpy.SpatialReference(prjFile)  <--- runtime error 999999
for DF in MAP.ListDataFrames(MXD):
DF.spatialReference = sr 


I'm just entering this one line at a time in the python command line window as follows: 

import arcpy 
prjPath = "C:/Program Files .../" 
prjFile = prjPath + "NAD 1983 UTM Zone 10N.prj" 
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd)[0] 
sr = arcpy.SpatialReference(prjFile) 

Then get the runtime error. 

Am I missing something, or does this not work with "CURRENT", or by entering one line at a time? 

Thanks, 
Scott


Could you explain a little further how this works? I haven't been able to figure out what I am missing to get this to work.
0 Kudos
JeffBarrette
Esri Regular Contributor
The issues identified earlier in this post were paths with typos.  It is extremely important to get the paths correct. 

As a simple test, I browsed to a PRJ file in the ArcGIS coordinate systems folder.  Here is a trick.  Hold down the SHIFT key and right-click on a file.  Then select "Copy as Path" from the context menu.

Then I opened a new map document and the Python window.  I typed:

sr = arcpy.SpatialReference(r  then I clicked Ctrl-V)  the result looked like

sr = arcpy.SpatialReference(r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone 10N.prj")

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.spatialReference = sr

If you are still having complications, please post your code.