whitebox Demo


whitebox is an R frontend for the ‘WhiteboxTools’ library, which is an advanced geospatial data analysis platform developed by Prof. John Lindsay at the University of Guelph’s Geomorphometry and Hydrogeomatics Research Group.

‘WhiteboxTools’ can be used to perform common geographical information systems (GIS) analysis operations, such as cost-distance analysis, distance buffering, and raster reclassification. Remote sensing and image processing tasks include image enhancement (e.g. panchromatic sharpening, contrast adjustments), image mosaicing, numerous filtering operations, simple classification (k-means), and common image transformations. ‘WhiteboxTools’ also contains advanced tooling for spatial hydrological analysis (e.g. flow-accumulation, watershed delineation, stream network analysis, sink removal), terrain analysis (e.g. common terrain indices such as slope, curvatures, wetness index, hillshading; hypsometric analysis; multi-scale topographic position analysis), and LiDAR data processing.

WhiteboxTools is not a cartographic or spatial data visualization package; instead it is meant to serve as an analytical backend for other data visualization software, mainly GIS.

This demonstration shows how to use the whitebox package to integrate WhiteboxTools with R.

Suggested citation: Lindsay, J. B. (2016). Whitebox GAT: A case study in geomorphometric analysis. Computers & Geosciences, 95, 75-84. doi: http://dx.doi.org/10.1016/j.cageo.2016.07.003


Load the whitebox library.


How whitebox works

The current implementation of whitebox generates system() calls to a local WhiteboxTools binary: usually whitebox_tools or whitebox_tools.exe

You can find the binary path that the package is currently going to use with wbt_exe_path()

wbt_exe_path(shell_quote = FALSE)
#> [1] "/home/andrew/workspace/whitebox-tools/target/release/whitebox_tools"

Interfacing with R spatial packages

The results of runs of WhiteboxTools via the whitebox package are specified as file paths for output files (often rasters in GeoTIFF format, sometimes other formats; other cases shapefiles, HTML output, LiDAR-related files etc.)

Many R users will be interacting with raster data with one of the many available R packages for spatial data. In this vignette we will use the raster package for visualization. Just as easily we could have used terra, stars or other options available in the R ecosystem.

Saving your “output” file paths as a variable so that you can use them later to load the file into an R object is the main way to view your output.


A demonstration workflow involving the popular raster package follows:

#> Loading required package: sp

# DEMO: calculate slope with WhiteboxTools and raster

# Typically the input/output paths are stored as variables

# sample DEM input GeoTIFF
input <- system.file("extdata", "DEM.tif", package = "whitebox")

# output file (to be created)
output <- file.path(tempdir(), "slope.tif")

Run a tool such as wbt_slope() or "Slope".

WhiteboxTools reads from input and writes to output.

