|
| 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 | +} |
0 commit comments