Tools are added to a .pyt file as classes. Each tool class should include at a minimum an __init__ and execute method. Optionally, getParameterInfo, isLicensed, updateParameters, and updateMessages methods can be used to add additional control to the behavior of the tool.
Methods used to define a working tool class
Tool method | Required/Optional | Description |
---|---|---|
__init__ | Required | Initializes the tool class. |
Optional | Defines the tool's parameters. | |
Optional | Returns whether the tool is licensed to execute. | |
Optional | Called each time the user changes a parameter on the tool dialog box. After returning from updateParameters, geoprocessing calls its internal validation routine. | |
Optional | Called after returning from the internal validation routine. You can examine the messages created from internal validation and change them if desired. | |
Required | The tool's source code. |
Setting a tool class __init__ method
The __init__ method in a tool class is a standard Python class initialization method. For a tool in a Python toolbox, the __init__ method is used to set properties of the tool, including the tool's label and description. The tool's name is established by the name of the class itself (in the example below, the tool name is CalculateSinuosity).
The __init__ method establishes tool properties such as the label and description. Below, a tool named CalculateSinuosity is created.
class CalculateSinuosity(object):
def __init__(self):
self.label = "Calculate Sinuosity"
self.description = "Sinuosity measures the amount that a river meanders within its valley, " + \
"calculated by dividing total stream length by valley length."
注意:
Python syntax rules will enforce most geoprocessing conventions when naming a Python toolbox tool class. However, underscores in a tool class name should also be avoided, as they will cause errors when running tools from ArcGIS Server.
The following properties can be set in a tool's __init__ method.
Property | Description |
---|---|
canRunInBackground | 注意:The canRunInBackground property is used only for ArcGIS Desktop; it has no effect in ArcGIS Pro. |
category | The name of the toolset in which the tool is located. A toolset is a way to organize tools within a toolbox. |
description | The description for the tool. |
label | The label is the display name for the tool as shown in the Geoprocessing pane. |
stylesheet | To change the default style sheet used for the tool. If not set, the default style sheet is used. |