Getting a spatial reference from a script tool parameter

03-10-2015 07:00 AM
MVP Frequent Contributor

Been stuck on this for a while this afternoon.

I have been writing a script tool which loads some data from a table in a db.

I needed to then set the spatial reference of the coordinates.

So, in my script I had :

SR = arcpy.GetParameterAsText(1)

And in the tool properties, this is set to a Coordinate System data type. So you get the nice pull down list of the available coordinate systems.

However, this returns a text representation of the prj, not a spatial reference object.

Then I tried to do this :

SR = arcpy.SpatialReference(arcpy.GetParameterAsText(1))

But you get an error because SR objects can only be created using :

1. Get them from a describe object

2. The WKID of the SR

3. A path to a projection file.

You cannot use a text string.

The answer turned out to be :

SRtxt = arcpy.GetParameterAsText(1)

SR = arcpy.SpatialReference()  # an empty spatial reference object



Now on with the rest of it....

13 Replies
MVP Regular Contributor

Interesting little tidbit. Thanks for sharing.

0 Kudos
MVP Regular Contributor

Did you try arcpy.GetParameter(1) instead of GetParameterAsText? GetParameter returns the parameter as an object instead of a string.

0 Kudos
MVP Frequent Contributor

I did try GetParameter, same result, it doesn't return a SR object, but a string!

I looked and looked through the documentation and found no pointers how to do this.

If the esri folk are listening, perhaps something should be in in the help files on how to do this.

My solution works, but is there another way to do it?

0 Kudos
MVP Frequent Contributor

Oh, and another thing...

How do you get the advanced editor when starting a new thread so that you can do the code formatting correctly?

0 Kudos
Esri Esteemed Contributor

If you start a new discussion you should already be in the advanced editor.


Esri Esteemed Contributor

... with respect to the coordinate system, remember that coordinate system and spatial reference are not the same. Look at the following tool, where a coordinate system and a spatial reference can be specified (both using the same interface!):


If the script for this tool is:

import arcpy

crdsys_astext = arcpy.GetParameterAsText(0)

crdsys_assobject = arcpy.GetParameter(0)

sr_astext = arcpy.GetParameterAsText(1)

sr_assobject = arcpy.GetParameter(1)

arcpy.AddMessage("crdsys_astext  : {0}".format(crdsys_astext))

arcpy.AddMessage("crdsys_asobject: {0}".format(crdsys_assobject))

arcpy.AddMessage("sr_astext      : {0}".format(sr_astext))

arcpy.AddMessage("sr_asobject    : {0}".format(sr_assobject))

then this would output:

Start Time: Tue Mar 10 22:55:26 2015

Running script Script...

crdsys_astext  : PROJCS['RD_New',GEOGCS['GCS_Amersfoort',DATUM['D_Amersfoort',SPHEROID[' ... etc

crdsys_asobject: PROJCS['RD_New',GEOGCS['GCS_Amersfoort',DATUM['D_Amersfoort',SPHEROID[' ... etc

sr_astext      : PROJCS['RD_New',GEOGCS['GCS_Amersfoort',DATUM['D_Amersfoort',SPHEROID[' ... etc

sr_asobject    : <geoprocessing spatial reference object object at 0x181E6E78>

Completed script Script...

So, to get the sr object, you should define the parameter as spatial reference and read it as object, not as text.

0 Kudos
MVP Frequent Contributor


see this..

My code

# test spatial reference
import sys, os
import arcpy

SR = arcpy.SpatialReference(arcpy.GetParameter(0))

msg = "Coord sys is {}".format(
print msg



0 Kudos
Esri Esteemed Contributor

... and if you change the definition of the parameter type to spatial reference (instead of coordinate system)?

0 Kudos
MVP Frequent Contributor

Did that, still doesn't work.

If you look at your code above, you are printing the contents of the variable.

Its still not an SR object.

0 Kudos