From d28901b624000ced33f49e58a6552b7be496a228 Mon Sep 17 00:00:00 2001 From: Andrew Gene Brown Date: Mon, 12 Feb 2024 14:22:24 -0800 Subject: [PATCH] get_site_data_from_NASIS_db: Add state, county, mlra area references - https://github.com/ncss-tech/soilDB/issues/272 - thanks to Zach Van Abbema for suggestion --- R/get_site_data_from_NASIS_db.R | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/R/get_site_data_from_NASIS_db.R b/R/get_site_data_from_NASIS_db.R index 3714882e..bbc3f4d2 100644 --- a/R/get_site_data_from_NASIS_db.R +++ b/R/get_site_data_from_NASIS_db.R @@ -91,6 +91,13 @@ ORDER BY pedon_View_1.peiid ;" INNER JOIN siteothvegclass_View_1 ON siteothvegclass_View_1.siteiidref = site_View_1.siteiid INNER JOIN othvegclass ON siteothvegclass_View_1.ovegcliidref = othvegclass.ovegcliid" + q4 <- "SELECT siteiid, area.areasymbol AS site_state FROM area + LEFT JOIN site_View_1 ON area.areaiid = site_View_1.stateareaiidref" + q5 <- "SELECT siteiid, area.areasymbol AS site_county FROM area + LEFT JOIN site_View_1 ON area.areaiid = site_View_1.countyareaiidref" + q6 <- "SELECT siteiid, area.areasymbol AS site_mlra FROM area + LEFT JOIN site_View_1 ON area.areaiid = site_View_1.mlraareaiidref" + channel <- dbConnectNASIS(dsn) if (inherits(channel, 'try-error')) @@ -101,6 +108,9 @@ ORDER BY pedon_View_1.peiid ;" q <- gsub(pattern = '_View_1', replacement = '', x = q, fixed = TRUE) q2 <- gsub(pattern = '_View_1', replacement = '', x = q2, fixed = TRUE) q3 <- gsub(pattern = '_View_1', replacement = '', x = q3, fixed = TRUE) + q4 <- gsub(pattern = '_View_1', replacement = '', x = q4, fixed = TRUE) + q5 <- gsub(pattern = '_View_1', replacement = '', x = q5, fixed = TRUE) + q6 <- gsub(pattern = '_View_1', replacement = '', x = q6, fixed = TRUE) } # exec query @@ -167,7 +177,7 @@ ORDER BY pedon_View_1.peiid ;" } # join-in othervegclass string - sov <- try(dbQueryNASIS(channel, q3)) + sov <- try(dbQueryNASIS(channel, q3, close = FALSE)) if (!inherits(sov, 'try-error') && nrow(sov) > 0) { ov <- data.table::data.table(sov)[, .formatOtherVegString(.SD, id.name = "siteiid", name.sep = ' & '), @@ -178,6 +188,23 @@ ORDER BY pedon_View_1.peiid ;" } } + # join-in state, county, mlra string + state <- try(dbQueryNASIS(channel, q4, close = FALSE)) + county <- try(dbQueryNASIS(channel, q5, close = FALSE)) + mlra <- try(dbQueryNASIS(channel, q6)) + + if (!inherits(state, 'try-error') && nrow(state) > 0) { + d2 <- merge(d2, state, by = "siteiid", all.x = TRUE, sort = FALSE) + } + + if (!inherits(county, 'try-error') && nrow(state) > 0) { + d2 <- merge(d2, county, by = "siteiid", all.x = TRUE, sort = FALSE) + } + + if (!inherits(mlra, 'try-error') && nrow(state) > 0) { + d2 <- merge(d2, mlra, by = "siteiid", all.x = TRUE, sort = FALSE) + } + # https://github.com/ncss-tech/soilDB/issues/41 # warn if mixed datums # if (length(na.omit(unique(d2$horizdatnm))) > 1)