I am trying to get the string associated with a spatial reference to print into an xml using arcpy and xml.cElementTree. Using the snippet:

spatial_ref = arcpy.GetParameterAsText(3)
Prj = et.SubElement(spaceref, "Prj")
space = arcpy.SpatialReference(spatial_ref)
Prj.text = arcpy.SpatialReference.exportToString(space)

Using essentially the same method, I am able to get the string to print with IDLE by pointing directly to a projection (.prj) file, e.g.:

u"GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"

but Arc returns this error when I try to run my script:

Traceback (most recent call last): File "\DEFOE\Proc Storage\ORTHO\Arc Custom Tools\ImportUltracam.py", line 99, in space = arcpy.SpatialReference(spatial_ref) File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 927, in init self._arc_object.createFromFile(item) RuntimeError: ERROR 999999: Error executing function.

Is there a way to input a spatial reference with Arc and return the string to be printed into the xml?

Update Playing with IDLE, I tried with an EPSG code and was able to do exactly what I wanted:

>>> epsg = arcpy.SpatialReference(32618)
>>> print epsg
<geoprocessing spatial reference object object at 0x000000000C7D0710>
>>> arcpy.SpatialReference.exportToString(epsg)
u"PROJCS['WGS_1984_UTM_Zone_18N',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-75.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]];-5120900 -9998100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision"

So I tried with an EPSG code in the script:

spatial_ref = "32618"
Prj = et.SubElement(spaceref, "Prj")
space = arcpy.SpatialReference(spatial_ref)
Prj.text = arcpy.SpatialReference.exportToString(space)

And I get the same error.


I couldn't find anything that would indicate why the file path version isn't working but I noticed that the EPSG code in your second script is being created/passed as a string whereas the documentation seems to expect an integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.