Clear Data Frame Projection

4690
8
06-12-2013 01:27 PM
PatrickYoung
New Contributor II
How would I use Python to clear the data frame projection in ArcMap?

Thanks,
Patrick
Tags (2)
0 Kudos
8 Replies
ChrisSnyder
Regular Contributor III
This should theoretically do it:

undefinedSr = arcpy.SpatialReference() #Note this DOES NOT make a 'Unknown' SR obj, see below
mxd = r"C:\temp\my_mxd.mxd"
for df in arcpy.mapping.ListDataFrames(mxd):
   df.spatialReference = undefinedSr
mxd.save()
del mxd
 

However, I found this is actually a bit more difficult.... The trick is to get a SR object to be of a "Unknown" coordinate system. The only way I could do this was to run the DefineProjection tool, and manually clear out the 'Coordinate System' parameter - which makes its value then default to a magic value called Unknown (note this is NOT a string value of "Unknown", but something else). Once I had a FC defined with an Unknown SR, then:

undefinedSr = arcpy.Describe(unknownSrFC).spatialReference
mxd = r"C:\temp\my_mxd.mxd"
for df in arcpy.mapping.ListDataFrames(mxd):
   df.spatialReference = undefinedSr
mxd.save()
del mxd


Which worked...

Anyone know a better way to create an Unknown spatial reference object?

Tried all these, which do not work:

undefinedSr = arcpy.SpatialReference(None) 
undefinedSr = arcpy.SpatialReference(0) 
undefinedSr = arcpy.SpatialReference("Unknown")
undefinedSr = arcpy.SpatialReference(Unknown) 


Seems like there should be a EPSG code for Unknown/Undefined, but I can't find it...
0 Kudos
ShaunWalbridge
Esri Regular Contributor


Anyone know a better way to create an Unknown spatial reference object?


try this:

sr = arcpy.SpatialReference()
sr = sr.loadFromString('{B286C06B-0879-11D2-AACA-00C04FA33C20}')


That should create a proper 'unknown' spatial reference object. Source bug report.
ChrisSnyder
Regular Contributor III
>>> unknownSr = arcpy.SpatialReference()
>>> unknownSr.loadFromString('{B286C06B-0879-11D2-AACA-00C04FA33C20}')
>>> unknownSr.name
'Unknown'

Works great!

Nothing says Unknown coordinate system like '{B286C06B-0879-11D2-AACA-00C04FA33C20}'...
ShaunWalbridge
Esri Regular Contributor

Nothing says Unknown coordinate system like '{B286C06B-0879-11D2-AACA-00C04FA33C20}'...


Yes, I agree. You can pull out these class ID values from the bin/XMLSupport.dat file within your installation directory, but it'd be nice to have a 'named' way to create an unknown coordinate system. I've submitted an enhancement request on the problem.
0 Kudos
JonPedder
Occasional Contributor II
Thanks for this info, though I'm having an issue with this as it doesn't appear to play well with arcpy.DefineProjection_management(p,newSpatialReference).


newSpatialReference = arcpy.SpatialReference()
newSpatialReference = newSpatialReference.loadFromString('{B286C06B-0879-11D2-AACA-00C04FA33C20}')

def clearSR():

    # Copy FC's to the new database
    dsList = arcpy.ListDatasets()
    fcList = arcpy.ListFeatureClasses()
    projectList = dsList + fcList

    # Project the datasets and featureclasses and write to target db
    for p in projectList:
        arcpy.AddMessage('Defining Spatial Reference for {0}'.format(p))
        arcpy.DefineProjection_management(p,newSpatialReference)


Thanks

Jon
0 Kudos
JoeZhou1
New Contributor

arcpy.DefineProjection_management(p,newSpatialReference)  ”

This is error in arcgis 10.

ExecuteError: ERROR 000622: Failed to execute (Define Projection). Parameters are not valid.

ERROR 000628: Cannot set input into parameter coor_system.

0 Kudos
ShaunWalbridge
Esri Regular Contributor

Jon and Joe,​

The reason your code doesn't work is your second line:

newSpatialReference = newSpatialReference.loadFromString('{B286C06B-0879-11D2-AACA-00C04FA33C20}') 

This is setting newSpatialReference to the return value of the loadFromString function.

But loadFromString doesn't return anything, it sets the calling object value, so you have a None value

for the rest of your script, which Define projection doesn't like. To unset projection, just call loadFromString:

newSpatialReference = arcpy.SpatialReference()
newSpatialReference.loadFromString('{B286C06B-0879-11D2-AACA-00C04FA33C20}')

That will create the object, then assign the value with loadFromString. I tested it and it works fine with DefineProjection.

Cheers,

Shaun

0 Kudos
JoeZhou1
New Contributor

Test OK!

Thank you very much!

0 Kudos