Skip to content

Commit 9b2870f

Browse files
committed
crown_overlay_pct(): add digits argument and example code
1 parent af0d78c commit 9b2870f

2 files changed

Lines changed: 47 additions & 15 deletions

File tree

R/crown_overlay_pct.R

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,56 @@
11
#' Compute fractional tree canopy cover of a subplot/microplot by crown overlay
22
#'
3-
#' @param plot_radius A numeric value giving the radius of the
3+
#' @param sample_radius A numeric value giving the radius of the
44
#' subplot/microplot.
55
#' @param tree_list A data frame containing tree records for the
66
#' subplot/microplot. Must have columns `DIST` (stem distance from subplot
7-
#' center in same units as `plot_radius`), `AZIMUTH` (horizontal angle from
7+
#' center in same units as `sample_radius`), `AZIMUTH` (horizontal angle from
88
#' subplot/microplot center to the stem location, in the range `0:359`) and
9-
#' `CRWIDTH` (tree crown width in the same units as `plot_radius` and `DIST`).
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()`).
1012
#' @return
11-
#' A numeric value for tree canopy cover as percent of the subplot/microplot
13+
#' An numeric value for tree canopy cover as percent of the subplot/microplot
1214
#' covered by a vertical projection of circular crowns.
1315
#'
16+
#' @examples
17+
#' crown_overlay_pct(24, plantation[plantation$SUBP == 1 &
18+
#' plantation$DIA >= 5, ])
1419
#' @export
15-
crown_overlay_pct <- function(plot_radius, tree_list) {
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+
27+
if (missing(tree_list) || is.null(tree_list))
28+
stop("'tree_list' is required", call. = FALSE)
29+
30+
if (!is.data.frame(tree_list))
31+
stop("'tree_list' must be a data frame", call. = FALSE)
32+
33+
if (any(tree_list$AZIMUTH < 0) || any(tree_list$AZIMUTH > 360))
34+
stop("'tree_list$AZIMUTH' contains values out of range", call. = FALSE)
35+
36+
if (is.null(digits))
37+
digits <- 1
38+
1639
x <- tree_list$DIST * sin(tree_list$AZIMUTH * (pi / 180))
1740
y <- tree_list$DIST * cos(tree_list$AZIMUTH * (pi / 180))
1841

1942
crowns <- lapply(seq_len(nrow(tree_list)), \(i) {
2043
gdalraster::g_create("POINT", c(x[i], y[i])) |>
21-
gdalraster::g_buffer(tree_list$CRWIDTH[i] / 2)
44+
gdalraster::g_buffer(tree_list$CRWIDTH[i] / 2)
2245
})
2346

2447
crowns_poly <- gdalraster::g_build_collection(crowns) |>
25-
gdalraster::g_unary_union()
48+
gdalraster::g_unary_union()
2649

27-
plot_poly <- gdalraster::g_buffer("POINT (0 0)", plot_radius, 90L)
50+
plot_poly <- gdalraster::g_buffer("POINT (0 0)", sample_radius, 90L)
2851

29-
gdalraster::g_intersection(plot_poly, crowns_poly) |>
30-
gdalraster::g_area() / gdalraster::g_area(plot_poly) * 100
52+
tcc <- gdalraster::g_intersection(plot_poly, crowns_poly) |>
53+
gdalraster::g_area() / gdalraster::g_area(plot_poly) * 100
54+
55+
round(tcc, digits)
3156
}

man/crown_overlay_pct.Rd

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

0 commit comments

Comments
 (0)