Learn R Programming

ForestTools (version 0.1.5)

SpatialStatistics: Spatial statistics

Description

Summarization tool for calculating tree counts and statistics within various spatial units.

Usage

SpatialStatistics(trees, areas = NULL, grid = NULL, variables = NULL,
  statFuns = NULL)

Arguments

trees

SpatialPointsDataFrame or SpatialPolygonsDataFrame. The locations of a set of trees, typically detected from a canopy height model using TreeTopFinder. Tree attributes, such as height or crown size, should be stored within this object's @data slot. Tree crowns delineated using SegmentCrowns can also be used.

areas

SpatialPolygonsDataFrame. An optional set of polygons corresponding to areas of interest. Tree counts and statistics will be returned for each area.

grid

RasterLayer (see raster) or numeric. An alternative to the areas argument. Using grid will compute tree counts and statistics within the cells of a spatial grid. Grid size and placement can be defined by inputting a raster object. A single numeric value can also be used, in which case the function will generate a grid with a cell size equal to this value.

variables

character. The names of tree attribute variables (stored in the trees@data slot). In addition to tree counts, the function will compute statistics for each of these variables. Only numeric variables are accepted.

statFuns

list. A named list of custom functions that are used to compute tree attribute statistics. If none are provided, default statistics are mean, median, standard deviation, minimum and maximum. Note that each element of the list should have a name that describes the statistics generated by the function. See below for details on defining custom functions.

Value

Tree count and, if any variables are supplied, tree attribute statistics. If no areas or grid is supplied, the tree count and statistics are computed for the entire trees dataset, and returned as a 'data.frame' object. If areas are defined, an identical SpatialPolygonsDataFrame will be returned, with all computed statistics appended to the object's @data slot. If a grid is defined, tree count will be returned as a RasterLayer, with cell values equal to the number of trees in each cell. If a grid and variables are defined, a RasterBrick (see brick) will be returned instead, with tree count and attribute statistics stored as stacked layers.

Details

Input trees can either be point locations (SpatialPointsDataFrame) or crown outlines (SpatialPolygonsDataFrame). If crown outlines (or other polygons) are inputted, they will be partitioned between spatial units according to their geographic centroids.

In addition to tree counts, statistics for the trees' attributes can also be generated. These attributes should be defined within the @data slot of the input. Only numeric variables are accepted.

By default, the statistics generated for each attribute will be its mean, median, standard deviation, minimum and maximum. However, custom functions can also be used with the statFuns argument. This should be a named list of functions, wherein each list element is given a name to represent the statistic computed by the function.

For example: list(qunt98 = function(x, ...) quantile(x, c(.98), na.rm = TRUE))

Furthermore, custom functions should:

  • Be able to accept numeric vectors.

  • Be able to handle NA values.

  • Have an ellipsis (three dots) in their arguments: function(x, ...)

  • Return a single numeric value.

See Also

TreeTopFinder SegmentCrowns

Examples

Run this code
# NOT RUN {
# Load sample data
library(ForestTools)
library(sp)
data("kootenayTrees", "kootenayBlocks", "kootenayCrowns")

# Get total tree count
SpatialStatistics(kootenayTrees)

# Get total tree count, tree height and crown area statistics
SpatialStatistics(kootenayCrowns, variables = c("height", "crownArea"))

# Get tree count, height statistics for specific areas of interest
areaStats <- SpatialStatistics(kootenayTrees, areas = kootenayBlocks, variables = "height")

# Plot according to tree count
plot(areaStats, col = heat.colors(3)[order(areaStats$TreeCount)])

# Get tree count and height statistics for a 20 x 20 m spatial grid
gridStats <- SpatialStatistics(kootenayTrees, grid = 20, variables = "height")

# Plot gridded tree count and statistics
plot(gridStats$TreeCount)
plot(gridStats$heightMax)

# }

Run the code above in your browser using DataLab