From 60037f8ac8782366092b495b0399f0af0a3f3a04 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown <brown.andrewg@gmail.com> Date: Thu, 16 May 2024 06:59:45 -0700 Subject: [PATCH 1/2] get_chorizon_from_SDA: concatenate many:1 texcl, lieutex within RV chtexturegrp --- R/get_component_from_SDA.R | 86 ++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/R/get_component_from_SDA.R b/R/get_component_from_SDA.R index 36269489..254cce61 100644 --- a/R/get_component_from_SDA.R +++ b/R/get_component_from_SDA.R @@ -622,46 +622,55 @@ get_chorizon_from_SDA <- function(WHERE = NULL, duplicates = FALSE, childs = TRUE, nullFragsAreZero = TRUE, droplevels = TRUE, - stringsAsFactors = NULL - ) { + stringsAsFactors = NULL) { if (!missing(stringsAsFactors) && is.logical(stringsAsFactors)) { .Deprecated(msg = sprintf("stringsAsFactors argument is deprecated.\nSetting package option with `NASISDomainsAsFactor(%s)`", stringsAsFactors)) NASISDomainsAsFactor(stringsAsFactors) } q.chorizon <- paste(" - SELECT", if (duplicates == FALSE) {"DISTINCT"} - , "hzname, hzdept_r, hzdepb_r, texture, texcl, lieutex, - fragvol_l, fragvol_r, fragvol_h, - sandtotal_l, sandtotal_r, sandtotal_h, - silttotal_l, silttotal_r, silttotal_h, - claytotal_l, claytotal_r, claytotal_h, - om_l, om_r, om_h, - dbthirdbar_l, dbthirdbar_r, dbthirdbar_h, - ksat_l, ksat_r, ksat_h, - awc_l, awc_r, awc_h, - lep_r, sar_r, ec_r, cec7_r, sumbases_r, - ph1to1h2o_l, ph1to1h2o_r, ph1to1h2o_h, - caco3_l, caco3_r, caco3_h, - kwfact, kffact, c.cokey, ch.chkey - FROM legend l INNER JOIN - mapunit mu ON mu.lkey = l.lkey", - if (duplicates == FALSE) { paste(" INNER JOIN - (SELECT MIN(nationalmusym) nationalmusym2, MIN(mukey) AS mukey2 - FROM mapunit - GROUP BY nationalmusym) AS mu2 ON mu2.mukey2 = mu.mukey - ") - } else { paste(" INNER JOIN - (SELECT nationalmusym, mukey - FROM mapunit) AS mu2 ON mu2.mukey = mu.mukey - ") + SELECT ", ifelse(!duplicates, "DISTINCT", ""), + " hzname, hzdept_r, hzdepb_r, texture, texcl, lieutex, + fragvol_l, fragvol_r, fragvol_h, + sandtotal_l, sandtotal_r, sandtotal_h, + silttotal_l, silttotal_r, silttotal_h, + claytotal_l, claytotal_r, claytotal_h, + om_l, om_r, om_h, + dbthirdbar_l, dbthirdbar_r, dbthirdbar_h, + ksat_l, ksat_r, ksat_h, + awc_l, awc_r, awc_h, + lep_r, sar_r, ec_r, cec7_r, sumbases_r, + ph1to1h2o_l, ph1to1h2o_r, ph1to1h2o_h, + caco3_l, caco3_r, caco3_h, + kwfact, kffact, c.cokey, ch.chkey + FROM legend l INNER JOIN + mapunit mu ON mu.lkey = l.lkey", + + if (duplicates == FALSE) { + paste( + "INNER JOIN + (SELECT MIN(nationalmusym) nationalmusym2, MIN(mukey) AS mukey2 + FROM mapunit + GROUP BY nationalmusym) AS mu2 ON mu2.mukey2 = mu.mukey + " + ) + } else { + paste( + "INNER JOIN + (SELECT nationalmusym, mukey + FROM mapunit) AS mu2 ON mu2.mukey = mu.mukey + " + ) }, - "INNER JOIN - component c ON c.mukey = mu.mukey LEFT JOIN - chorizon ch ON ch.cokey = c.cokey LEFT OUTER JOIN - chtexturegrp chtg ON chtg.chkey = ch.chkey AND rvindicator = 'Yes' RIGHT JOIN - chtexture cht ON cht.chtgkey = chtg.chtgkey - + "INNER JOIN component c ON c.mukey = mu.mukey + INNER JOIN chorizon ch ON ch.cokey = c.cokey + INNER JOIN (SELECT ch2.chkey, texture, + STRING_AGG(texcl, ',') texcl, + STRING_AGG(lieutex, ',') lieutex + FROM chorizon ch2 + LEFT JOIN chtexturegrp chtg ON chtg.chkey = ch2.chkey AND rvindicator = 'Yes' + LEFT JOIN chtexture cht ON cht.chtgkey = chtg.chtgkey + GROUP BY ch2.chkey, chtg.texture) texagg ON texagg.chkey = ch.chkey LEFT OUTER JOIN (SELECT SUM(fragvol_l) fragvol_l, SUM(fragvol_r) fragvol_r, SUM(fragvol_h) fragvol_h, ch2.chkey FROM chorizon ch2 @@ -672,15 +681,12 @@ get_chorizon_from_SDA <- function(WHERE = NULL, duplicates = FALSE, WHERE", WHERE, "ORDER BY c.cokey, hzdept_r ASC;") - + # exec query d.chorizon <- SDA_query(q.chorizon) - ## TODO: might be nice to abstract this into a new function - # hacks to make R CMD check --as-cran happy: - metadata <- NULL - # load local copy of metadata - load(system.file("data/metadata.rda", package = "soilDB")[1]) + # get metadata + metadata <- get_NASIS_metadata() # transform variables and metadata if (!is.null(d.chorizon) && nrow(d.chorizon) > 0){ @@ -813,7 +819,7 @@ get_chorizon_from_SDA <- function(WHERE = NULL, duplicates = FALSE, d.chfrags[idx] <- lapply(d.chfrags[idx], function(x) ifelse(is.na(x), 0, x)) } - d.chorizon <- merge(d.chorizon, d.chfrags, all.x = TRUE, by = "chkey") + d.chorizon <- merge(d.chorizon, d.chfrags, all.x = TRUE, by = "chkey", sort = FALSE) } # } else { From bc0cba6b4216faa7b877109e2580f5b05a1d799c Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown <brown.andrewg@gmail.com> Date: Fri, 17 May 2024 05:52:24 -0700 Subject: [PATCH 2/2] left join for horizonless components --- R/get_component_from_SDA.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/get_component_from_SDA.R b/R/get_component_from_SDA.R index 254cce61..8e80b353 100644 --- a/R/get_component_from_SDA.R +++ b/R/get_component_from_SDA.R @@ -663,8 +663,8 @@ get_chorizon_from_SDA <- function(WHERE = NULL, duplicates = FALSE, ) }, "INNER JOIN component c ON c.mukey = mu.mukey - INNER JOIN chorizon ch ON ch.cokey = c.cokey - INNER JOIN (SELECT ch2.chkey, texture, + LEFT JOIN chorizon ch ON ch.cokey = c.cokey + LEFT JOIN (SELECT ch2.chkey, texture, STRING_AGG(texcl, ',') texcl, STRING_AGG(lieutex, ',') lieutex FROM chorizon ch2