Skip to content

Commit

Permalink
Merge pull request #298 from ncss-tech/sdaquery-errors1
Browse files Browse the repository at this point in the history
Fix some SDA-related errors and formatting in WCS vignette
  • Loading branch information
brownag authored Jul 27, 2023
2 parents 03de2d7 + 6fa1364 commit 0ca0ff1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
27 changes: 18 additions & 9 deletions R/SDA_query.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,27 @@ SDA_query <- function(q) {

# error message is encapsulated in XML, use xml2 library functions to extract
if (inherits(request.status, 'try-error')) {

# get the request response, this will contain an error message
r.content <- httr::content(r, as = 'parsed', encoding = 'UTF-8')

# parse the XML to get the error message
error.msg <- xml2::xml_text(r.content)

## message about bad result (previously error/warning)
message(error.msg)

r.content <- try(httr::content(r, as = 'parsed', encoding = 'UTF-8'), silent = TRUE)

if (!inherits(r.content, 'try-error')) {

# parse the XML to get the error message
error.msg <- try(xml2::xml_text(r.content), silent = TRUE)

if (inherits(error.msg, 'try-error')) {
error.msg <- "Unable to parse error message from XML response"
}

## message about bad result (previously error/warning)
message(error.msg)

}

# return the error object so calling function/user can handle it
return(invisible(request.status))

}

# the result is JSON:
Expand Down
5 changes: 4 additions & 1 deletion R/get_SDA_muaggatt.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ get_SDA_muaggatt <- function(areasymbols = NULL, mukeys = NULL, WHERE = NULL, qu
WHERE <- paste("legend.areasymbol IN", format_SQL_in_statement(areasymbols))
}

q <- sprintf("SELECT mapunit.mukey, muaggatt.*
q <- sprintf("SELECT muaggatt.mukey, muaggatt.*
FROM legend
INNER JOIN mapunit ON mapunit.lkey = legend.lkey
INNER JOIN muaggatt ON mapunit.mukey = muaggatt.mukey
Expand All @@ -51,6 +51,9 @@ get_SDA_muaggatt <- function(areasymbols = NULL, mukeys = NULL, WHERE = NULL, qu
warnings()
stop(attr(res, 'condition'))
}

# remove duplicated mukey column
res[[ncol(res)]] <- NULL

return(res)
}
15 changes: 10 additions & 5 deletions vignettes/wcs-ssurgo.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Map Unit Key Grids and Thematic Maps of SSURGO Data"
title: "Map Unit Key Grids and Thematic Maps of Soil Survey Geographic (SSURGO) Data"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Map Unit Key Grids and Thematic Maps of SSURGO Data}
Expand All @@ -26,7 +26,7 @@ options(width = 100, stringsAsFactors = FALSE)
```


This vignette will explore how to use the soilDB package to create thematic maps of SSURGO data via Soil Data Access and SoilWeb coverage services.
This vignette will explore how to use the soilDB package to create thematic maps of the Soil Survey Geographic Database (SSURGO) via Soil Data Access (SDA) and SoilWeb coverage services.

# Introduction

Expand Down Expand Up @@ -527,6 +527,7 @@ aws <- catalyze(mu2)
plot(
aws,
axes = FALSE,
cex.main = 0.7,
main = c(
'Plant Available Water Storage (cm)\nWeighted Mean over Components, 0-50cm',
'Plant Available Water Storage (cm)\nWeighted Mean over Components, 0-100cm'
Expand Down Expand Up @@ -576,6 +577,7 @@ tab$class_ENGConstructionMaterialsRoadfill <- factor(
par(mar = c(4, 12, 3, 3))
boxplot(
rating_ENGConstructionMaterialsRoadfill ~ class_ENGConstructionMaterialsRoadfill,
cex.main = 0.7,
main = 'ENG - Construction Materials; Roadfill',
ylab = "",
data = tab,
Expand Down Expand Up @@ -603,6 +605,7 @@ rating <- catalyze(mu2)
plot(
rating,
axes = FALSE,
cex.main = 0.7,
main = c(
'Construction Materials; Roadfill\nWeighted Mean over Components',
'Irrigation Disposal of Wastewater\nWeighted Mean over Components'
Expand Down Expand Up @@ -665,6 +668,7 @@ plot(
mu,
legend = FALSE,
axes = FALSE,
cex.main = 0.7,
main = 'gSSURGO Map Unit Key Grid'
)
```
Expand Down Expand Up @@ -759,13 +763,13 @@ box()
Plot aggregate soil properties.

```{r}
plot(mu2[['dbthirdbar_r']],
plot(mu2[['dbthirdbar_r']], cex.main = 0.7,
main = '1/3 Bar Bulk Density (g/cm^3)\nDominant Component\n0-25cm')
plot(mu2[['awc_r']],
plot(mu2[['awc_r']], cex.main = 0.7,
main = 'AWC (cm/cm)\nDominant Component\n0-25cm')
plot(mu2[['ph1to1h2o_r']],
plot(mu2[['ph1to1h2o_r']], cex.main = 0.7,
main = 'pH 1:1 H2O\nDominant Component\n0-25cm')
```

Expand Down Expand Up @@ -836,6 +840,7 @@ r <- c(ssc, texture.class)
# graphical check
plot(
r,
cex.main = 0.7,
main = paste0(names(r), " - 25-50cm\nDominant Component")
)
```

0 comments on commit 0ca0ff1

Please sign in to comment.