Select to view content in your preferred language

ArcMap 10 - Change coordinate system in mxd file

4079
7
05-26-2010 06:38 AM
RDHarles
Regular Contributor
I want to change the coordinate system in the Data Frame Properties through arcpy.mapping.MapDocument()  using python.

[ATTACH=CONFIG]1052[/ATTACH]

Here's the code I came up with:
import arcpy, sys
mxd = arcpy.mapping.MapDocument("base.mxd")

prjList = "q:/python/prj/"

# Read in the coordinate system definition
prj = arcpy.SpatialReference(prjList+sys.argv[1]+".prj")
# Get the code (e.g. 2288 = "NAD_1983_StatePlane_California_VI_FIPS_0406_Feet")
code = prj.PCSCode

df = arcpy.mapping.ListDataFrames(mxd)[0]
sr = df.spatialReference
sr.PCSCode = code
df.spatialRefence = sr

mxd.save()
del mxd
This code works ONLY if I first open the mxd file and hit the clear button (see image above).
If the coordinate system isn't cleared first, the code runs through without error but doesn't change the coordinate system.
Not sure if this is a bug or not? (If it's not a bug, I can find or figure out any way to clear the coordinate system through scripting).
Anyone have any experience with this?
Thanks!
0 Kudos
7 Replies
JasonScheirer
Esri Alum
How about this:

import arcpy, sys
mxd = arcpy.mapping.MapDocument("base.mxd")

prjList = "q:/python/prj/"

# Read in the coordinate system definition
prj = arcpy.SpatialReference(prjList+sys.argv[1]+".prj")

df = arcpy.mapping.ListDataFrames(mxd)[0]
df.spatialRefence = prj

mxd.save()
del mxd
0 Kudos
RDHarles
Regular Contributor
Thanks, Jason.
Unfortunately, that code still doesn't work, if there is already a coordinate system saved in the mxd.
It did work though after I cleared the coordinate system and saved off the mxd (same as what was happening originally).

What I find really surprising is that the following code also works (as long as the mxd contains no previous coordinate system). It appears it is reading the new .prj files?
A simple "open and save":
import arcpy, sys
mxd = arcpy.mapping.MapDocument("base.mxd")
mxd.save()
del mxd
Maybe these details will help:
I'm reprojecting directories (each containing an mxd) full of shapefiles that were previously in Lat/Long WGS84.  I've reprojected them to a State Plane coordinate system.  All I want now is for the Data Frame Properties in the mxd to match the new State Plane coordinate system.
0 Kudos
LaurentDupont
New Contributor
Did you finally find a way out ?

I'm exactly in the same situation as you are. Around 12000 MXDs that must be moved from one coordonate system to another.
I also intend to reproject on the fly thousands of shape files and rasters.
For this, I rely on a Python script. It's already able to convert shapes and rasters, but I havn't found the clue so far for mxd.
Your message gave me some hope.

As if that wasn't enougth, I run a 9.3 version of ArcMap. Arcpy is available since ArcMap 10 isn't it ?

Please let me know if you got through this, it could help me a lot.

Thank you
0 Kudos
RDHarles
Regular Contributor
Nope, still haven't gotten it to work (if coordinate system is already present in the mxd).  And yes,  you need the arcpy module (Arc10) to do this.
0 Kudos
FrankLoy
New Contributor
Hi,

this script works correctly,
in the scripts before are two problems.
Problem 1:
df.spatialRefence = prj
correct is df.spatialReference = prj
Problem 2:
false is prjFile = r"D:\Programme\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\UTM\Europe\ETRS 1989 UTM Zone 32N.prj"
correct is prjFile = r"E:\home\MXD_DE_NEU\ETRS_1989_UTM_Zone_32N.prj"

import arcpy, sys
mxd = arcpy.mapping.MapDocument(r"E:\home\MXD_DE_NEU\_MXD_DE\k8_2000_de.mxd")
outMXD = r"E:\home\MXD_DE_NEU\_MXD_DE\k8_2000_de_test.mxd"

prjFile = r"E:\home\MXD_DE_NEU\ETRS_1989_UTM_Zone_32N.prj"

# Read in the coordinate system definition prj = arcpy.SpatialReference(prjFile)

prj = arcpy.SpatialReference(prjFile)

print prj.name

df = arcpy.mapping.ListDataFrames(mxd)[0]

print df.spatialReference.name

df.spatialReference = prj

print "New Spatial Reference:" + df.spatialReference.name

mxd.save(outMXD)

del mxd
0 Kudos
LledSmith
Emerging Contributor
I was just having the same issue and this works with ut having to save a new file.  Thanks for posting this it helped me!

import os, arcpy   
mxd = arcpy.mapping.MapDocument("C://HDR//AKDatadrivenpages//DATADRIVEN_Map.mxd")
sr = arcpy.SpatialReference(r"C:\Program Files\ArcGIS\Desktop10.0\Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1927\NAD 1927 UTM Zone 10N.prj")
df = arcpy.mapping.ListDataFrames(mxd)[0]
print df.spatialReference.name
df.spatialReference = sr
print df.spatialReference.name
arcpy.RefreshActiveView
mxd.save()
del mxd


And this sets the SR to be the same as a different dataframe.  For some reason using Data Drvien Pages there is no way to set the overview maps SR.
import os, arcpy   
mxd = arcpy.mapping.MapDocument("C://HDR//AKDatadrivenpages//DATADRIVEN_Map.mxd")
df0 = arcpy.mapping.ListDataFrames(mxd)[0]
df1 = arcpy.mapping.ListDataFrames(mxd)[1]
print df0.spatialReference.name
df0.spatialReference = df1.spatialReference
print df0.spatialReference.name
arcpy.RefreshActiveView
mxd.save()
del mxd
0 Kudos
NobbirAhmed
Esri Regular Contributor
Jason's code is working for me. The intial SR was UTM 11 N and I was able to change it to California state plane.

import arcpy

mxd = arcpy.mapping.MapDocument(r"D:\dataframesr\df_sr.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]

prjfile = r"D:\dataframesr\ca_fips.prj"
sr = arcpy.SpatialReference(prjfile)

df.spatialReference = sr

mxd.save() 
del mxd


I'm on Win XP, ArcGIS 10.0 SP3.
0 Kudos