Arcpy user defined spatial reference

849
5
06-29-2022 03:20 AM
MichaelFowler1
New Contributor III

Hi All!!

So I'm currently writing a script that will allow end users to create a map, and the only hiccup I'm having is the spatial reference. 

When I run the below code I receive the below error. But since its the generic error I'm not too sure what's going wrong.

arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(arcpy.GetParameterAsText(3))

RuntimeError:  ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds.

 In the toolbox I have the data type set as 'Coordinate System'.

Any help would be appreciated.

Thank you.

0 Kudos
5 Replies
by Anonymous User
Not applicable

It could be something else in your script, if there is more to it so step through it with the debugger.

If the error is on this part, what is getting past as in the GetParameterAsText? SpatialReference accepts the projection (.prj), the name of the projection as text ("Hawaii Albers Equal Area Conic") or the spatial reference's factory code (32145) but not '32145'.  Does it work if the inputs are hardcoded?

arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(32145)

 

0 Kudos
MichaelFowler1
New Contributor III

See my comment below on what happens when i try and hard code a coordinate system

0 Kudos
Luke_Pinner
MVP Regular Contributor

Use the SpatialReference or CoordinateSystem objects directly (i.e. use arcpy.GetParameter instead of GetParameterAsText)

Luke_Pinner_0-1656536305023.png

 

import arcpy

#This works
sr = arcpy.GetParameter(0)
arcpy.env.outputCoordinateSystem = sr

#So does this
crs = arcpy.GetParameter(1)
arcpy.env.outputCoordinateSystem = crs

 

 

0 Kudos
MichaelFowler1
New Contributor III

I have tried both the above methods and its still not working for me.

So i have tried running a hard coded test in the python window:

crs = 'WGS 1984'
arcpy.env.outputCoordinateSystem = crs

RuntimeError: Object: Error in accessing environment <outputCoordinateSystem>

utm = 'WGS 1984 UTM Zone 56S'
arcpy.env.outputCoordinateSystem = utm

RuntimeError: Object: Error in accessing environment <outputCoordinateSystem>

 

Would this be because of a part earlier in my script?

0 Kudos
Luke_Pinner
MVP Regular Contributor

@MichaelFowler1 wrote:

I have tried both the above methods and its still not working for me.

Weird, works fine for me.


@MichaelFowler1 wrote:

 

crs = 'WGS 1984'
arcpy.env.outputCoordinateSystem = crs

RuntimeError: Object: Error in accessing environment <outputCoordinateSystem>

utm = 'WGS 1984 UTM Zone 56S'
arcpy.env.outputCoordinateSystem = utm

RuntimeError: Object: Error in accessing environment <outputCoordinateSystem>

 

 

If you want to hardcode a spatial reference:

 

crs = arcpy.SpatialReference(4326) # 'WGS 1984' https://epsg.io/4326
arcpy.env.outputCoordinateSystem = crs

utm = arcpy.SpatialReference(32756) # 'WGS 1984 UTM Zone 56S' https://epsg.io/32756
arcpy.env.outputCoordinateSystem = utm

 

 

 

 

0 Kudos