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
SR.loadFromString(SRtxt)
Phew....
Now on with the rest of it....
Get parameter should work,
Try your procedure from the command line, which necessitates reading from an input file
And to get the advanced syntax, you have to click on the title of the thread and go to the actual thread itself and NOT reply from your inbox...at least in IE.
>>> import arcpy >>> input_shp = 'c:/!Scripts/Shapefiles/AOI_mtm9.shp' >>> desc = arcpy.Describe(input_shp) >>> SR = desc.spatialReference >>> SR <geoprocessing spatial reference object object at 0x089330E0> >>> SR_PCSCode = SR.PCSCode >>> print('Spatial Reference {}'.format(SR.name)) Spatial Reference NAD_1983_CSRS_MTM_9 >>>
Dan,
I know I can get a SR object from a describe of a feature.
The point here is to set a SR from a script tool parameter.
It seems the way to do it is as outlined in my first post.
But I am very open to alternative suggestions.
Perhaps someone from esri could advise how this is done, and put something in the help files to illustrate this.
OK, let's make it more visual. I change the code to:
import arcpy crdsys_astext = arcpy.GetParameterAsText(0) crdsys_assobject = arcpy.GetParameter(0) sr_astext = arcpy.GetParameterAsText(1) sr_assobject = arcpy.GetParameter(1) if hasattr(crdsys_astext, "name"): arcpy.AddMessage("crdsys_astext.name : {0}".format(crdsys_astext.name)) if hasattr(crdsys_assobject, "name"): arcpy.AddMessage("crdsys_asobject.name: {0}".format(crdsys_assobject.name)) if hasattr(sr_astext, "name"): arcpy.AddMessage("sr_astext.name : {0}".format(sr_astext.name)) if hasattr(sr_assobject, "name"): arcpy.AddMessage("sr_asobject.name : {0}".format(sr_assobject.name)) arcpy.AddMessage("sr_asobject.factoryCode: {0}".format(sr_assobject.factoryCode))
The result being:
Start Time: Wed Mar 11 08:39:03 2015 Running script Script... sr_asobject.name : RD_New Completed script Script... Succeeded at Wed Mar 11 08:39:03 2015 (Elapsed Time: 0,07 seconds)
So, the only correct and intended way of doing this is using a spatial reference parameter type and reading it as an object. BTW, I'm using 10.2.2 for this demo code. What version are you using?
Xander,
okay, I see what you are doing.
And, yes, if I set the Data Type to Spatial Reference & use GetParameter
There is no need to use arcpy.SpatialReference on the input variable, because it already is a SR object.
Thanks,
using v10.2.2