Skip to content

Commit 38c6710

Browse files
committed
add plot_crowns() initial commit [WIP]
1 parent 63ca8aa commit 38c6710

5 files changed

Lines changed: 75 additions & 1 deletion

File tree

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Suggests:
2727
glue (>= 1.6.0),
2828
spatstat.explore,
2929
spelling,
30-
testthat (>= 3.0.0)
30+
testthat (>= 3.0.0),
31+
vctrs (>= 0.3.0)
3132
Remotes:
3233
firelab/gdalraster
3334
URL: https://ctoney.github.io/FIAstemmap/, https://github.com/ctoney/FIAstemmap

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export(create_fia_owin)
77
export(create_fia_ppp)
88
export(load_tree_data)
99
export(overlay_crowns)
10+
export(plot_crowns)
1011
export(predict_crwidth)

R/overlay_crowns.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#' trees <- within(plantation, CRWIDTH <- predict_crwidth(plantation))
2929
#' trees[trees$SUBP == 1 & trees$DIA >= 5, ] |>
3030
#' overlay_crowns(sample_radius = 24)
31+
#'
32+
#' plot_crowns(trees, subplot = 1, main = "plantation subplot 1")
3133
#' @export
3234
overlay_crowns <- function(tree_list, sample_radius, digits = 1) {
3335
if (missing(tree_list) || is.null(tree_list))

R/plot_crowns.R

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#' @export
2+
plot_crowns <- function(tree_list, subplot = NULL, microplot = FALSE,
3+
linear_unit = "ft", main = "") {
4+
5+
if (is.null(subplot) || is.na(subplot))
6+
subplot <- FALSE
7+
8+
if (subplot && microplot) {
9+
trees_in <- tree_list[tree_list$STATUSCD == 1 &
10+
tree_list$SUBP == subplot &
11+
tree_list$DIA < 5, ]
12+
} else if (subplot && !microplot) {
13+
trees_in <- tree_list[tree_list$STATUSCD == 1 &
14+
tree_list$SUBP == subplot &
15+
tree_list$DIA >= 5, ]
16+
} else {
17+
trees_in <- tree_list[tree_list$STATUSCD == 1 & tree_list$DIA >= 5, ]
18+
}
19+
20+
trees_in$height <- pmin(trees_in$HT, trees_in$ACTUALHT, na.rm = TRUE)
21+
trees_in$dia_ft <- 0.0833333 * trees_in$DIA
22+
trees_in <- trees_in[order(trees_in$height), ]
23+
24+
if (subplot) {
25+
pts <- vector(mode = "list", length = 2)
26+
names(pts) <- c("x", "y")
27+
pts$x <- tree_list$DIST * sin(tree_list$AZIMUTH * (pi / 180))
28+
pts$y <- tree_list$DIST * cos(tree_list$AZIMUTH * (pi / 180))
29+
} else {
30+
pts <- .get_tree_list_xy(trees_in)
31+
}
32+
33+
crowns <- lapply(seq_len(nrow(trees_in)), \(i) {
34+
gdalraster::g_create("POINT", c(pts$x[i], pts$y[i])) |>
35+
gdalraster::g_buffer(trees_in$CRWIDTH[i] / 2)
36+
})
37+
38+
rct <- as.list(gdalraster::g_build_collection(crowns) |>
39+
gdalraster::g_envelope())
40+
names(rct) <- c("xmin", "xmax", "ymin", "ymax")
41+
42+
if (subplot) {
43+
fia_poly <- gdalraster::g_buffer("POINT (0 0)", 24)
44+
stems <- lapply(seq_len(nrow(trees_in)), \(i) {
45+
gdalraster::g_create("POINT", c(pts$x[i], pts$y[i])) |>
46+
gdalraster::g_buffer(trees_in$dia_ft[i] / 2)
47+
})
48+
} else {
49+
fia_poly <- .get_fia_plot_geom(linear_unit = linear_unit)
50+
}
51+
52+
xlab <- paste0("x (", linear_unit, ")")
53+
ylab <- paste0("y (", linear_unit, ")")
54+
gdalraster::plot_geom(fia_poly, xlab, ylab, main, border = "gray62",
55+
lwd = 3, bbox = rct)
56+
57+
for (i in seq_len(nrow(trees_in))) {
58+
# alternate green: #40c945
59+
gdalraster::plot_geom(crowns[[i]], col = "#328e13", border = NA,
60+
add = TRUE)
61+
if (subplot) {
62+
gdalraster::plot_geom(stems[[i]], col = "#b85e00", border = NA,
63+
add = TRUE)
64+
}
65+
}
66+
67+
invisible(NULL)
68+
}

man/overlay_crowns.Rd

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

0 commit comments

Comments
 (0)