Set default Spatial Reference paramter value

2879
14
07-31-2012 02:30 PM
GlennKammerer
New Contributor II
ArcView 10.0 SP1
Windows XP Pro SP3

I have a standalone script in which one of the parameters is a Spatial Reference. I have figured out how to set default values for other data types in the script properties window, but I can't figure out how to do it for a Spatial Reference. Do I have to do it within the script? Any help or suggestions would be greatly appreciated.
Tags (2)
0 Kudos
14 Replies
KatharinaPalffy-Gelfand
New Contributor
Hi Glenn,
it is a little unclear about what you need to do here.
Are you trying to get a spatial reference that is not hard-coded in a script?
If so, you can get the spatial reference from your input data in the script and then carry it over to the other data you create.
This would look like:
sr = arcpy.Describe(infc).spatialReference # with infc being your input data
Kat
0 Kudos
GlennKammerer
New Contributor II
Hi Glenn,
it is a little unclear about what you need to do here.
Are you trying to get a spatial reference that is not hard-coded in a script?
If so, you can get the spatial reference from your input data in the script and then carry it over to the other data you create.
This would look like:
sr = arcpy.Describe(infc).spatialReference # with infc being your input data
Kat


Kat,

My apologies. I will be more verbose.

I have a standalone script that I launch/run from a custom toolbox in my project folder. I run it by double-clicking on the script icon in the catalog window from within ArcMap. When my script runs, a window pops up that prompts the user enter or select some inputs/parameters. One of the choices the user must make is to select a Spatial Reference. I would like the script window to open up with a Spatial Reference already entered for that parameter. The user can either go with that entry, or they can select a different one. I am trying to figure out how to make my script window open up with a Spatial Reference already selected for the appropriate parameter. I have attached a screenshot of my script window so that you can see how many of the parameters are already set. I have tried specifying a default Spatial reference parameter in the same way as I did for my other parameters (by going to the script properties window), but each time I copy in the name of the spatial reference for the parameter's default property, it disappears when I click "Apply" or "Ok".

Screenshot of script window:
[ATTACH=CONFIG]16545[/ATTACH]
0 Kudos
KatharinaPalffy-Gelfand
New Contributor
Hi Glenn,
sorry I did not understand what you meant. This is strange, because it works fine for me.
You could hard code the projection in the script itself, if it's the same every time you run the script.
Just assign a variable for projection and point to the projection file on your hard drive, instead of using getParameterAsText.
Another way of setting a default in toolbox is this: Define the projection parameter as a string and add your default in the value table field. Not very user friendly, but it might work better for you.
Thanks,
Kat
0 Kudos
GlennKammerer
New Contributor II
Bump.

Sorry for the lengthy delay in getting back to this.

Can anybody else offer a way to set a default value for a paramter that is a spatial reference?

To summarize, I am creating some feature classes and would like to provide a default value for the spatial reference parameter. The reason is that it is several clicks to get to the one you want, and as this script is project specific, the spatial reference isn't going to change, thus saving time entering parameters. If I provide the user the option to select something different then the script is a little more flexible and can be used in other projects.

Should I do it through the Tool Validation Class? If so, how?

Thoroughly confused here.
0 Kudos
RaphaelR
Occasional Contributor II
I tried this with toolValidator but i can´t get it to work either.

def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
        
    prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],
                       "Coordinate Systems/Geographic Coordinate Systems/North America/NAD 1983.prj")
    spatialRef = arcpy.SpatialReference(prjFile)
    self.params[1].value = spatialRef.name    # parameter 1 is a string, just to test 
    self.params[2].value = spatialRef            # parameter 2 is a SpatialReference
    return


When i run the tool, the name of the spatialreference will show up in parameter 1, parameter 2 will be empty though (and the tool won´t run correctly).
If i change params[2].value to spatialRef.name, the validator will produce an error(it´ll only take the spatialRef object).
0 Kudos
GlennKammerer
New Contributor II
I figured it out.

You can set a default spatial reference for a spatial reference parameter two different ways on the parameters tab of the script properties window.

At wits end, I tried copying in the system path to the prj I wanted as the default value on the parameters tab. When I hit enter it replaced the path value with the actual contents of the prj file. And when you run the script it displays the name of the coordinate system in the parameter value box.

Either value works as well if you're hard coding a spatial reference in a script.

I'm fairly certain none of this is covered in any documentation I could find. Good to know now, though!

Thanks for help, guys!
GraemeBrowning
Occasional Contributor III

I didn't need to go to the trouble of finding the path to a *.prj.

On the tool properties I entered "GDA 1994 MGA Zone 55" (without the double quotes) in as the Default which replaced it with the very long text equivalent.  Then when I opened the tool the spatial reference showed up as GDA_1994_MGA_Zone_55.

0 Kudos
RaphaelR
Occasional Contributor II
good to know, thanks!
0 Kudos
NobbirAhmed
Esri Regular Contributor
See the screenshot below - when you select the parameter name you'll see a list of parameter properties below. Click next to Default and type the name of the coordinate system:

[ATTACH=CONFIG]19158[/ATTACH]
0 Kudos