Canopy analysis in R using Forest Tools

Andrew Plowright

2020-05-06

Introduction

The Forest Tools R package offers functions to analyze remotely sensed forest data. Currently, tools to detect dominant treetops and outline tree crowns have been implemented, both of which are applied to a rasterized canopy height model (CHM), which is generally derived from LiDAR or photogrammetric point clouds. A function to summarize the height and count of trees within user-defined geographical areas is also available.

Loading sample data

A sample canopy height model (CHM) is included in the Forest Tools package. It represents a small 1.5 hectare swath of forest in the Kootenay Mountains, British Columbia. The following examples use this sample, but if you would rather use your own data, it can be loaded into R using the raster function. A brief section on reading and writing geospatial data in R is included in this document. Otherwise, begin by loading the necessary libraries and the sample CHM using the library and data functions respectively.

Detecting treetops

Dominant treetops can be detected using vwf. This function implements the variable window filter algorithm developped by Popescu and Wynne (2004). In short, a moving window scans the CHM, and if a given cell is found to be the highest within the window, it is tagged as a treetop. The size of the window itself changes depending on the height of the cell on which it is centered. This is to compensate for varying crown sizes, with tall trees having wide crowns and vice versa.

Therefore, the first step is to define the function that will define the dynamic window size. Essentially, this function should take a CHM cell value (i.e.: the height of the canopy above ground at that location) and return the radius of the search window. Here, we will define a simple linear equation, but any function with a single input and output will work.

We do not wish for the vwf to tag low-lying underbrush or other spurious treetops, and so we also set a minimum height of 2 m using the minHeight argument. Any cell with a lower value will not be tagged as a treetop.

The ttops object created by vwf in this example contains the spatial coordinates of each detected treetop, as well as two default attributes: height and winRadius. These correspond to the tree’s height above ground and the radius of the moving window where the tree was located. Note that winRadiusis not necessarily equivalent to the tree’s crown radius.

Outlining tree crowns

Canopy height models often represent continuous, dense forests, where tree crowns abut against eachother. Outlining discrete crown shapes from this type of forest is often refered to as canopy segmentation, where each crown outline is represented by a segment. Once a set of treetops have been detected from a canopy height model, the mcws function can be used for this purpose.

The mcws function implements the watershed algorithm from the imager library. Watershed algorithms are frequently used in topograhical analysis to outline drainage basins. Given the morphological similarity between an inverted canopy and a terrain model, this same process can be used to outline tree crowns. However, a potential problem is the issue of oversegmentation, whereby branches, bumps and other spurious treetops are given their own segments. This source of error can be mitigated by using a variant of the algorithm known as marker-controlled segmentation (Beucher & Meyer, 1993), whereby the watershed algorithm is constrained by a set of markers–in this case, treetops.

The mcws function also takes a minHeight argument, although this value should be lower than that which was assigned to vwf. For the latter, minHeight defines the lowest expected treetop, whereas for the former it should correspond to the height above ground of the fringes of the lowest trees.

By default, mcws returns a raster, where each crown is given a unique cell value. Depending on the intended purpose of the crown map, it may be preferable to store these outlines as polygons. Setting the format argument to “polygons” will convert the rasterized crown map to a set of polygons (a SpatialPolygonsDataFrame). As an added benefit, these polygons will inherit the attributes of the treetops from which they were generated, such as height. Furthermore, an extra attribute, crownArea, will be calculated for each polygon.

It should be noted, however, that producing crown outlines as polygons requires significantly more processing time and disk space.

Spatial statistics

Managed forests are often divided into discrete spatial units. In British Columbia, for instance, these can range from cut blocks measuring a few hectares to timber supply areas, spanning several hundred square kilometers. The forest composition within these spatial units can be characterized through summarized statistics of tree attributes. For instance, a timber license holder may want a rough estimate of the number of dominant trees within a woodlot, while the standard deviation of tree height is of interest to anyone mapping heterogeneous old growth forest.

The sp_summarise function can be used to count trees within a set of spatial units, as well as compute statistics of the trees’ attributes. These spatial units can be in the form of spatial polygons, or can be generated in the form of a raster grid.

When no specific area is defined, sp_summarise will simply return the count of all inputted trees.

Tree crowns can also be used as input. By defining the variables argument, sp_summarise will generate summarized statistics of the trees’ attributes. By default, the mean, median, standard deviation, minimum and maximum are computed, but custom functions can also be used.

Statistics by polygon

The Forest Tools package includes the boundaries of three cutting blocks that can be overlayed on kootenayCHM. Tree counts and height statistics can be summarized within these boundaries using the areas argument.

Statistics by grid

Instead of defining polygonal areas, the sp_summarise function can also generate counts and stastics in raster format. In this case, the grid argument should be used instead of areas. If you have an existing raster with the extent, cell size and alignment that you would like to use, it can be input as the grid argument. Otherwise, simply entering a numeric value will generate a raster with that cell size.

Using custom functions

By default, the statistics generated by sp_summarise for each attribute will be its mean, median, standard deviation, minimum and maximum. However, by using the statFuns argument, custom functions can be used instead.

Any custom function should observe the following conditions:

It should accept numeric vectors.

It should handle NA values.

It should have a single argument followed by an ellipsis (three dots). i.e.: function(x, ...)

It should return a single numeric value.

For instance, the following function will calculate the 98th quantile of a numeric vector.

Reading and writing geospatial data in R

The raster and sp libraries

The Forest Tools package is built on the raster and sp libraries, which are automatically installed when ForestTools is downloaded. These libraries define a variety of classes and functions for working with raster and vector datasets in R.

It is recommended that any user performing geospatial analyses in R be familiar with both of these libraries.

Raster files

To load a raster file, such as a CHM, use the raster function from the raster library (both the function and the library have the same name). Simply provide a path to a valid raster file. Don’t forget to use either double backslashes \\ or forward slashes / in the file path.

Polygon and point files

There are many options for saving point and polygon files to disk. The rgdal library provides functions for reading and writing the most common vector formats. The following examples use ESRI Shapefiles.

Use the readOGR function to load a polygonal ESRI Shapefile. Instead of providing an entire file path, readOGR takes two separate arguments: the file’s directory, followed by the file name without an extension. The following would import a file named “C:\myFiles\blockBoundaries\block375.shp”.