wbt_slope(input, output, units = 'radians')
if (file.exists(output)) {
  # create a RasterLayer from file output path
  outputras <- raster::raster(output)

In this case, we can achieve a similar slope map result using raster::terrain(), so we will create and plot a RasterLayer from output and compare the two.

if (file.exists(output)) {
  # par(mfrow = c(2, 1), mar = c(1, 1, 1, 1))
  # inspect the output graphically
    main = "whitebox::wbt_slope() [radians]",
    axes = FALSE, horizontal = TRUE
  # calculate equivalent using raster::terrain() on input
    raster::terrain(raster::raster(input, crs = "EPSG:26918")),
    main = "raster::terrain() [radians]",
    axes = FALSE, horizontal = TRUE

# the RasterLayer retains a reference to the input file name

#> [1] "/tmp/RtmpUbYDO5/slope.tif"

The RasterLayer, SpatRaster and related classes in the raster and terra packages are perfect for maintaining the linkage between file output and an R object with the data in or out of memory.

Use raster::filename() to get the “source” file name. If you are using a terra SpatRaster objects the equivalent method is terra::sources().

WhiteboxTools R setup

Installing WhiteboxTools

If you do not have WhiteboxTools installed in one of the standard locations, and have not set up your package options, the package will not be able to find your WhiteboxTools installation.

Most often you will be able to use whitebox::install_whitebox() to download the latest binaries that correspond to the available version of the R package. However, this is not required.

For general information consult the WhiteboxTools User Manual: https://jblindsay.github.io/wbt_book/install.html

You may download/compile WhiteboxTools yourself and install anywhere for use with the whitebox R package.

For details on building from source see: https://github.com/jblindsay/whitebox-tools

Specify the path to whitebox_tools executable with whitebox.exe_path option.

In whitebox wbt_init() is the standard way to set the “exe_path” for a session.

Package Settings with wbt_init()

wbt_init() is used to set and check the path of the binary executable that commands are passed to.

The executable path and other options are stored as package options, and can be overridden by system environment variables. A default value wbt_exe_path(shell_quote = FALSE) is passed when the exe_path argument is unspecified.

# inspect where wbt_init() will be checking
wbt_exe_path(shell_quote = FALSE)
#> [1] "/home/andrew/workspace/whitebox-tools/target/release/whitebox_tools"

# TRUE when file is found at one of the user specified paths or package default
# FALSE when whitebox_tools does not exist at path

This section will cover optional arguments to wbt_init() (exe_path, wd and verbose) and their corresponding options and helper functions.

exe_path argument

The exe_path argument to wbt_init() sets the whitebox.exe_path package option. exe_path is the path to a WhiteboxTools executable file. The default value is the package installation directory, subdirectory "WBT", followed by whitebox_tools.exe or whitebox_tools depending on your operating system.

# set path manually to whitebox_tools executable, for instance:
wbt_init(exe_path = '/home/andrew/workspace/whitebox-tools/target/release/whitebox_tools')

A way to find your whitebox_tools executable is if it is in a directory that exists in your PATH. This requires that you have the Unix tool which or one of its analogues.

Sys.setenv(R_WHITEBOX_EXE_PATH = Sys.which("whitebox_tools"))

You can also set R_WHITEBOX_EXE_PATH manually.

  • Replace the Sys.which() call with a custom path string as needed such as "C:/path/to/whitebox_tools.exe".

Package options other than exe_path (as detailed in ?whitebox::whitebox and ?wbt_init) can be set with wbt_init(exe_path, ...), where ... is additional named arguments corresponding to the * suffix in whitebox.* package options names. Use wbt_options() or specific methods like wbt_verbose(), wbt_wd() to get all values or set specific values.

wd argument

The wd argument can be used to set the WhiteboxTools working directory.

A working directory specifies a base folder path where WhiteboxTools can find inputs and create outputs. Setting the whitebox.wd package option (via the wd argument to wbt_init() or wbt_wd()) aids the process of setting file paths. If a value is set for the option the --wd directory flag is added for tools that support it.

Before you set the working directory in a session the default output will be in your current R working directory unless directory is specified in your input/output arguments. You can change working directory at any time by setting the wd argument to wbt_wd() and running a tool.

NOTE: once you have set a working directory in a session, the directory needs to be set somewhere new to “replace” the old value; just dropping the flag will not automatically change the working directory back to your R working directory* and your output will show up in whatever folder you set initially.

A helper method for setting the whitebox.wd option is wbt_wd().

*To “unset” the option in the R package you can use wbt_wd("") which is equivalent to wbt_wd(getwd()). The next tool call will change the WhiteboxTools working directory setting to the new path. After this point the flag need not be specified [until you wish to change again].

wbt_wd("") # "" equivalent to getwd()

verbose argument

The verbose argument is used to set the package option related to tool “verbosity”: whitebox.verbose. When whitebox.verbose is FALSE no output will be cat() to the console by running tools.

A helper method for getting and setting the whitebox.verbose option is wbt_verbose(). wbt_verbose() is used throughout the package to check what level of verbosity should be used. By default, the result of wbt_verbose() is the result of interactive() so tools will print extra console output when you are there to see it. This is used in a variety of wbt_* methods to allow the package option to control output for many functions in a consistent manner, hide output in your automated tests, markdown documents, vignettes etc.

In this vignette we use wbt_verbose(TRUE) so the package option whitebox.verbose is set to TRUE

# force output when run non-interactively (knitr)

This is mainly to print out the tool name and elapsed time whenever we run a tool:

#> wbt_breach_depressions - Elapsed Time (excluding I/O): 0.12s

This package-level verbose option can also control the verbose_mode values passed to wbt_* tool functions. Turning on “full” output requires a third option to be set for this argument: "all". Use wbt_verbose("all"). wbt_verbose() will still return TRUE when the whitebox.verbose option is "all".

Long-term Package Option Settings

For long-term package option settings you can put whitebox_tools on your $PATH and set R_WHITEBOX_EXE_PATH, perhaps in your user .Rprofile or ~/.profile.

This command will check your PATH environment variable for whitebox_tools and set the path to value found:

On Windows you can add the path to whitebox_tools.exe as a new entry R_WHITEBOX_EXE_PATH in User or System Environment variable.

On Linux/Mac you can set R_WHITEBOX_EXE_PATH directly with export R_WHITEBOX_EXE_PATH="/path/to/whitebox_tools".

Running tools

Specify input and output paths, and any other options, as specified in package reference:

For instance, here we use the tool “BreachDepressions” to pre-process a Digital Elevation Model (DEM) so we can identify flow pathways.

# sample DEM file path in package extdata folder
input <- system.file("extdata", "DEM.tif", package="whitebox")

# output file name
output <- file.path(tempdir(), "output.tif")

# run breach_depressions tool
wbt_breach_depressions(dem = input, output = output)
#> breach_depressions - Elapsed Time (excluding I/O): 0.8s

For more info see: ?wbt_breach_depressions

These wbt_*_tool_name_*() functions are wrappers around the wbt_run_tool() function that does the system() call given a function-specific argument string.

# sample DEM file path in package extdata folder
input <- system.file("extdata", "DEM.tif", package="whitebox")

# output file name
output <- file.path(tempdir(), "output.tif")

# run breach_depressions tool
wbt_run_tool(tool_name = "BreachDepressions", args = paste0("--dem=", input, " --output=", output))
#> BreachDepressions - Elapsed Time (excluding I/O): 0.8s

The above method of creating wbt_breach_depressions(dem = ..., output = ...) to handle wbt_run_tool("BreachDepressions", args = ...) makes it easy to generate static methods that have parity with the latest WhiteboxTools interface.

Example: Compare input v.s. output with raster

We use the raster R package to read the GeoTIFF outputs from WhiteboxTools.



# sample DEM file path in package extdata folder
input <- system.file("extdata", "DEM.tif", package="whitebox")

# output file name
output <- file.path(tempdir(), "output.tif")

Run wbt_breach_depressions() (BreachDepressions tool)

# run breach_depressions tool
wbt_breach_depressions(dem = input, output = output)
#> breach_depressions - Elapsed Time (excluding I/O): 0.9s

Visualize results with raster

# create raster object from input file
input <- raster(input)

if (file.exists(output)) {
  # create raster object from output file
  output <- raster(output)
  # par(mar = c(2, 1, 2, 1))
  # inspect input v.s. output
  plot(input, axes = FALSE, horizontal = TRUE, box = FALSE, main = "DEM")
  plot(output, axes = FALSE, horizontal = TRUE, box = FALSE, 
       main = "DEM (Breached Depressions)")
  # inspect numeric difference (output - input) 
  plot(output - input, axes = FALSE, horizontal = TRUE, box = FALSE, 
       main = "Difference ([Breached Depressions] - [DEM])")

Example: Identifying Tributaries

Here we will take our processing of DEMs a bit further by performing several WhiteboxTools operations in sequence.

We are interested in identifying and ranking tributaries of watercourses (streams and rivers).

A package that makes use of the whitebox R package is hydroweight.

Here is a brief snippet based on the beginning of the hydroweight README showing how the breached DEM we made above can be used in a spatial hydrological analysis of stream networks.



## Import toy_dem from whitebox package
toy_file <- system.file("extdata", "DEM.tif", package = "whitebox")
toy_dem <- raster(x = toy_file, values = TRUE)
crs(toy_dem) <- "EPSG:26918"

## Generate wd as a temporary directory. 
## Replace with your own path, or "." for current directory
wd <- tempdir()

## Write toy_dem to working directory
  x = toy_dem, filename = file.path(wd, "toy_dem.tif"),
  overwrite = TRUE

wbt_breach_depressions() – Breach DEM Depressions

First we pre-process by breaching depressions in the DEM

## Breach depressions to ensure continuous flow
  dem = file.path(wd, "toy_dem.tif"),
  output = file.path(wd, "toy_dem_breached.tif")
#> breach_depressions - Elapsed Time (excluding I/O): 0.9s

wbt_d8_pointer() – Calculate Flow Direction

Then we generate the direction of flow on the DEM surface using the “D8” flow pointer method

## Generate d8 flow pointer (note: other flow directions are available)
  dem = file.path(wd, "toy_dem_breached.tif"),
  output = file.path(wd, "toy_dem_breached_d8.tif")
#> d8_pointer - Elapsed Time (excluding I/O): 0.1s

wbt_d8_flow_accumulation() – Flow Accumulation

Once we calculate the direction of flow by some method, we calculate cumulative flow

For example with wbt_d8_flow_accumulation():

## Generate d8 flow accumulation in units of cells (note: other flow directions are available)
  input = file.path(wd, "toy_dem_breached.tif"),
  output = file.path(wd, "toy_dem_breached_accum.tif"),
  out_type = "cells"
#> d8_flow_accumulation - Elapsed Time (excluding I/O): 0.3s
Additional Flow Direction and Accumulation Tools

In addition to D8 flow pointers (flow direction), there are several other options for both direction and accumulation such as FD8, D-infinity, and D-infinity.

  • Keyword “Pointer” tools: "D8Pointer", "DInfPointer", "FD8Pointer", "Rho8Pointer"

  • Keyword “FlowAccumulation” tools: "D8FlowAccumulation", "DInfFlowAccumulation", "FD8FlowAccumulation", "Rho8FlowAccumulation", "MDInfFlowAccumulation"

Search for more tools involving "flow pointer" by key word: wbt_list_tools(keyword = "flow pointer")

#> All 22 Tools containing keywords:
#> AverageFlowpathSlope: Measures the average slope gradient from each grid cell to all upslope divide cells.
#> AverageUpslopeFlowpathLength: Measures the average length of all upslope flowpaths draining each grid cell.
#> D8FlowAccumulation: Calculates a D8 flow accumulation raster from an input DEM or flow pointer.
#> D8Pointer: Calculates a D8 flow pointer raster from an input DEM.
#> DInfFlowAccumulation: Calculates a D-infinity flow accumulation raster from an input DEM.
#> DInfPointer: Calculates a D-infinity flow pointer (flow direction) raster from an input DEM.
#> DownslopeFlowpathLength: Calculates the downslope flowpath length from each cell to basin outlet.
#> ExtractStreams: Extracts stream grid cells from a flow accumulation raster.
#> FD8FlowAccumulation: Calculates an FD8 flow accumulation raster from an input DEM.
#> FD8Pointer: Calculates an FD8 flow pointer raster from an input DEM.
#> FindNoFlowCells: Finds grid cells with no downslope neighbours.
#> FindParallelFlow: Finds areas of parallel flow in D8 flow direction rasters.
#> FlowAccumulationFullWorkflow: Resolves all of the depressions in a DEM, outputting a breached DEM, an aspect-aligned non-divergent flow pointer, and a flow accumulation raster.
#> FlowLengthDiff: Calculates the local maximum absolute difference in downslope flowpath length, useful in mapping drainage divides and ridges.
#> LongProfileFromPoints: Plots the longitudinal profiles from flow-paths initiating from a set of vector points.
#> LongestFlowpath: Delineates the longest flowpaths for a group of subbasins or watersheds. 
#> MDInfFlowAccumulation: Calculates an FD8 flow accumulation raster from an input DEM.
#> MaxUpslopeFlowpathLength: Measures the maximum length of all upslope flowpaths draining each grid cell.
#> NumInflowingNeighbours: Computes the number of inflowing neighbours to each cell in an input DEM based on the D8 algorithm.
#> Rho8Pointer: Calculates a stochastic Rho8 flow pointer raster from an input DEM.
#> SnapPourPoints: Moves outlet points used to specify points of interest in a watershedding operation to the cell with the highest flow accumulation in its neighbourhood.
#> TraceDownslopeFlowpaths: Traces downslope flowpaths from one or more target sites (i.e. seed points).

This is just an example of the wealth of tool options made available by the WhiteboxTools platform.

wbt_extract_streams() – Extract Stream Network

With our flow accumulation raster in hand, we can extract a stream network with wbt_extract_streams() based on a threshold (e.g. 100) of accumulated flow. This threshold value you choose will depend on analysis goals, the choice of flow accumulation algorithm used, local topography, as well as resolution and extent of DEM.

## Generate streams with a stream initiation threshold of 100 cells
  flow_accum = file.path(wd, "toy_dem_breached_accum.tif"),
  output = file.path(wd, "toy_dem_streams.tif"),
  threshold = 100
#> extract_streams - Elapsed Time (excluding I/O): 0.0s

wbt_tributary_identifier() – Identify Tributaries

Next, let’s identify tributaries. This function wbt_tributary_identifier() is a little more complicated because it takes takes two inputs:

  • Our raster D8 pointer file.

  • And our raster streams file.

  d8_pntr = file.path(wd, "toy_dem_breached_d8.tif"),
  streams = file.path(wd, "toy_dem_streams.tif"),
  output = file.path(wd, "toy_dem_tributaries.tif")
#> tributary_identifier - Elapsed Time (excluding I/O): 0.0s

Compare results

Finally, we compare results of wbt_extract_streams() with wbt_tributary_identifier()

if (file.exists(file.path(wd, "toy_dem_streams.tif"))) {
  # par(mfrow = c(2, 1), mar = c(3, 1, 2, 1))
    raster(file.path(wd, "toy_dem_streams.tif")),
    main = "Streams",
    col = "black",
    axes = FALSE,
    horizontal = TRUE,
    box = FALSE
    raster(file.path(wd, "toy_dem_tributaries.tif")),
    main = "TributaryIdentifier",
    axes = FALSE,
    horizontal = TRUE,
    box = FALSE

Appendix: wbt_* utility functions

These methods provide access to WhiteboxTools executable parameters and metadata.


wbt_help() prints the WhiteboxTools help: a listing of available commands for executable

#> WhiteboxTools Help
#> The following commands are recognized:
#> --cd, --wd       Changes the working directory; used in conjunction with --run flag.
#> -h, --help       Prints help information.
#> -l, --license    Prints the whitebox-tools license. Tool names may also be used, --license="Slope"
#> --listtools      Lists all available tools. Keywords may also be used, --listtools slope.
#> -r, --run        Runs a tool; used in conjunction with --wd flag; -r="LidarInfo".
#> --toolbox        Prints the toolbox associated with a tool; --toolbox=Slope.
#> --toolhelp       Prints the help associated with a tool; --toolhelp="LidarInfo".
#> --toolparameters Prints the parameters (in json form) for a specific tool; --toolparameters="LidarInfo".
#> -v               Verbose mode. Without this flag, tool outputs will not be printed.
#> --viewcode       Opens the source code of a tool in a web browser; --viewcode="LidarInfo".
#> --version        Prints the version information.
#> Example Usage:
#> >> ./whitebox_tools -r=lidar_info --cd="/path/to/data/" -i=input.las --vlr --geokeys


wbt_license() prints the WhiteboxTools license

#> WhiteboxTools License
#> Copyright 2017-2020 John Lindsay
#> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
#> associated documentation files (the "Software"), to deal in the Software without restriction,
#> including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
#> and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
#> subject to the following conditions:
#> The above copyright notice and this permission notice shall be included in all copies or substantial
#> portions of the Software.


Prints the WhiteboxTools version

#> WhiteboxTools v2.0.0 by Dr. John B. Lindsay (c) 2017-2021
#> WhiteboxTools is an advanced geospatial data analysis platform developed at
#> the University of Guelph's Geomorphometry and Hydrogeomatics Research 
#> Group (GHRG). See www.whiteboxgeo.com for more details.


Use wbt_list_tools() to list all available tools in WhiteboxTools. In v1.5.0 this is 448 tools!


The full list can be an overwhelming amount of output, so you pass the keyword argument to search and filter.

For example we list tools with keyword ‘flowaccumulation’ in tool name or description.

wbt_list_tools(keywords = "flowaccumulation")
#> All 5 Tools containing keywords:
#> D8FlowAccumulation: Calculates a D8 flow accumulation raster from an input DEM or flow pointer.
#> DInfFlowAccumulation: Calculates a D-infinity flow accumulation raster from an input DEM.
#> FD8FlowAccumulation: Calculates an FD8 flow accumulation raster from an input DEM.
#> FlowAccumulationFullWorkflow: Resolves all of the depressions in a DEM, outputting a breached DEM, an aspect-aligned non-divergent flow pointer, and a flow accumulation raster.
#> MDInfFlowAccumulation: Calculates an FD8 flow accumulation raster from an input DEM.


Once we find a tool that we are interested in using, we can investigate what sort of parameters it takes. The R methods generally take the same named parameters.

R functions have the naming scheme wbt_tool_name where _ is used for spaces, whereas the tools themselves have no spaces.

wbt_tool_help("tributaryidentifier") shows the command line help for a tool by name.

#> TributaryIdentifier
#> Description:
#> Assigns a unique identifier to each tributary in a stream network.
#> Toolbox: Stream Network Analysis
#> Parameters:
#> Flag               Description
#> -----------------  -----------
#> --d8_pntr          Input raster D8 pointer file.
#> --streams          Input raster streams file.
#> -o, --output       Output raster file.
#> --esri_pntr        D8 pointer uses the ESRI style scheme.
#> --zero_background  Flag indicating whether a background value of zero should be used.
#> Example usage:
#> >>./whitebox_tools -r=TributaryIdentifier -v --wd="/path/to/data/" --d8_pntr=D8.tif --streams=streams.tif -o=output.tif
#> >>./whitebox_tools -r=TributaryIdentifier -v --wd="/path/to/data/" --d8_pntr=D8.tif --streams=streams.tif -o=output.tif --esri_pntr --zero_background

?wbt_tributary_identifier shows the corresponding R function help, which is derived from the command line help page and other metadata.


Another way that tools are organized in WhiteboxTools is by “toolbox.”

wbt_toolbox() prints the toolbox for a specific tool (or all tools if none specified)

wbt_toolbox(tool_name = "aspect")
#> Geomorphometric Analysis

Print the full list by not specifying tool_name



wbt_tool_parameters() retrieves the tool parameter descriptions for a specific tool as JSON formatted string.

#> {"parameters": [{"name":"Input DEM File","flags":["-i","--dem"],"description":"Input raster DEM file.","parameter_type":{"ExistingFile":"Raster"},"default_value":null,"optional":false},{"name":"Output File","flags":["-o","--output"],"description":"Output raster file.","parameter_type":{"NewFile":"Raster"},"default_value":null,"optional":false},{"name":"Z Conversion Factor","flags":["--zfactor"],"description":"Optional multiplier for when the vertical and horizontal units are not the same.","parameter_type":"Float","default_value":null,"optional":true},{"name":"Units","flags":["--units"],"description":"Units of output raster; options include 'degrees', 'radians', 'percent'","parameter_type":{"OptionList":["degrees","radians","percent"]},"default_value":"degrees","optional":true}]}


WhiteboxTools is written in Rust and is open source. You can view the source code for a specific tool on the source code repository.

#> https://github.com/jblindsay/whitebox-tools/blob/master/whitebox-tools-app/src/tools/hydro_analysis/breach_depressions.rs

Use the argument viewer=TRUE to use browseURL() to open a browser window to the corresponding GitHub page.