-
Notifications
You must be signed in to change notification settings - Fork 0
/
legend_tools.R
40 lines (38 loc) · 1.9 KB
/
legend_tools.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#' get_projectmapunits_by_uprojectid
#'
#' @param uprojectid vector of user project IDs
#' @param areasymbol Soil Survey Legend areasymbol to include in WHERE clause; Default: `NULL` is no constraint on areasymbol
#' @param dsn data source name, default is `soilDB::NASIS()` _OdbcConnection_
#'
#' @return data.frame result
#' @export
#' @importFrom soilDB dbQueryNASIS NASIS format_SQL_in_statement
get_projectmapunits_by_uprojectid <- function(uprojectid, areasymbol = NULL, dsn = soilDB::NASIS()) {
q <- sprintf("SELECT * FROM mapunit
INNER JOIN lmapunit ON lmapunit.muiidref = mapunit.muiid
INNER JOIN legend ON legend.liid = lmapunit.liidref
INNER JOIN area ON area.areaiid = legend.areaiidref
INNER JOIN projectmapunit ON projectmapunit.muiidref = mapunit.muiid
INNER JOIN project ON project.projectiid = projectmapunit.projectiidref
WHERE uprojectid IN %s%s",
soilDB::format_SQL_in_statement(uprojectid),
ifelse(is.null(areasymbol), "", sprintf(" AND areasymbol IN %s",
soilDB::format_SQL_in_statement(areasymbol))))
soilDB::dbQueryNASIS(dsn, q)
}
#' get_lmapunit_by_areasymbol
#'
#' @param areasymbol vector of areasymbols
#' @param dsn data source name, default is `soilDB::NASIS()` _OdbcConnection_
#'
#' @return data.frame result
#' @export
#' @importFrom soilDB dbQueryNASIS NASIS format_SQL_in_statement
get_lmapunit_by_areasymbol <- function(areasymbol, dsn = soilDB::NASIS()) {
q <- sprintf("SELECT * FROM mapunit
INNER JOIN lmapunit ON lmapunit.muiidref = mapunit.muiid
INNER JOIN legend ON legend.liid = lmapunit.liidref
INNER JOIN area ON area.areaiid = legend.areaiidref
WHERE areasymbol IN %s", soilDB::format_SQL_in_statement(areasymbol))
soilDB::dbQueryNASIS(dsn, q)
}