ArcGIS Pro Python toolbox 2
Institute for Environmental and Spatial Analysis...University of North Georgia
Contents
1 How to define parameters
Use arcpy.Parameter
and data types.
All parameters in arcpy.Parameter
have a default value, but it doesn’t really make sense to omit the first five parameters*.
arcpy.Parameter(
name=, #*Parameter name with no spaces (?)
displayName=, #*Parameter label for the user interface
direction= #*Input, Output
datatype=, #*Data type
parameterType=, #*Required, Optional, Derived (always output with no user value; use SetParameterAsText to define its output)
enabled=, # False to make it unavailable (optional, None by default)
category=, # Parameter category (optional, None by default)
symbology=, # Path to a layer file for drawing the output (optional, None by default)
multiValue=, # True if the parameter has multiple values (optional, None by default)
)
Oops! That trailing comma after multiValue=
? Don’t worry. Python can handle that for you.
2 Common data types
There are a lot of different data types, but for now, let’s focus on text, numbers, raster and feature layers.
Data type | datatype |
---|---|
Text | GPString |
Integer | GPLong |
Floating-point number | GPDouble |
Input raster layer | GPRasterLayer |
Input feature layer | GPFeatureLayer |
Interactive input feature set | GPFeatureRecordSetLayer |
Output raster dataset | DERasterDataset |
Output feature class | DEFeatureClass |
3 Fill
def getParameterInfo(self):
elev = arcpy.Parameter(
datatype="GPRasterLayer",
fill = arcpy.Parameter(
datatype="DERasterDataset",
def execute(self, parameters, messages):
out_fill = arcpy.sa.Fill(elev)
out_fill.save(fill)
4 Flow direction
def getParameterInfo(self):
fill = arcpy.Parameter(
datatype="GPRasterLayer",
fdir = arcpy.Parameter(
datatype="DERasterDataset",
def execute(self, parameters, messages):
out_fdir = arcpy.sa.FlowDirection(fill)
out_fdir.save(fdir)
5 Flow accumulation
def getParameterInfo(self):
fdir = arcpy.Parameter(
datatype="GPRasterLayer",
facc = arcpy.Parameter(
datatype="DERasterDataset",
def execute(self, parameters, messages):
out_facc = arcpy.sa.FlowAccumulation(fdir)
out_facc.save(facc)
6 Homework: Batch flow accumulation tool
Create a Python toolbox called FirstLastname_Hydrology.pyt
with only one tool. This batch flow accumulation tool should take the following parameters:
- elev: Input raster
- fill: Output raster
- fdir: Output raster
- facc: Output raster
and executes the Fill, Flow Direction, and Flow Accumulation tools. The tool name should be BatchFlowAccumulation
. Submit your FirstLastname_Hydrology.pyt
only.