Skip to content

Commit 673baef

Browse files
committed
add convenience functions for unit conversion
1 parent cf7c7fb commit 673baef

8 files changed

Lines changed: 178 additions & 12 deletions

File tree

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ export(calc_crown_overlay)
77
export(calc_crwidth)
88
export(calc_ht_metrics)
99
export(calc_tcc_metrics)
10+
export(cm_to_in)
1011
export(create_fia_owin)
1112
export(create_fia_ppp)
13+
export(ft_to_m)
14+
export(in_to_cm)
1215
export(load_tree_data)
16+
export(m_to_ft)
1317
export(plot_crowns)
1418
export(process_tree_data)

R/convert_units.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#' Convenience functions for unit conversion
2+
#'
3+
#' Functions to convert between SI and US customary units.
4+
#'
5+
#' @name convert_units
6+
#' @details
7+
#' `ft_to_m(x)` converts feet to meters.
8+
#'
9+
#' `m_to_ft(x)` converts meters to feet.
10+
#'
11+
#' `in_to_cm(x)` converts inches to centimeters.
12+
#'
13+
#' `cm_to_in(x)` converts centimeters to inches.
14+
#'
15+
#' @param x Numeric vector of values to convert.
16+
#' @return
17+
#' A numeric vector of the converted values.
18+
#'
19+
#' @examples
20+
#' ft_to_m(1)
21+
#'
22+
#' m_to_ft(1)
23+
#'
24+
#' in_to_cm(1)
25+
#'
26+
#' cm_to_in(1)
27+
#' @export
28+
ft_to_m <- function(x) {
29+
if (!is.numeric(x)) {
30+
stop(cli::format_error(c(
31+
"{.var x} must be a numeric vector",
32+
"x" = "Invalid input type: {.cls {class(x)}}")))
33+
}
34+
35+
x * 0.3048
36+
}
37+
38+
#' @name convert_units
39+
#' @export
40+
m_to_ft <- function(x) {
41+
if (!is.numeric(x)) {
42+
stop(cli::format_error(c(
43+
"{.var x} must be a numeric vector",
44+
"x" = "Invalid input type: {.cls {class(x)}}")))
45+
}
46+
47+
x * 3.28084
48+
}
49+
50+
#' @name convert_units
51+
#' @export
52+
in_to_cm <- function(x) {
53+
if (!is.numeric(x)) {
54+
stop(cli::format_error(c(
55+
"{.var x} must be a numeric vector",
56+
"x" = "Invalid input type: {.cls {class(x)}}")))
57+
}
58+
59+
x * 2.54
60+
}
61+
62+
#' @name convert_units
63+
#' @export
64+
cm_to_in <- function(x) {
65+
if (!is.numeric(x)) {
66+
stop(cli::format_error(c(
67+
"{.var x} must be a numeric vector",
68+
"x" = "Invalid input type: {.cls {class(x)}}")))
69+
}
70+
71+
x * 0.393701
72+
}

