Skip to content

Commit

Permalink
fetchNASIS("components"): Use a custom query for mapunit info
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Jun 9, 2022
1 parent cdd0330 commit 0495bdc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
28 changes: 20 additions & 8 deletions R/fetchNASIS_components.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@
assign('component.hz.problems', value = character(0), envir = soilDB.env)
}

# load data in pieces
# optionally legend and mapunit information are included if in local DB/selected set
# includes possible results for rep and non-rep DMUs and any mustatus
f.lg <- get_legend_from_NASIS(SS = SS, dsn = dsn)
f.mu <- get_mapunit_from_NASIS(SS = SS, repdmu = FALSE, dsn = dsn) # include possible results for rep and non-rep DMUS
mu.q <- "SELECT ng.grpname, liid, lmapunitiid, lmapunitiid AS mukey, areatypename,
nationalmusym, muiid, musym, muname, mukind, mutype, muacres, mustatus,
dmuinvesintens, farmlndcl, dmuiid, repdmu
FROM area a
INNER JOIN areatype at ON at.areatypeiid = a.areatypeiidref
INNER JOIN legend_View_1 l ON l.areaiidref = a.areaiid
INNER JOIN lmapunit_View_1 lmu ON lmu.liidref = l.liid
INNER JOIN mapunit_View_1 mu ON mu.muiid = lmu.muiidref
INNER JOIN nasisgroup ng ON ng.grpiid = mu.grpiidref
INNER JOIN correlation_View_1 cor ON cor.muiidref = mu.muiid
INNER JOIN datamapunit_View_1 dmu ON dmu.dmuiid = cor.dmuiidref"
if (!SS) {
mu.q <- gsub("_View_1", "", mu.q)
}
f.mu <- uncode(dbQueryNASIS(NASIS(dsn = dsn), mu.q), dsn = dsn)

# load data in pieces
f.comp <- get_component_data_from_NASIS_db(SS = SS, dsn = dsn, nullFragsAreZero = nullFragsAreZero)
f.chorizon <- get_component_horizon_data_from_NASIS_db(SS = SS, fill = fill, dsn = dsn, nullFragsAreZero = nullFragsAreZero)
f.copm <- get_component_copm_data_from_NASIS_db(SS = SS, dsn = dsn)
Expand Down Expand Up @@ -75,12 +92,7 @@

# add mapunit data to object if any
if (!is.null(f.mu) && nrow(f.mu) > 0) {
f.mu$mukey <- f.mu$lmapunitiid
site(f.chorizon) <- f.mu[,c("liid", "lmapunitiid", "mukey",
"nationalmusym", "muiid", "musym",
"muname", "mukind", "mutype",
"mustatus", "dmuinvesintens",
"farmlndcl", "dmuiid", "repdmu")] # left-join via dmuiid
site(f.chorizon) <- f.mu # left-join via dmuiid

This comment has been minimized.

Copy link
@dylanbeaudette

dylanbeaudette Jun 11, 2022

Member

No rush, but I've got a quick question related to this join in cases where a single DMU is linked to multiple MU. It has been a while, but back in 2018 we used a helper function get_component_correlation_data_from_NASIS_db() to get tables above component, so that the join could be done after optional filtering.

This comment has been minimized.

Copy link
@dylanbeaudette

dylanbeaudette Jun 11, 2022

Member

I'll try to find an example of a map unit where this is true.

This comment has been minimized.

Copy link
@brownag

brownag Jun 11, 2022

Author Member

Check out dmuiid 290028 (mild) or 296655 (extreme). You are right that as implemented it is problematic; these will appear as if only on one legend/one mukey.

This comment has been minimized.

Copy link
@brownag

brownag Jun 11, 2022

Author Member

This got kinda stuck in my mind.... here are some ideas #251

}

# add legend data to object if any
Expand Down
4 changes: 2 additions & 2 deletions R/get_component_data_from_NASIS_db.R
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ get_component_horizon_data_from_NASIS_db <- function(SS = TRUE,
LEFT OUTER JOIN chtexturegrp_View_1 cht ON cht.chiidref = ch.chiid AND cht.rvindicator = 1
LEFT OUTER JOIN chstructgrp_View_1 chs ON chs.chiidref = ch.chiid AND chs.rvindicator = 1
-- why is this here?
-- why is this here? to order on dmudesc
INNER JOIN datamapunit_View_1 dmu ON dmu.dmuiid = co.dmuiidref
ORDER BY dmudesc, comppct_r DESC, compname ASC, hzdept_r ASC;"
Expand Down Expand Up @@ -658,7 +658,7 @@ get_component_horizon_data_from_NASIS_db <- function(SS = TRUE,

# "sieving" chfrags, chuarts tables for parity with fetchNASIS("pedons") @horizons slot columns

if (nrow(d) > 0){
if (nrow(d) > 0) {
# horizon fragments
chf <- simplifyFragmentData(
uncode(dbQueryNASIS(channel, q2, close = FALSE), dsn = dsn),
Expand Down

0 comments on commit 0495bdc

Please sign in to comment.