Skip to content

Commit dd240c4

Browse files
authored
Merge pull request #467 from ncss-tech/fix-466
fix: fetchGDB when WHERE is NULL
2 parents 87c8d3f + 25938bb commit dd240c4

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

R/get_component_from_GDB.R

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ get_mapunit_from_GDB <- function(dsn = "gNATSGO_CONUS.gdb",
325325
}
326326
)
327327
pmg <- sf::read_sf(dsn = dsn, query = qry, as_tibble = FALSE, fid_column_name = "copmgrpkey")
328-
328+
pmg$copmgrpkey.1 <- NULL
329329

330330
# remove duplicate rvindicators
331331
dat <- as.data.frame.matrix(table(pmg$cokey, pmg$rvindicator))
@@ -647,6 +647,12 @@ fetchGDB <- function(dsn = "gNATSGO_CONUS.gdb",
647647
stop("package 'aqp' is required", call. = FALSE)
648648
}
649649

650+
stopifnot(length(WHERE) <= 1)
651+
whr <- WHERE
652+
if (is.null(whr)) {
653+
whr <- basename(dsn)
654+
}
655+
650656
# checks
651657
vars_le <- vars_le <- sf::read_sf(dsn = dsn, query = "SELECT * FROM legend LIMIT 0", as_tibble = FALSE, fid_column_name = "lkey") |> names() |> paste(collapse = "|")
652658

@@ -703,7 +709,7 @@ fetchGDB <- function(dsn = "gNATSGO_CONUS.gdb",
703709
if (idx_le) {
704710
message("getting components and horizons from areasymbol = '", unique(x$idx), "'")
705711
} else{
706-
message("getting components and horizons from ", WHERE)
712+
message("getting components and horizons from ", whr)
707713
}
708714

709715
# components
@@ -744,7 +750,7 @@ fetchGDB <- function(dsn = "gNATSGO_CONUS.gdb",
744750

745751
if (idx_co || is.null(WHERE)) {
746752

747-
message("getting components and horizons from ", WHERE)
753+
message("getting components and horizons from ", whr)
748754

749755
# target component table ----
750756
co <- get_component_from_GDB(
@@ -766,8 +772,14 @@ fetchGDB <- function(dsn = "gNATSGO_CONUS.gdb",
766772
childs = childs)
767773
}
768774

769-
le$lkey.1 <- NULL
770-
mu$mukey.1 <- NULL
775+
if (idx_le) {
776+
le$lkey.1 <- NULL
777+
}
778+
779+
if (idx_mu) {
780+
mu$mukey.1 <- NULL
781+
}
782+
771783
co$cokey.1 <- NULL
772784
h$chkey.1 <- NULL
773785

tests/testthat/test-fetchGDB.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
test_that("fetchGDB works", {
2+
3+
skip_on_cran()
4+
5+
skip_if_not_installed("aqp")
6+
7+
skip_if_not_installed("soilDBdata")
8+
9+
f <- system.file("extdata", "manual", "gSSURGO_MH_FY26.zip", package = "soilDBdata")
10+
11+
skip_if_not(nzchar(f))
12+
13+
dsn <- paste0("/vsizip/{", f, "}/gSSURGO_MH.gdb")
14+
15+
# default behavior
16+
res <- suppressWarnings(fetchGDB(dsn))
17+
expect_true(inherits(res, 'SoilProfileCollection'))
18+
19+
# include child tables
20+
res <- suppressWarnings(fetchGDB(dsn, childs = TRUE))
21+
expect_true(inherits(res, 'SoilProfileCollection'))
22+
23+
# trigger legend-level iteration
24+
res <- suppressWarnings(fetchGDB(dsn, WHERE = "legend.areasymbol = legend.areasymbol"))
25+
expect_true(inherits(res, 'SoilProfileCollection'))
26+
})

0 commit comments

Comments
 (0)