1

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.

  • Here's a wild stab: Have you tried the getOutput method on your spatial reference string? help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… ; resources.arcgis.com/en/help/main/10.1/index.html#//… ; gis.stackexchange.com/questions/55246/… – Jason Mar 5 '14 at 15:03
  • 1
    I'm not sure calling a spatial reference is considered a tool with an output value. I got the error: AttributeError: 'SpatialReference' object has no attribute 'getOutput' – Wes Mar 5 '14 at 15:12
  • I wouldn't expect assigning a unicode string to an etree object to cause problems, but have you tried decoding to an ascii string? – Jason Mar 5 '14 at 15:47
  • 1
    I noticed that your EPSG code example is passing an integer in the IDLE repl but you're using a string in the actual script. Looking at the documentation for arcpy.SpatialReference, it looks like it expects an integer. – Evicatos Jun 11 '14 at 0:03
  • @Wes Done! I'm glad I could help. – Evicatos Jun 12 '14 at 17:18
3

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.