Working with tool parameters
Almost all tools have parameters. The parameter values will be set by the end user with the Tool dialog box, or they will be hardcoded within the script. When the tool is executed, the parameter values are sent to your tool's source code. Your tool reads these values and proceeds with its work. You use the getParameterInfo()
method to define the parameters for your tool. Individual parameter objects are created as part of this process. If necessary, open InsertWildfires.pyt
in your code editor and find the getParameterInfo()
function in the USGSDownload
class. Add the following parameters, and then we'll discuss what the code is doing:
Each parameter is created using arcpy.Parameter
and is passed a number of arguments that define the object. For the first Parameter object (param0
), we are going to capture a URL to an ArcGIS Server map service containing real-time wildfire data. We give it a display name ArcGIS Server Wildfire URL, which will be displayed on the dialog box for the tool. A name for the parameter, a datatype, a parameter type, and a direction. In the case of the first parameter (param0
), we also assign an initial value, which is the URL to an existing map service containing the wildfire data. For the second parameter, we're going to define an output feature class where the wildfire data that is read from the map service will be written. An empty feature class to store the data has already been created for you.
Next we'll add both the parameters to a Python list called params
and return
, to the list to the calling function. Add the following code:
The main work of a tool is done inside the execute()
method. This is where the geoprocessing of your tool takes place. The execute()
method, as shown in the following code snippet, can accept a number of arguments, including the tools self
, parameters
, and messages
:
To access the parameter values that are passed into the tool, you can use the valueAsText()
method. The following steps will guide you through how to add and execute the execute()
method:
- Find the
execute()
function in the USGSDownload
class and add the following code snippet to access the parameter values that will be passed into your tool. Remember from a previous step that the first parameter will contain a URL to a map service containing the wildfire data and the second parameter will be the output feature class where the data will be written:At this point, you have created a Python Toolbox, added a tool, defined the parameters for the tool, and created variables that will hold the parameter values that the end user has defined. Ultimately, this tool will use the URL that is passed to the tool to connect to an ArcGIS Server map service, download the current wildfire data, create a feature class, and write the wildfire data to the feature class. However, we're going to save the geoprocessing tasks for later. For now, I just want you to print out the values of the parameters that have been entered so that we know that the structure of the tool is working correctly. Print this information using the following code:
- Execute the tool by double-clicking on USGS Download from the InsertWildfires.pyt toolbox that you've created. You should see the following dialog box:
- Leave the default URL parameter as it is and select an output feature class. You'll want to click on the Browse button and then navigate to the
C:\ArcGIS_Blueprint_Python\Data\WildfireData
folder and select the WildlandFires
geodatabase. Inside, there is an empty feature class called CurrentFires
. Select this feature class. - Now click on OK. The progress dialog box will contain the parameter values that you passed in. Your output feature class will probably be different than mine, as shown in the following screenshot:
We'll complete the actual geoprocessing of this tool in the next section.