From db821be25c88f74593ac8afe46357389179b5cdd Mon Sep 17 00:00:00 2001 From: Daniel Schlaepfer Date: Tue, 19 Sep 2023 11:13:38 -0400 Subject: [PATCH 1/5] Initiate development of v0.2.1 --- DESCRIPTION | 2 +- NEWS.md | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 48c76d5..c84d252 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rSW2exter Title: Access External Data as Input for SOILWAT2 and STEPWAT2 Simulations -Version: 0.2.0 +Version: 0.2.1-9000 Authors@R: c( person( "Daniel", "Schlaepfer", diff --git a/NEWS.md b/NEWS.md index adc5bde..7c3479a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# rSW2exter v0.2.1-9000 + + # rSW2exter v0.2.0 * `fetch_mukeys_spatially_NRCS_SDA()` now handles versions of "soilDB" from `2.5.7` to at least `2.6.14`. From 933091d3cfe22f786b11e1b70185df4a5cb3b422 Mon Sep 17 00:00:00 2001 From: Daniel Schlaepfer Date: Tue, 19 Sep 2023 15:35:52 -0400 Subject: [PATCH 2/5] Update lintr to v3.1.0 * [matrix_apply_linter] Use rowSums(res[,var_stxt3,drop=FALSE], na.rm = TRUE) rather than apply(res[, var_stxt3, drop = FALSE], 1, sum, na.rm = TRUE) * [indentation_linter] Indentation should be 12 spaces but is 10 spaces. * [unnecessary_nested_if_linter] Don't use nested `if` statements, where a single `if` with the combined conditional expression will do. For example, instead of `if (x) { if (y) { ... }}`, use `if (x && y) { ... }`. * [object_usage_linter] local variable 'tmp_tag' assigned but may not be used * [implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`. --- DESCRIPTION | 2 +- NEWS.md | 1 + R/extract_soils_Miller1998_CONUSSoils.R | 2 +- R/extract_soils_NRCS_SDA.R | 38 +++++++++++++------------ R/extract_soils_POLARIS.R | 2 +- tests/testthat/test_soils_NRCS_SDA.R | 2 ++ 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c84d252..a4f6216 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,7 +28,7 @@ Suggests: utils, testthat (>= 3.0.0), spelling, - lintr (>= 3.0.0), + lintr (>= 3.1.0), covr Remotes: github::DrylandEcology/rSW2utils, diff --git a/NEWS.md b/NEWS.md index 7c3479a..ab50adf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,5 @@ # rSW2exter v0.2.1-9000 +* Linting updated to `lintr` >= 3.1 # rSW2exter v0.2.0 diff --git a/R/extract_soils_Miller1998_CONUSSoils.R b/R/extract_soils_Miller1998_CONUSSoils.R index ead7522..715ee66 100644 --- a/R/extract_soils_Miller1998_CONUSSoils.R +++ b/R/extract_soils_Miller1998_CONUSSoils.R @@ -567,7 +567,7 @@ extract_soils_Miller1998_CONUSSoil <- function( if (all(var_stxt3 %in% colnames(res))) { has_vals <- complete.cases(res[, var_stxt3]) & - apply(res[, var_stxt3, drop = FALSE], 1, sum, na.rm = TRUE) > 0 + rowSums(res[, var_stxt3, drop = FALSE], na.rm = TRUE) > 0 res[has_vals, var_stxt3] <- rSW2utils::scale_rounded_by_sum( x = res[has_vals, var_stxt3], diff --git a/R/extract_soils_NRCS_SDA.R b/R/extract_soils_NRCS_SDA.R index daf114a..d9320fd 100644 --- a/R/extract_soils_NRCS_SDA.R +++ b/R/extract_soils_NRCS_SDA.R @@ -235,7 +235,7 @@ calculate_soil_depth_NRCS <- function( ) ids_ph_restricted <- which( xc[, "check"] & !is.na(xc[, "ph1to1h2o_r"]) & - xc[, "ph1to1h2o_r"] <= 3.5 + xc[, "ph1to1h2o_r"] <= 3.5 ) c( @@ -388,10 +388,10 @@ calculate_soil_depth_NRCS <- function( MARGIN = 1, FUN = function(x) { !is.na(x[[1L]]) & - x[[1L]] > 0 & - x[[2L]] == 0 & - !is.na(x[[3L]]) & - x[[3L]] > 0 + x[[1L]] > 0 & + x[[2L]] == 0 & + !is.na(x[[3L]]) & + x[[3L]] > 0 } ) locs_table_depths[ids, "SoilDepth_cm"] <- locs_table_depths[ids, "depth_L1"] @@ -718,16 +718,14 @@ fetch_soils_from_NRCS_SDA <- function( tmp_sql <- paste(sql, collapse = " ") res[[k]] <- suppressMessages(soilDB::SDA_query(tmp_sql)) - if (length(res) > 0) { - if (inherits(res[[k]], "try-error")) { - message( - "Error produced during call to `soilDB::SDA_query`; ", - "result will be set to NULL; query leading to error was: ", - tmp_sql - ) - warning(res[[k]]) - res[[k]] <- NULL - } + if (length(res) > 0 && inherits(res[[k]], "try-error")) { + message( + "Error produced during call to `soilDB::SDA_query`; ", + "result will be set to NULL; query leading to error was: ", + tmp_sql + ) + warning(res[[k]]) + res[[k]] <- NULL } if (has_progress_bar) { @@ -960,12 +958,14 @@ extract_soils_NRCS_SDA <- function( if (FALSE) { # e.g., unique soil units defined by mukey-component combinations + # nolint start: object_usage_linter. tmp_tag <- apply( locs_keys[, c("mukey", "compname", "comppct_r", "localphase")], MARGIN = 1, FUN = function(x) paste0(as.integer(x[[1L]]), "_", x[[2L]]) ) locs_keys[, "unit_id"] <- match(tmp_tag, unique(tmp_tag)) + # nolint end: object_usage_linter. } @@ -989,6 +989,7 @@ extract_soils_NRCS_SDA <- function( if (FALSE) { # e.g., unique soil units defined by mukey-compname combinations + # nolint start: object_usage_linter. tmp_tag2 <- apply( res[, c("MUKEY", "compname", "comppct_r", "localphase")], MARGIN = 1, @@ -996,6 +997,7 @@ extract_soils_NRCS_SDA <- function( ) ids <- match(tmp_tag2, tmp_tag) res[, "unit_id"] <- locs_keys[ids, "unit_id"] + # nolint end: object_usage_linter. } @@ -1279,7 +1281,7 @@ extract_soils_NRCS_SDA <- function( if (all(var_stxt3 %in% colnames(res))) { has_vals <- complete.cases(res[, var_stxt3]) & - apply(res[, var_stxt3, drop = FALSE], 1, sum, na.rm = TRUE) > 0 + rowSums(res[, var_stxt3, drop = FALSE], na.rm = TRUE) > 0 res[has_vals, var_stxt3] <- rSW2utils::scale_rounded_by_sum( x = res[has_vals, var_stxt3], @@ -1326,8 +1328,8 @@ extract_soils_NRCS_SDA <- function( ids_h0 <- which(locs_table_depths[, "N_horizons"] == 0) if ( length(ids_h0) > 0 && - !missing(x) && - method == "SSURGO_then_STATSGO" + !missing(x) && + method == "SSURGO_then_STATSGO" ) { # Call again for nosoil rows and extract from STATSGO instead of SSURGO diff --git a/R/extract_soils_POLARIS.R b/R/extract_soils_POLARIS.R index 07d3d05..2ba7793 100644 --- a/R/extract_soils_POLARIS.R +++ b/R/extract_soils_POLARIS.R @@ -567,7 +567,7 @@ extract_soils_POLARIS <- function( for (k in seq_len(N_layers)) { has_vals <- complete.cases(res[, var_stxt3, k]) & - apply(res[, var_stxt3, k, drop = FALSE], 1, sum, na.rm = TRUE) > 0 + rowSums(res[, var_stxt3, k, drop = FALSE], na.rm = TRUE) > 0 res[has_vals, var_stxt3, k] <- rSW2utils::scale_rounded_by_sum( x = res[has_vals, var_stxt3, k], diff --git a/tests/testthat/test_soils_NRCS_SDA.R b/tests/testthat/test_soils_NRCS_SDA.R index 0ff7bc5..d0a9fb1 100644 --- a/tests/testthat/test_soils_NRCS_SDA.R +++ b/tests/testthat/test_soils_NRCS_SDA.R @@ -98,6 +98,7 @@ test_that("Extract soils from NRCS SDA", { x[, "organic"] <- is_NRCS_horizon_organic(x) expect_true(all(x[, "organic"] %in% c(NA, FALSE, TRUE))) + # nolint start: implicit_assignment_linter. expect_silent( sd1 <- calculate_soil_depth_NRCS( x, @@ -120,6 +121,7 @@ test_that("Extract soils from NRCS SDA", { var_soiltexture = var_soiltexture ) ) + # nolint end: implicit_assignment_linter. expect_true(all(expected_depth_variables %in% colnames(sd1))) expect_true( From 6c1f17313f04f493821ad0393bb4b9f8010618b6 Mon Sep 17 00:00:00 2001 From: Daniel Schlaepfer Date: Tue, 19 Sep 2023 15:36:09 -0400 Subject: [PATCH 3/5] Correct spelling in NEWS --- NEWS.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index ab50adf..4c32bf5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,15 +3,15 @@ # rSW2exter v0.2.0 -* `fetch_mukeys_spatially_NRCS_SDA()` now handles versions of "soilDB" +* `fetch_mukeys_spatially_NRCS_SDA()` now handles versions of `"soilDB"` from `2.5.7` to at least `2.6.14`. * `extract_soils_NRCS_SDA()` queries include now `localphase`; this should improve the ability to identify a component of a soil map unit - across NRCS data releases by a combination of + across `NRCS` data releases by a combination of `compname`, `comppct_r`, `localphase`. * `extract_soils_NRCS_SDA()` gains argument `only_soilcomp` which excludes non-soil components, i.e., those that are not "Miscellaneous areas" and - are not "NOTCOM" (not completed). + are not `"NOTCOM"` (not completed). * Linting updated to `lintr` >= 3 and lint workflow switched from package tests to Github Action (#5). From 899691963861788db3042a557f6bd91758eb6117 Mon Sep 17 00:00:00 2001 From: Daniel Schlaepfer Date: Tue, 19 Sep 2023 15:01:06 -0400 Subject: [PATCH 4/5] `fetch_mukeys_spatially_NRCS_SDA()` now requires at least `soilDB` v2.6.10 - `soilDB` v2.6.10 started to handle `sf` objects - now, we require this minimal version and drop support of sp objects --- DESCRIPTION | 3 +-- NEWS.md | 2 ++ R/extract_soils_NRCS_SDA.R | 47 +++++++++++--------------------------- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a4f6216..49ef907 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,8 +23,7 @@ Imports: sf Suggests: FedData (>= 3.0.2), - soilDB (>= 2.5.7), - sp, + soilDB (>= 2.6.10), utils, testthat (>= 3.0.0), spelling, diff --git a/NEWS.md b/NEWS.md index 4c32bf5..303119e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,6 @@ # rSW2exter v0.2.1-9000 +* `fetch_mukeys_spatially_NRCS_SDA()` now requires at least + `"soilDB"` version `2.6.10` (and no longer supports `"sp"`). * Linting updated to `lintr` >= 3.1 diff --git a/R/extract_soils_NRCS_SDA.R b/R/extract_soils_NRCS_SDA.R index d9320fd..40053e6 100644 --- a/R/extract_soils_NRCS_SDA.R +++ b/R/extract_soils_NRCS_SDA.R @@ -457,23 +457,15 @@ fetch_mukeys_spatially_NRCS_SDA <- function( stopifnot( requireNamespace("soilDB"), - "db" %in% methods::formalArgs(soilDB::SDA_spatialQuery), curl::has_internet() ) - vsoilDB <- getNamespaceVersion("soilDB") #------ Make sure inputs are correctly formatted db <- match.arg(db) - if (vsoilDB >= as.numeric_version("2.6.10")) { - locations <- rSW2st::as_points(x, to_class = "sf", crs = crs) - nxlocs <- nrow(locations) - } else { - stopifnot(requireNamespace("sp")) - locations <- rSW2st::as_points(x, to_class = "sp", crs = crs) - nxlocs <- length(locations) - } + locations <- rSW2st::as_points(x, to_class = "sf", crs = crs) + nxlocs <- nrow(locations) #--- Extract mukeys for each point location @@ -505,13 +497,9 @@ fetch_mukeys_spatially_NRCS_SDA <- function( for (k in seq_len(N_chunks)) { res_mukeys <- try( soilDB::SDA_spatialQuery( - geom = locations[ids_chunks[[k]], ], + geom = locations[ids_chunks[[k]], ], # sf since soilDB v2.6.10 db = db, - what = if (vsoilDB >= as.numeric_version("2.6.3")) { - "mupolygon" - } else { - "geom" - } + what = "mupolygon" # since soilDB v2.6.3 ), silent = FALSE ) @@ -528,25 +516,16 @@ fetch_mukeys_spatially_NRCS_SDA <- function( # Extract mukey for each location because # return values of `SDA_spatialQuery` are not ordered by input `geom` # (unless `byFeature = TRUE` since v2.6.10) - res[[k]] <- if (inherits(locations, "sf")) { - ids <- unlist(unclass( - sf::st_intersects(locations[ids_chunks[[k]], ], res_mukeys) - )) - as.vector(res_mukeys[ids, "mukey", drop = TRUE]) - - } else if (inherits(locations, "Spatial")) { - # sp is only used if soilDB < 2.6.10 - sp::over( - x = sp::spTransform( - locations[ids_chunks[[k]], ], - CRSobj = sp::proj4string(res_mukeys) - ), - y = res_mukeys - )[, "mukey"] - - } else { - stop(class(res_mukeys), "/", class(locations), " is not implemented.") + tmp <- sf::st_intersects(locations[ids_chunks[[k]], ], res_mukeys) + ltmp <- lengths(tmp) + + if (any(ltmp == 0L, ltmp > 1L)) { + stop("Spatial SDA query return no or more than one result.") } + + res[[k]] <- as.vector( + res_mukeys[unlist(unclass(tmp)), "mukey", drop = TRUE] + ) } if (has_progress_bar) { From e2efa62a73416cd900fafd964450a88d9e26289e Mon Sep 17 00:00:00 2001 From: Daniel Schlaepfer Date: Wed, 20 Sep 2023 09:37:10 -0400 Subject: [PATCH 5/5] Finalize v0.2.1 --- DESCRIPTION | 4 ++-- NEWS.md | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 49ef907..7e4807d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rSW2exter Title: Access External Data as Input for SOILWAT2 and STEPWAT2 Simulations -Version: 0.2.1-9000 +Version: 0.2.1 Authors@R: c( person( "Daniel", "Schlaepfer", @@ -26,7 +26,7 @@ Suggests: soilDB (>= 2.6.10), utils, testthat (>= 3.0.0), - spelling, + spelling (>= 2.1.0), lintr (>= 3.1.0), covr Remotes: diff --git a/NEWS.md b/NEWS.md index 303119e..ec04495 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,7 @@ -# rSW2exter v0.2.1-9000 +# rSW2exter v0.2.1 * `fetch_mukeys_spatially_NRCS_SDA()` now requires at least `"soilDB"` version `2.6.10` (and no longer supports `"sp"`). -* Linting updated to `lintr` >= 3.1 +* Linting updated to `lintr` >= `3.1`. # rSW2exter v0.2.0 @@ -14,7 +14,7 @@ * `extract_soils_NRCS_SDA()` gains argument `only_soilcomp` which excludes non-soil components, i.e., those that are not "Miscellaneous areas" and are not `"NOTCOM"` (not completed). -* Linting updated to `lintr` >= 3 and +* Linting updated to `lintr` >= `3.0` and lint workflow switched from package tests to Github Action (#5). # rSW2exter v0.1.0