|
1 | | -#' Create a 'spatstat' point pattern dataset for an FIA plot tree list |
| 1 | +#' Helper functions for spatial point pattern analysis of FIA plots |
| 2 | +#' |
| 3 | +#' Functions that facilitate analysis of FIA tree data using the \pkg{spatstat} |
| 4 | +#' library. |
| 5 | +#' |
| 6 | +#' @name spatstat_helpers |
| 7 | +#' @details |
| 8 | +#' `create_fia_owin()` returns an object of class `"owin"` from the |
| 9 | +#' \pkg{spatstat} library. This object represents the generic 2-D observation |
| 10 | +#' window for the nationally standard FIA plot design which is a four-point |
| 11 | +#' cluster of subplots. Used when creating a \pkg{spatstat} point pattern object |
| 12 | +#' for an FIA tree list. |
2 | 13 | #' |
3 | 14 | #' `create_fia_ppp()` returns an object of class `"ppp"` representing the point |
4 | 15 | #' pattern of an FIA tree list in the 2-D plane. A point pattern object defines |
5 | 16 | #' the dataset for using a stem-mapped FIA plot with functions of the |
6 | 17 | #' \pkg{spatstat} library. |
7 | 18 | #' |
8 | | -#' @details |
9 | 19 | #' The standard set of columns for tree list data in \pkg{FIAstemmap} is given |
10 | | -#' below, along with their status as required or optional for |
| 20 | +#' below, along with the status of each column as required or optional for |
11 | 21 | #' `create_fia_ppp()`: |
12 | 22 | #' * `PLT_CN`: optional, `create_fia_ppp()` assumes input is for one plot |
13 | 23 | #' * `SUBP`: required subplot number |
|
22 | 32 | #' * `ACTUALHT`: optional tree actual height (accounts for broken top) |
23 | 33 | #' * `CCLCD`: optional crown class code |
24 | 34 | #' * `TPA_UNADJ`: optional tree expansion factor (per acre) |
25 | | -#' * `CRWIDTH`: optional crown width |
| 35 | +#' * `CRWIDTH`: optional crown width (may be computed with **TODO**) |
| 36 | +#' |
| 37 | +#' @param linear_unit An optional character string specifying the linear |
| 38 | +#' distance unit. Defaults to the native FIA unit of `"ft"`, but may be set to |
| 39 | +#' `"m"` instead (or `"meter"` / `"metre"`). |
| 40 | +#' @param macroplot An optional logical value. The default is `FALSE`, which |
| 41 | +#' defines the FIA plot footprint in terms of the standard four-subplot |
| 42 | +#' configuration with subplot radius of 24 ft (7.3152 m). By default, |
| 43 | +#' "macroplot trees" having `DIST` outside the subplot boundary are not |
| 44 | +#' included. This argument may be set to `TRUE` in which case the observation |
| 45 | +#' window will be defined using the FIA optional "macroplot" configuration |
| 46 | +#' instead (58.9 ft or 18.227 m radius, used only in certain areas of the |
| 47 | +#' Pacific Northwest FIA region). |
| 48 | +#' @param npoly Integer value giving the number of edges to use for polygon |
| 49 | +#' approximation. Defaults to `360`. |
26 | 50 | #' @param tree_list A data frame containing a set of tree records for one FIA |
27 | 51 | #' plot (see Deatils). |
28 | 52 | #' @param live_trees A logical value, `TRUE` to include live trees only (the |
29 | 53 | #' default, i.e., `STATUSCD == 1`). |
30 | 54 | #' @param min_dia A numeric value specifying the minimum diameter threshold |
31 | 55 | #' for included trees. The default is `5.0`. Trees less than 5-in. diameter but |
32 | | -#' \>= 1.0-in. diameter, denoted as "saplings", are only recorded in FIA |
33 | | -#' microplots so cannot be stem-mapped across the 4-subplot footprint. |
34 | | -#' @param linear_unit An optional character string specifying the linear |
35 | | -#' distance unit. Defaults to the native FIA unit of `"ft"``, but may be set to |
36 | | -#' `"m"` instead. |
37 | | -#' @param macroplot An optional logical value. The default is `FALSE`, which |
38 | | -#' defines the FIA plot footprint in terms of the standard four-subplot |
39 | | -#' configuration with subplot radius of 24 ft (7.3152 m). See |
40 | | -#' [create_fia_owin()]. By default, "macroplot trees" having `DIST` outside the |
41 | | -#' subplot boundary are not included. |
| 56 | +#' greater than or equal to 1.0-in. diameter, denoted as "saplings", are only |
| 57 | +#' recorded in FIA microplots so cannot be stem-mapped across the full 4-subplot |
| 58 | +#' footprint. |
42 | 59 | #' @param window An optional object of class `"owin"` defining the observation |
43 | 60 | #' window of an FIA plot in the 2-D plane. Defaults to |
44 | 61 | #' `create_fia_owin(linear_unit, macroplot)`. |
|
49 | 66 | #' `factor` marks. If not already `factor`, these will be corced as such upon |
50 | 67 | #' input. `factor` marks are those that take only a finite number of possible |
51 | 68 | #' values (e.g. colors or types). |
52 | | -#' @return |
53 | | -#' An object of class `"ppp"` which defines a point pattern dataset for use |
54 | | -#' with functions of the \pkg{spatstat} library. |
55 | | -#' |
56 | | -#' @seealso |
57 | | -#' [create_fia_owin()] |
58 | 69 | #' |
59 | 70 | #' @examples |
| 71 | +#' # observation window for the standard FIA plot design |
| 72 | +#' w <- create_fia_owin() |
| 73 | +#' summary(w) |
| 74 | +#' |
| 75 | +#' # or using metric units |
| 76 | +#' w <- create_fia_owin("m") |
| 77 | +#' summary(w) |
| 78 | +#' |
| 79 | +#' plot(w, main = "FIA standard four-subplot design") |
| 80 | +#' |
| 81 | +#' # point pattern object for the plantation example dataset |
60 | 82 | #' X <- create_fia_ppp(plantation) |
61 | 83 | #' summary(X) |
62 | 84 | #' |
|
67 | 89 | #' plot(X, main = "Pine plantation stem map", |
68 | 90 | #' shape = "arrows", direction = 90, size = 12, cols = "darkgreen", |
69 | 91 | #' background = "gray90", legend = FALSE) |
| 92 | +#' |
| 93 | +#' # Ripley's K-function |
| 94 | +#' K <- spatstat.explore::Kest(X, rmax = 12) |
| 95 | +#' plot(K, main = "Ripley's K-function for the plantation trees") |
| 96 | +#' @export |
| 97 | +create_fia_owin <- function(linear_unit = "ft", macroplot = FALSE, |
| 98 | + npoly = 360) { |
| 99 | + |
| 100 | + if (is.null(linear_unit)) |
| 101 | + linear_unit <- "ft" |
| 102 | + else if (!(is.character(linear_unit) && length(linear_unit) == 1)) |
| 103 | + stop("'linear_unit' must be a single character string", call. = FALSE) |
| 104 | + else |
| 105 | + linear_unit <- tolower(linear_unit) |
| 106 | + |
| 107 | + if (!(linear_unit %in% c("ft", "foot", "m", "meter", "metre"))) |
| 108 | + stop("'linear_unit' is invalid", call. = FALSE) |
| 109 | + |
| 110 | + if (is.null(macroplot)) |
| 111 | + macroplot <- FALSE |
| 112 | + else if (!(is.logical(macroplot) && length(macroplot) == 1)) |
| 113 | + stop("'macroplot' must be a single logical value", call. = FALSE) |
| 114 | + |
| 115 | + if (is.null(npoly)) |
| 116 | + npoly <- 720L |
| 117 | + else if (!(is.numeric(npoly) && length(npoly) == 1)) |
| 118 | + stop("'npoly' must be a single integer value", call. = FALSE) |
| 119 | + |
| 120 | + # avoid points falling outside a subplot boundary due to rounding error by |
| 121 | + # adding 0.001 here |
| 122 | + subp_radius <- 24.001 |
| 123 | + if (macroplot) |
| 124 | + subp_radius <- 59.801 |
| 125 | + |
| 126 | + unit_conv <- 1 # FIA native unit ft |
| 127 | + unit_names <- c("foot", "feet") |
| 128 | + if (linear_unit %in% c("m", "meter", "metre")) { |
| 129 | + unit_conv <- 0.3048 # ft to m |
| 130 | + unit_names <- c("meter", "meters") |
| 131 | + subp_radius <- subp_radius * unit_conv |
| 132 | + } |
| 133 | + |
| 134 | + s1 <- spatstat.geom::disc(subp_radius, c(0, 0), npoly = npoly) |
| 135 | + s2 <- spatstat.geom::disc(subp_radius, |
| 136 | + c(0, 120 * unit_conv), |
| 137 | + npoly = npoly) |
| 138 | + s3 <- spatstat.geom::disc(subp_radius, |
| 139 | + c(103.92 * unit_conv, -60 * unit_conv), |
| 140 | + npoly = npoly) |
| 141 | + s4 <- spatstat.geom::disc(subp_radius, |
| 142 | + c(-103.92 * unit_conv, -60 * unit_conv), |
| 143 | + npoly = npoly) |
| 144 | + |
| 145 | + w <- spatstat.geom::union.owin(s1, s2, s3, s4) |
| 146 | + spatstat.geom::unitname(w) <- unit_names |
| 147 | + |
| 148 | + return(w) |
| 149 | +} |
| 150 | + |
| 151 | +#' @name spatstat_helpers |
70 | 152 | #' @export |
71 | 153 | create_fia_ppp <- function(tree_list, live_trees = TRUE, min_dia = 5, |
72 | 154 | linear_unit = "ft", macroplot = FALSE, window = NULL, |
|
0 commit comments