Skip to content

Commit 20a45fd

Browse files
committed
add ac_to_ha() and ha_to_ac()
1 parent cba4fb5 commit 20a45fd

6 files changed

Lines changed: 89 additions & 11 deletions

File tree

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export(.get_fia_plot_geom)
44
export(.get_tree_list_xy)
55
export(DEFAULT_TREE_COLUMNS)
6+
export(ac_to_ha)
67
export(calc_crown_overlay)
78
export(calc_crwidth)
89
export(calc_ht_metrics)
@@ -11,6 +12,7 @@ export(cm_to_in)
1112
export(create_fia_owin)
1213
export(create_fia_ppp)
1314
export(ft_to_m)
15+
export(ha_to_ac)
1416
export(in_to_cm)
1517
export(load_tree_data)
1618
export(m_to_ft)

R/convert_units.R

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#' Convenience functions for length unit conversion
1+
#' Convenience functions for common unit conversions
22
#'
33
#' Functions to convert between US customary units and SI units for
4-
#' measurements of length used with tree data.
4+
#' measurements of length and area commonly used with tree data.
55
#'
66
#' @name convert_units
77
#' @details
@@ -13,10 +13,18 @@
1313
#'
1414
#' `cm_to_in(x)` converts centimeters to inches.
1515
#'
16+
#' `ac_to_ha()` converts acres to hectares.
17+
#'
18+
#' `ha_to_ac()` converts hectares to acres.
19+
#'
1620
#' @param x Numeric vector of values to convert.
1721
#' @return
1822
#' A numeric vector of the converted values.
1923
#'
24+
#' @note
25+
#' The hectare (ha) is technically a non-SI unit of area that is [accepted for
26+
#' use with SI](https://en.wikipedia.org/wiki/International_System_of_Units#Non-SI_units_accepted_for_use_with_SI).
27+
#'
2028
#' @examples
2129
#' ft_to_m(1)
2230
#'
@@ -25,6 +33,10 @@
2533
#' in_to_cm(1)
2634
#'
2735
#' cm_to_in(1)
36+
#'
37+
#' ac_to_ha(1)
38+
#'
39+
#' ha_to_ac(1)
2840
#' @export
2941
ft_to_m <- function(x) {
3042
if (!is.numeric(x)) {
@@ -71,3 +83,27 @@ cm_to_in <- function(x) {
7183

7284
x * 0.393701
7385
}
86+
87+
#' @name convert_units
88+
#' @export
89+
ac_to_ha <- function(x) {
90+
if (!is.numeric(x)) {
91+
stop(cli::format_error(c(
92+
"{.var x} must be a numeric vector",
93+
"x" = "Invalid input type: {.cls {class(x)}}")))
94+
}
95+
96+
x * 4046.8564224 / 10000
97+
}
98+
99+
#' @name convert_units
100+
#' @export
101+
ha_to_ac <- function(x) {
102+
if (!is.numeric(x)) {
103+
stop(cli::format_error(c(
104+
"{.var x} must be a numeric vector",
105+
"x" = "Invalid input type: {.cls {class(x)}}")))
106+
}
107+
108+
x * 10000 / 4046.8564224
109+
}

README.Rmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pak::pak("firelab/FIAstemmap")
4949

5050
### Predict tree crown width
5151

52-
The data frame `cw_coef` contains a curated set of linear regression coefficients for predicting crown width from stem diameter of tree species in the conterminous US (see `?cw_coef`). The method for crown width prediction attempts to avoid extrapolation beyond the range of the model fitting data by providing reasonable fall backs for the obvious cases. Details are given in the documentation for `calc_crwidth()`. The input is a data frame of tree records which must have columns `SPCD` (FIA species code), `STATUSCD` (FIA tree status code, `1` = live) and `DIA` (FIA tree diameter). FIA distribute data using US customary units with `DIA` given in inches, tree height in feet, etc. Convenience functions are provided for converting to and from SI units (`in_to_cm()`, `cm_to_in()`, `ft_to_m()`, `m_to_ft()`).
52+
The data frame `cw_coef` contains a curated set of linear regression coefficients for predicting crown width from stem diameter of tree species in the conterminous US (see `?cw_coef`). The method for crown width prediction attempts to avoid extrapolation beyond the range of the model fitting data by providing reasonable fall backs for the obvious cases. Details are given in the documentation for `calc_crwidth()`. The input is a data frame of tree records which must have columns `SPCD` (FIA species code), `STATUSCD` (FIA tree status code, `1` = live) and `DIA` (FIA tree diameter). FIA distribute data in [United States customary units](https://en.wikipedia.org/wiki/United_States_customary_units) with tree diameter given in inches, height in feet, and area in acres. Convenience functions are provided for converting to and from SI units[^1^](#footnotes) (`in_to_cm()`, `cm_to_in()`, `ft_to_m()`, `m_to_ft()`, `ac_to_ha()`, `ha_to_ac()`).
5353

5454
The `plantation` dataset used here is an example tree list included in the package.
5555

@@ -196,3 +196,7 @@ _Ecological Indicators_, Volume 142, 109271, https://doi.org/10.1016/j.ecolind.2
196196
[14] Burrill, Elizabeth A.; DiTommaso, Andrea M.; Turner, Jeffery A.; Pugh, Scott A.; Christensen, Glenn; Kralicek, Karin M.; Perry, Carol J.; Lepine, Lucie C.; Walker, David M.; Conkling, Barbara L. 2024. The Forest Inventory and Analysis Database, FIADB user guides, volume: database description (version 9.4), nationwide forest inventory (NFI). U.S. Department of Agriculture, Forest Service. 1016 p. [Online]. Available at: https://research.fs.usda.gov/understory/forest-inventory-and-analysis-database-user-guide-nfi.
197197

198198
[15] Crookston, N.L. and A.R. Stage. (1999). Percent canopy cover and stand structure statistics from the Forest Vegetation Simulator. Gen. Tech. Rep. RMRS-GTR-24. Ogden, UT: U. S. Department of Agriculture, Forest Service, Rocky Mountain Research Station. 11 p. Available: https://research.fs.usda.gov/treesearch/6261.
199+
200+
## Footnotes
201+
202+
^1^ The hectare (ha) is technically a non-SI unit of area that is [accepted for use with SI](https://en.wikipedia.org/wiki/International_System_of_Units#Non-SI_units_accepted_for_use_with_SI).

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ model fitting data by providing reasonable fall backs for the obvious
7171
cases. Details are given in the documentation for `calc_crwidth()`. The
7272
input is a data frame of tree records which must have columns `SPCD`
7373
(FIA species code), `STATUSCD` (FIA tree status code, `1` = live) and
74-
`DIA` (FIA tree diameter). FIA distribute data using US customary units
75-
with `DIA` given in inches, tree height in feet, etc. Convenience
76-
functions are provided for converting to and from SI units
77-
(`in_to_cm()`, `cm_to_in()`, `ft_to_m()`, `m_to_ft()`).
74+
`DIA` (FIA tree diameter). FIA distribute data in [United States
75+
customary
76+
units](https://en.wikipedia.org/wiki/United_States_customary_units) with
77+
tree diameter given in inches, height in feet, and area in acres.
78+
Convenience functions are provided for converting to and from SI
79+
units[<sup>1</sup>](#footnotes) (`in_to_cm()`, `cm_to_in()`,
80+
`ft_to_m()`, `m_to_ft()`, `ac_to_ha()`, `ha_to_ac()`).
7881

7982
The `plantation` dataset used here is an example tree list included in
8083
the package.
@@ -353,7 +356,7 @@ f <- system.file("extdata/mt_lnf_2022_1cond_tree.csv", package="FIAstemmap")
353356
tree_table <- load_tree_data(f)
354357
#> ! The data source does not have DIST and/or AZIMUTH.
355358
#> ℹ Fetching tree data
356-
#> ✔ Fetching tree data [17ms]
359+
#> ✔ Fetching tree data [14ms]
357360
#>
358361
#> ℹ 910 tree records returned.
359362

@@ -517,3 +520,9 @@ stand structure statistics from the Forest Vegetation Simulator.
517520
Gen. Tech. Rep. RMRS-GTR-24. Ogden, UT: U. S. Department of Agriculture,
518521
Forest Service, Rocky Mountain Research Station. 11 p. Available:
519522
<https://research.fs.usda.gov/treesearch/6261>.
523+
524+
## Footnotes
525+
526+
<sup>1</sup> The hectare (ha) is technically a non-SI unit of area that
527+
is [accepted for use with
528+
SI](https://en.wikipedia.org/wiki/International_System_of_Units#Non-SI_units_accepted_for_use_with_SI).

man/convert_units.Rd

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

tests/testthat/test-convert_units.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ test_that("convert_units works", {
1111
expect_equal(cm_to_in(1), 0.393701)
1212
expect_equal(cm_to_in(c(1, 10)), c(0.393701, 3.93701))
1313

14+
expect_equal(ac_to_ha(1), 0.404685642, tolerance = 1e-6)
15+
expect_equal(ac_to_ha(c(1, 10)), c(0.404685642, 4.04685642),
16+
tolerance = 1e-6)
17+
1418
x_ft <- 24
1519
x_m <- ft_to_m(x_ft)
1620
expect_equal(m_to_ft(x_m), x_ft, tolerance = 1e-4)
@@ -19,9 +23,15 @@ test_that("convert_units works", {
1923
x_cm <- in_to_cm(x_in)
2024
expect_equal(cm_to_in(x_cm), x_in, tolerance = 1e-4)
2125

22-
x <- "char"
26+
x_ac <- 6.018046
27+
x_ha <- ac_to_ha(x_ac)
28+
expect_equal(ha_to_ac(x_ha), x_ac, tolerance = 1e-4)
29+
30+
x <- "non-numeric"
2331
expect_error(ft_to_m(x), "numeric vector")
2432
expect_error(m_to_ft(x), "numeric vector")
2533
expect_error(in_to_cm(x), "numeric vector")
2634
expect_error(cm_to_in(x), "numeric vector")
35+
expect_error(ac_to_ha(x), "numeric vector")
36+
expect_error(ha_to_ac(x), "numeric vector")
2737
})

0 commit comments

Comments
 (0)