R/spatstat_helpers.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,16 @@
106106
#' plot(K, main = "Ripley's K for the plantation FIA plot")
107107
#'
108108
#' ## point pattern object for the `western_redcedar` example data
109-
#' X <- create_fia_ppp(western_redcedar)
109+
#' ## tree distances may be specified in meters
110+
#' trees <- within(western_redcedar, DIST <- ft_to_m(DIST))
111+
#'
112+
#' X <- create_fia_ppp(trees, linear_unit = "m")
110113
#' summary(X)
111114
#'
112115
#' plot(X, pch = 16, main = "Western redcedar FIA plot")
113116
#'
114117
#' # Ripley's K-function
115-
#' K <- spatstat.explore::Kest(X, rmax = 12, correction = "isotropic")
118+
#' K <- spatstat.explore::Kest(X, rmax = ft_to_m(12), correction = "isotropic")
116119
#' plot(K, main = "Ripley's K for the western redcedar FIA plot")
117120
#' @export
118121
create_fia_owin <- function(linear_unit = "ft", macroplot = FALSE,
@@ -241,7 +244,7 @@ create_fia_ppp <- function(tree_list, live_trees = TRUE, min_dia = 5,
241244
tree_list_in <- tree_list[tree_list$DIA >= min_dia, ]
242245
}
243246

244-
xy <- .get_tree_list_xy(tree_list_in)
247+
xy <- .get_tree_list_xy(tree_list_in, linear_unit)
245248

246249
marks <- NULL
247250
if (!is.null(mark_cols))

R/utils.R

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@
22
#'
33
#' `.get_tree_list_xy()` returns a named list with two elements each containing
44
#' a numeric vector of the x and y coordinates for trees within the FIA
5-
#' 4-subplot configuration.
5+
#' 4-subplot configuration. The origin is the center of the center subplot.
66
#'
77
#' @param tree_list A data frame containing the standard tree list columns for
88
#' a plot. This input generally has been pre-filtered, e.g., to live trees with
99
#' `DIA >= 5.0`.
10+
#' @param linear_unit An optional character string specifying the linear
11+
#' distance unit. Defaults to the native FIA unit of `"ft"`, but may be set to
12+
#' `"m"` instead (or `"meter"` / `"metre"`). Specifies units of the input
13+
#' `tree_list$DIST`.
1014
#' @return
1115
#' A named list with elements `x` and `y` containing numeric vectors of stem
1216
#' coordinates.
1317
#' @noRd
1418
#' @export
15-
.get_tree_list_xy <- function(tree_list) {
19+
.get_tree_list_xy <- function(tree_list, linear_unit = "ft") {
20+
unit_conv <- 1 # FIA native unit ft
21+
if (linear_unit %in% c("m", "meter", "metre")) {
22+
unit_conv <- 0.3048 # ft to m
23+
}
24+
1625
x <- rep(NA_real_, nrow(tree_list))
1726
y <- rep(NA_real_, nrow(tree_list))
1827

@@ -24,23 +33,23 @@
2433

2534
# subplot 2 - offsets from plot center
2635
xoff2 <- 0.0
27-
yoff2 <- 120.0
36+
yoff2 <- 120.0 * unit_conv
2837
dist <- tree_list$DIST[tree_list$SUBP == 2]
2938
azimuth <- tree_list$AZIMUTH[tree_list$SUBP == 2]
3039
x[tree_list$SUBP == 2] <- dist * sin(azimuth * (pi / 180)) + xoff2
3140
y[tree_list$SUBP == 2] <- dist * cos(azimuth * (pi / 180)) + yoff2
3241

3342
# subplot 3 - offsets from plot center
34-
xoff3 <- 103.92
35-
yoff3 <- -60.0
43+
xoff3 <- 103.92 * unit_conv
44+
yoff3 <- -60.0 * unit_conv
3645
dist <- tree_list$DIST[tree_list$SUBP == 3]
3746
azimuth <- tree_list$AZIMUTH[tree_list$SUBP == 3]
3847
x[tree_list$SUBP == 3] <- dist * sin(azimuth * (pi / 180)) + xoff3
3948
y[tree_list$SUBP == 3] <- dist * cos(azimuth * (pi / 180)) + yoff3
4049

4150
# subplot 4 - offsets from plot center
42-
xoff4 <- -103.92
43-
yoff4 <- -60.0
51+
xoff4 <- -103.92 * unit_conv
52+
yoff4 <- -60.0 * unit_conv
4453
dist <- tree_list$DIST[tree_list$SUBP == 4]
4554
azimuth <- tree_list$AZIMUTH[tree_list$SUBP == 4]
4655
x[tree_list$SUBP == 4] <- dist * sin(azimuth * (pi / 180)) + xoff4

_pkgdown.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ reference:
1919
- calc_ht_metrics
2020
- calc_tcc_metrics
2121
- spatstat_helpers
22+
- convert_units
2223
- load_tree_data
2324
- plot_crowns
2425
- process_tree_data

man/convert_units.Rd

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

man/spatstat_helpers.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
test_that("convert_units works", {
2+
expect_equal(ft_to_m(1), 0.3048)
3+
expect_equal(ft_to_m(c(1, 10)), c(0.3048, 3.048))
4+
5+
expect_equal(m_to_ft(1), 3.28084)
6+
expect_equal(m_to_ft(c(1, 2)), c(3.28084, 2 * 3.28084))
7+
8+
expect_equal(in_to_cm(1), 2.54)
9+
expect_equal(in_to_cm(c(1, 10)), c(2.54, 25.4))
10+
11+
expect_equal(cm_to_in(1), 0.393701)
12+
expect_equal(cm_to_in(c(1, 10)), c(0.393701, 3.93701))
13+
14+
x_ft <- 24
15+
x_m <- ft_to_m(x_ft)
16+
expect_equal(m_to_ft(x_m), x_ft, tolerance = 1e-4)
17+
expect_equal(ft_to_m(x_ft), x_m, tolerance = 1e-4)
18+
19+
x_in <- 15.1
20+
x_cm <- in_to_cm(x_in)
21+
expect_equal(cm_to_in(x_cm), x_in, tolerance = 1e-4)
22+
expect_equal(in_to_cm(x_in), x_cm, tolerance = 1e-4)
23+
24+
x <- "char"
25+
expect_error(ft_to_m(x), "numeric vector")
26+
expect_error(m_to_ft(x), "numeric vector")
27+
expect_error(in_to_cm(x), "numeric vector")
28+
expect_error(cm_to_in(x), "numeric vector")
29+
})

0 commit comments

Comments
 (0)