How I Build Geoprocessing Tools: #11 Script Parameters
Perhaps like many of you I dabble in home improvements to save money, and to ruin every weekend for the rest of my life! In December I set a moratorium on projects to be sure to enjoy the Christmas season. Well the doldrums of January are upon us and its back to the honey-do list.
Back in November I installed a three-way light switch to control the lights at the top and bottom of my stairs. Unfortunately, I’ve wired the light three different ways and they’re all wrong. The first way only turned the lights on and off from the top of the stairs, the second way only from the bottom, and its current wired position neither of them are working! (Its been that way all of December.) Getting the information from the switch interface to the light seems a simple task, and it should be, however in this case syntax and wiring are critical or everything stays dark.
The geoprocessing has typed variables. The geoprocessing framework validates its variables according to these types. The Geoprocessing framework creates a dialog box interface on-the-fly according to each variable’s type. When I defined my geoprocessing script tool variables I gave them an order and a type. I use the knowledge of the order and type to build my script code. In this post I will simply explain how I get the information the user enters into the script tool interface, into the script’s python code.
Python is just one of the scripting environments that support the building of geoprocessing tools. As discussed in earlier posts all of ArcGIS geoprocessing is really accessed through the geoprocessor object (GP). One arguable exception to this is how the data is passed to the script from the geoprocessing framework. I use a different programming object to get the parameters into my script. In one of the first lines of my code I asked python to load code to use a programming object called SYS. As far as I can tell SYS means “system”, which I figure is my operating system. I am sure SYS can do a lot of cool things, but right now all I care about is that it gives me access to the variables entered by the user of my script tool. ARGV is a property of the SYS object. It is an array or collection of things, specifically the variables in my scrip tool. Below is a snippet of how I access these values with the knowledge of the variable’s order and meaning in my script.
SYS is a built in module that comes with python. The argv property is a list of the parameters associated with a particular script. sys.argv is always the name of the script and is kind of an "invisible" parameter to users of ArcGIS (although it often comes in handy). Therefore, the SYS.argv list always has a length of at least one. When a script is run from ArcGIS, the application passes parameters to python. In the SYS object, these parameters will start from the  index position in the argv list but the list will have a length of 2 because it has that first "self path" parameter. Thus a python script set up to run in ArcGIS where it will have one parameter will have a SYS.argv of length two.
Worth noting is how you might access optional variables based on the number of arguments. Some recomend when dealing with optional variables to put them at the end of the list of variables. However, for style I like to put the output last and other optional variables in the middle. Either way you’ll need a strategy for reading the parameter list you are given from the geoprocessing framework as delivered by the SYS object.
# Get the Input Arguments
Variable1 = sys.argv
if len(sys.argv) > 2:
Variable2 = sys.argv
Variable3 = sys.argv
Variable2 = "#"
Variable3 = sys.argv
You might assume if you have one optional variable in the middle and that if you have two variables the first is the input the second it the output. Or, if you have three variables that the first is the input the second is the optional variable and the third is the output, etc… Or, you could order the variable with the optional parameters last and just assign the variables in order and if there is an error you’ve run out of parameters and you assign the rest to empty values.
To get variables I use the SYS object. To pass the variables back I use the GP object. I talk back to the geoprocessing framework to populate my geoprocessing tool’s output using the geoprocessor object’s SetParameterAsText method.
# Set the Return Output Value
Note: the parameter index 2, is Zero-based like the SYS.argv except the first parameter in SYS.argv is the name of the calling scirpt tool, so the first variable in SYS.args it one not zero. Therefore the GP.SetParameterAsText variable index is one less than the index for the SYS.argv.