Hydrology toolbox
Institute for Environmental and Spatial Analysis...University of North Georgia
Contents
1 Simple toolbox first
Let’s automate the following hydrology analyses:
- Flow accumulation
- Stream network delineation
- Watershed delineation
Download sample data: nc_spm_08_grass7_exercise.zip
2 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.
3 Data types for today’s exercise
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 |
4 Syntax for geoprocessing tools
We will combine built-in geoprocessing tools, but how do we know how to invoke those tools programmatically?
Fill
FlowDirection
FlowAccumulation
- Raster Calculator’s
Con
RasterToPolyline
Watershed
RasterToPolygon
5 Flow accumulation
def getParameterInfo(self):
elev = arcpy.Parameter(
datatype="GPRasterLayer",
fill = arcpy.Parameter(
datatype="DERasterDataset",
fdir = arcpy.Parameter(
datatype="DERasterDataset",
facc = arcpy.Parameter(
datatype="DERasterDataset",
def execute(self, parameters, messages):
outFill = arcpy.sa.Fill(elev)
outFill.save(fill)
outFlowDirection = arcpy.sa.FlowDirection(fill)
outFlowDirection.save(fdir)
outFlowAccumulation = arcpy.sa.FlowAccumulation(fdir)
outFlowAccumulation.save(facc)
6 Stream network delineation
def getParameterInfo(self):
facc = arcpy.Parameter(
datatype="GPRasterLayer",
thresh = arcpy.Parameter(
datatype="GPLong",
streamRast = arcpy.Parameter(
datatype="DERasterDataset",
streamVect = arcpy.Parameter(
datatype="DEFeatureClass",
def execute(self, parameters, messages):
outStreamRast = arcpy.sa.Con(arcpy.Raster(facc) >= thresh, 1, 0)
outStreamRast.save(streamRast)
arcpy.RasterToPolyline_conversion(streamRast, streamVect)
prj = arcpy.mp.ArcGISProject("CURRENT")
m = prj.activeMap
m.addDataFromPath(streamVect)
7 Watershed delineation
def getParameterInfo(self):
fdir = arcpy.Parameter(
datatype="GPRasterLayer",
outlets = arcpy.Parameter(
datatype="GPFeatureRecordSetLayer",
watershedRast = arcpy.Parameter(
datatype="DERasterDataset",
watershedVect = arcpy.Parameter(
datatype="DEFeatureClass",
def execute(self, parameters, messages):
arcpy.env.extent = "MAXOF"
outletsOidFieldName = arcpy.Describe(outlets).OIDFieldName
outWatershed = arcpy.sa.Watershed(fdir, outlets, outletsOidFieldName)
outWatershed.save(watershedRast)
arcpy.RasterToPolygon_conversion(watershedRast, watershedVect)
prj = arcpy.mp.ArcGISProject("CURRENT")
m = prj.activeMap
m.addDataFromPath(watershedVect)
8 Homework: Longest flow path
Develop a tool that calculates the longest flow path in raster and add it to the Hydrology toolbox.
Hints:
- Run the Flow Length tool with the Upstream direction
- Run the Flow Length tool with the Downstream direction
- Add outputs from steps 1 and 2