Select to view content in your preferred language

Problem with changing Spatial Reference of data frame using Python

3411
3
10-11-2012 12:18 PM
CrystalCarreon
Emerging Contributor

Hi-
I am trying to understand how to change the coordinate system of a data frame using python. I have tried the following code but itdoesn't seem to work:

import arcpy.mapping
MXD = arcpy.mapping.MapDocument(Path to .mxd)
prjFile = Path to .prj file <-- NOTE: I am not using the InstalDir/Coordinate Systems files.
sr = arcpy.spatialReference(prjFile)

for DF in arcpy.mapping.ListDataFrames(MXD):
   DF.spatialReference = sr

It seems I can get properties such as the name, type etc. of the data frame coordinate system just fine but I am unable to set thecoordinate system using the above code. I do note that I am NOT using the install coordinate system files. Reason being is I am working on a citrix version of ArcMap, so effectively I don't have access to these files as a normal desktop user would. I was thinking Icould just copy the reference system from a feature class sitting on a computer I do have access to but, again, this doesn't seem to work. Do I need to have access to the installation coordinate systems files in order for this to work? Or is there a way around this? Or is my code totally wrong?

Any help is much appreciated!
Crystal

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Crystal,

Are you saving your MXD after you apply the change?  Ex:

import arcpy
from arcpy import mapping

prjFile = r"C:\DATA\VECTOR\LOCAL\chester_county.prj"
sr = arcpy.SpatialReference(prjFile)

mxd = mapping.MapDocument(r"C:\temp\python\Airports.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
   df.spatialReference = sr

mxd.save()

del mxd
CrystalCarreon
Emerging Contributor
I feel embarrassed to say this but no, I hadn't saved and that worked. It is working now. THANK YOU!
0 Kudos
DylanKennard
Occasional Contributor

You can also pipe in a WKID code in replace of a prjFile like sr = arcpy.SpatialReference(3857) which is the Web Mercator Projection to bypass that project file step.  Also I got hung up on the df.spatialReference = sr .....I capitalized Spatial as some help articles on ESRI reference it that way.  Thanks for the clear exacting code! It helped me stop pulling my hair out.

0 Kudos