Skip to content

Commit

Permalink
get_site_data_from_NASIS_db: Add state, county, mlra area references
Browse files Browse the repository at this point in the history
 - #272
 - thanks to Zach Van Abbema for suggestion
  • Loading branch information
brownag committed Feb 12, 2024
1 parent 89d68db commit d28901b
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion R/get_site_data_from_NASIS_db.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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
Expand Down Expand Up @@ -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 = ' & '),
Expand All @@ -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)
Expand Down

0 comments on commit d28901b

Please sign in to comment.