Skip to content

Commit f34078b

Browse files
committed
group the spatstat utilities under spatstat_helpers and add to examples
1 parent 3eca4c5 commit f34078b

5 files changed

Lines changed: 157 additions & 177 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Description: Maps individual tree stem locations on field plots of the Forest
1818
License: MIT + file LICENSE
1919
Depends: R (>= 4.1.0)
2020
Imports: gdalraster, methods, spatstat.geom
21-
Suggests: testthat (>= 3.0.0)
21+
Suggests: spatstat.explore, testthat (>= 3.0.0)
2222
URL: https://ctoney.github.io/FIAstemmap/
2323
Config/testthat/edition: 3
2424
Encoding: UTF-8

R/create_fia_owin.R

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
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.
213
#'
314
#' `create_fia_ppp()` returns an object of class `"ppp"` representing the point
415
#' pattern of an FIA tree list in the 2-D plane. A point pattern object defines
516
#' the dataset for using a stem-mapped FIA plot with functions of the
617
#' \pkg{spatstat} library.
718
#'
8-
#' @details
919
#' 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
1121
#' `create_fia_ppp()`:
1222
#' * `PLT_CN`: optional, `create_fia_ppp()` assumes input is for one plot
1323
#' * `SUBP`: required subplot number
@@ -22,23 +32,30 @@
2232
#' * `ACTUALHT`: optional tree actual height (accounts for broken top)
2333
#' * `CCLCD`: optional crown class code
2434
#' * `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`.
2650
#' @param tree_list A data frame containing a set of tree records for one FIA
2751
#' plot (see Deatils).
2852
#' @param live_trees A logical value, `TRUE` to include live trees only (the
2953
#' default, i.e., `STATUSCD == 1`).
3054
#' @param min_dia A numeric value specifying the minimum diameter threshold
3155
#' 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.
4259
#' @param window An optional object of class `"owin"` defining the observation
4360
#' window of an FIA plot in the 2-D plane. Defaults to
4461
#' `create_fia_owin(linear_unit, macroplot)`.
@@ -49,14 +66,19 @@
4966
#' `factor` marks. If not already `factor`, these will be corced as such upon
5067
#' input. `factor` marks are those that take only a finite number of possible
5168
#' 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()]
5869
#'
5970
#' @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
6082
#' X <- create_fia_ppp(plantation)
6183
#' summary(X)
6284
#'
@@ -67,6 +89,66 @@
6789
#' plot(X, main = "Pine plantation stem map",
6890
#' shape = "arrows", direction = 90, size = 12, cols = "darkgreen",
6991
#' 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
70152
#' @export
71153
create_fia_ppp <- function(tree_list, live_trees = TRUE, min_dia = 5,
72154
linear_unit = "ft", macroplot = FALSE, window = NULL,

man/create_fia_owin.Rd

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)