Skip to content

Commit bf1fb83

Browse files
committed
crown_overlay_pct(): make tree_list the first argument and add to documentation
1 parent a05831a commit bf1fb83

3 files changed

Lines changed: 58 additions & 36 deletions

File tree

R/crown_overlay_pct.R

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
#' Compute fractional tree canopy cover of a subplot/microplot by crown overlay
22
#'
3-
#' @param sample_radius A numeric value giving the radius of the
3+
#' `crown_overlay_pct()` computes the proportion of a circular polygon covered
4+
#' by a given set of tree crowns modeled as discs and having spatially explicit
5+
#' stem locations. The sampled area is generally an FIA subplot with radius 24
6+
#' ft (7.315 m) for trees with diameter \verb{>= 5 in.} (12.7 cm), or an FIA
7+
#' microplot with radius 6.8 ft (2.073 m) for trees \verb{>= 1 in.} (2.54 cm)
8+
#' but \verb{< 5 in.} (12.7 cm) diameter (denoted as "saplings"). Stem locations
9+
#' are specified as distance and azimuth from subplot/microplot center.
10+
#'
11+
#' @param tree_list A data frame containing tree records for a
12+
#' subplot/microplot. Must have columns `DIST` (stem distance from
13+
#' subplot/microplot center in the same units as `sample_radius`), `AZIMUTH`
14+
#' (horizontal angle from subplot/microplot center to the stem location, in the
15+
#' range `0:359`) and `CRWIDTH` (tree crown width in the same units as
16+
#' `sample_radius` and `DIST`).
17+
#' @param sample_radius A numeric value giving the radius of the circular
418
#' subplot/microplot.
5-
#' @param tree_list A data frame containing tree records for the
6-
#' subplot/microplot. Must have columns `DIST` (stem distance from subplot
7-
#' center in same units as `sample_radius`), `AZIMUTH` (horizontal angle from
8-
#' subplot/microplot center to the stem location, in the range `0:359`) and
9-
#' `CRWIDTH` (tree crown width in the same units as `sample_radius` and `DIST`).
10-
#' @param digits Optional integer number of digits to keep in the result
11-
#' (defaults to `1`, will be passed to `round()`).
19+
#' @param digits Optional integer indicating the number of digits to keep in the
20+
#' return value (defaults to `1`, will be passed to `round()`).
1221
#' @return
13-
#' An numeric value for tree canopy cover as percent of the subplot/microplot
14-
#' covered by a vertical projection of circular crowns.
22+
#' Estimated tree canopy cover as percent of the area specified by
23+
#' `sample_radius` that is covered by a vertical projection of circular
24+
#' crowns.
1525
#'
1626
#' @examples
17-
#' crown_overlay_pct(24, plantation[plantation$SUBP == 1 &
18-
#' plantation$DIA >= 5, ])
27+
#' # subplot 1 of the `plantation` plot
28+
#' subp1_trees <- plantation[plantation$SUBP == 1 &
29+
#' plantation$DIA >= 5, ]
30+
#' crown_overlay_pct(subp1_trees, 24)
1931
#' @export
20-
crown_overlay_pct <- function(sample_radius, tree_list, digits = 1) {
21-
if (missing(sample_radius) || is.null(sample_radius))
22-
stop("'sample_radius' is required", call. = FALSE)
23-
24-
if (!(is.numeric(sample_radius) && length(sample_radius) == 1))
25-
stop("'sample_radius' must be a single numeric value", call. = FALSE)
26-
32+
crown_overlay_pct <- function(tree_list, sample_radius, digits = 1) {
2733
if (missing(tree_list) || is.null(tree_list))
2834
stop("'tree_list' is required", call. = FALSE)
2935

@@ -33,6 +39,12 @@ crown_overlay_pct <- function(sample_radius, tree_list, digits = 1) {
3339
if (any(tree_list$AZIMUTH < 0) || any(tree_list$AZIMUTH > 360))
3440
stop("'tree_list$AZIMUTH' contains values out of range", call. = FALSE)
3541

42+
if (missing(sample_radius) || is.null(sample_radius))
43+
stop("'sample_radius' is required", call. = FALSE)
44+
45+
if (!(is.numeric(sample_radius) && length(sample_radius) == 1))
46+
stop("'sample_radius' must be a single numeric value", call. = FALSE)
47+
3648
if (is.null(digits))
3749
digits <- 1
3850

@@ -51,6 +63,6 @@ crown_overlay_pct <- function(sample_radius, tree_list, digits = 1) {
5163

5264
tcc <- gdalraster::g_intersection(plot_poly, crowns_poly) |>
5365
gdalraster::g_area() / gdalraster::g_area(plot_poly) * 100
54-
66+
5567
round(tcc, digits)
5668
}

man/crown_overlay_pct.Rd

Lines changed: 25 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-crown_overlay_pct.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ test_that("crown_overlay_pct works", {
44
matrix(nrow = 4, ncol = 3, byrow = TRUE) |>
55
as.data.frame()
66
colnames(trees) <- c("AZIMUTH", "DIST", "CRWIDTH")
7-
expect_equal(round(crown_overlay_pct(24, trees)), 20)
7+
expect_equal(round(crown_overlay_pct(trees, 24)), 20)
88
})

0 commit comments

Comments
 (0)