Skip to content

Commit

Permalink
all examples that require internet now use curl::has_internet() as pa…
Browse files Browse the repository at this point in the history
…rt of their "dependencies" for running
  • Loading branch information
brownag committed Jan 25, 2020
1 parent c2181d8 commit 1bfb235
Show file tree
Hide file tree
Showing 17 changed files with 933 additions and 884 deletions.
36 changes: 20 additions & 16 deletions R/fetchSDA_spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@
#' @param x A vector of MUKEYs or national mapunit symbols.
#' @param by.col Column name containing mapunit identifier ("mukey" or "nmusym"); default: "mukey"
#' @param method geometry result type: 'feature' returns polygons, 'bbox' returns the bounding box of each polygon, and 'point' returns a single point within each polygon.
#' @param add.fields Column names from `mapunit` table to add to result. Must specify table name prefix as either `G` or `mapunit`.
#' @param chunk.size How many queries should spatial request be divided into? Necessary for large extents. Default: 10
#' @return A SpatialPolygonsDataFrame corresponding to SDA spatial data for all MUKEYs / nmusyms requested. Default result contains MupolygonWktWgs84-derived geometry with attribute table containing `gid`, `mukey` and `nationalmusym`, additional fields in result are specified with `add.fields`.
#' @param add.fields Column names from `mapunit` table to add to result. Must specify table name prefix `mapunit` before column name (e.g. `mapunit.muname`).
#' @param chunk.size How many queries should spatial request be divided into? Necessary for large results. Default: 10
#' @return A Spatial*DataFrame corresponding to SDA spatial data for all MUKEYs / nmusyms requested. Default result contains mapunit delineation geometry with attribute table containing `gid`, `mukey` and `nationalmusym`, plus additional fields in result specified with `add.fields`.
#' @author Andrew G. Brown.
#' @examples
#' \donttest{
#' # get spatial data for a single mukey
#' single.mukey <- fetchSDA_spatial(x = "2924882")
#'
#' # demonstrate fetching full extent (multi-mukey) of national musym
#' full.extent.nmusym <- fetchSDA_spatial(x = "2x8l5", by = "nmusym")
#'
#' # compare extent of nmusym to single mukey within it
#' if(require(sp)) {
#' plot(full.extent.nmusym, col = "RED",border=0)
#' plot(single.mukey, add = TRUE, col = "BLUE", border=0)
#' if(requireNamespace("curl") &
#' curl::has_internet()) {
#'
#' # get spatial data for a single mukey
#' single.mukey <- fetchSDA_spatial(x = "2924882")
#'
#' # demonstrate fetching full extent (multi-mukey) of national musym
#' full.extent.nmusym <- fetchSDA_spatial(x = "2x8l5", by = "nmusym")
#'
#' # compare extent of nmusym to single mukey within it
#' if(require(sp)) {
#' plot(full.extent.nmusym, col = "RED",border=0)
#' plot(single.mukey, add = TRUE, col = "BLUE", border=0)
#' }
#'
#' # demo adding a field (`muname`) to attribute table of result
#' head(fetchSDA_spatial(x = "2x8l5", by="nmusym", add.fields="muname"))
#' }
#'
#' # demo adding a field (`muname`) to attribute table of result
#' head(fetchSDA_spatial(x = "2x8l5", by="nmusym", add.fields="muname"))
#' }
#' @rdname fetchSDA_spatial
#' @export fetchSDA_spatial
Expand Down
7 changes: 6 additions & 1 deletion man/OSDquery.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@

\examples{
\donttest{
if(require(aqp)) {
if(requireNamespace("curl") &
curl::has_internet() &
require(aqp)) {

# find all series that list Pardee as a geographically associated soil.
s <- OSDquery(geog_assoc_soils = 'pardee')

# get data for these series
x <- fetchOSD(s$series, extended = TRUE, colorState = 'dry')

# simple figure
par(mar=c(0,0,1,1))
plot(x$SPC)
Expand Down
191 changes: 97 additions & 94 deletions man/SDA_query.Rd
Original file line number Diff line number Diff line change
@@ -1,94 +1,97 @@
\name{SDA_query}
\alias{SDA_query}
\alias{makeChunks}
\alias{format_SQL_in_statement}

\title{Soil Data Access Query}
\description{Submit a query to the Soil Data Acccess (SDA) website in SQL, get the results as a dataframe.}

\usage{
SDA_query(q)
makeChunks(ids, size=100)
format_SQL_in_statement(x)
}

\arguments{
\item{q}{a valid T-SQL query surrounded by double quotes}
\item{ids}{vector of IDs for chunking, contents aren't used just length}
\item{size}{target chunk size}
\item{x}{character vector to be packed into an SQL `IN` statement}
}
\details{The SDA website can be found at \url{http://sdmdataaccess.nrcs.usda.gov} and query examples can be found at \url{http://sdmdataaccess.nrcs.usda.gov/QueryHelp.aspx}. A library of query examples can be found at \url{https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=SDA-SQL_Library_Home}.
SSURGO (detailed soil survey) and STATSGO (generalized soil survey) data are stored together within SDA. This means that queries that don't specify an area symbol may result in a mixture of SSURGO and STATSGO records. See the examples below and the \href{http://ncss-tech.github.io/AQP/soilDB/SDA-tutorial.html}{SDA Tutorial} for details.
}

\value{A dataframe containing the results. NULL is retutned when queries result in 0 matches rows.}
\author{D.E. Beaudette}
\note{This function requires the `httr`, `jsonlite`, and `XML` packages}

\seealso{\code{\link{mapunit_geom_by_ll_bbox}}}

\examples{
\donttest{

## get SSURGO export date for all soil survey areas in California
# there is no need to filter STATSGO
# because we are filtering on SSURGO areasymbols
q <- "SELECT areasymbol, saverest FROM sacatalog WHERE areasymbol LIKE 'CA\%';"
x <- SDA_query(q)
head(x)


## get SSURGO component data associated with the
## Amador series / major component only
# this query must explicitly filter out STATSGO data
q <- "SELECT cokey, compname, comppct_r FROM legend\n
INNER JOIN mapunit mu ON mu.lkey = legend.lkey\n
INNER JOIN component co ON mu.mukey = co.mukey\n
WHERE legend.areasymbol != 'US' AND compname = 'Amador';"

res <- SDA_query(q)
str(res)


## get component-level data for a specific soil survey area (Yolo county, CA)
# there is no need to filter STATSGO because the query contains
# an implicit selection of SSURGO data by areasymbol
q <- "SELECT \n
component.mukey, cokey, comppct_r, compname, taxclname, \n
taxorder, taxsuborder, taxgrtgroup, taxsubgrp \n
FROM legend \n
INNER JOIN mapunit ON mapunit.lkey = legend.lkey \n
LEFT OUTER JOIN component ON component.mukey = mapunit.mukey \n
WHERE legend.areasymbol = 'CA113' ;"

res <- SDA_query(q)
str(res)


## get tabular data based on result from spatial query
# there is no need to filter STATSGO because
# SDA_Get_Mukey_from_intersection_with_WktWgs84() implies SSURGO
#
# requires raster and rgeos packages because raster is suggested
# and rgeos is additional
if(require(raster) & require(rgeos)) {
# text -> bbox -> WKT
# xmin, xmax, ymin, ymax
b <- c(-120.9, -120.8, 37.7, 37.8)
p <- writeWKT(as(extent(b), 'SpatialPolygons'))
q <- paste0("SELECT mukey, cokey, compname, comppct_r FROM component \n
WHERE mukey IN (SELECT DISTINCT mukey FROM\n
SDA_Get_Mukey_from_intersection_with_WktWgs84('"
, p, "')) ORDER BY mukey, cokey, comppct_r DESC")

x <- SDA_query(q)
str(x)
}
}
}

\keyword{manip}

\name{SDA_query}
\alias{SDA_query}
\alias{makeChunks}
\alias{format_SQL_in_statement}

\title{Soil Data Access Query}
\description{Submit a query to the Soil Data Acccess (SDA) website in SQL, get the results as a dataframe.}

\usage{
SDA_query(q)
makeChunks(ids, size=100)
format_SQL_in_statement(x)
}

\arguments{
\item{q}{a valid T-SQL query surrounded by double quotes}
\item{ids}{vector of IDs for chunking, contents aren't used just length}
\item{size}{target chunk size}
\item{x}{character vector to be packed into an SQL `IN` statement}
}
\details{The SDA website can be found at \url{http://sdmdataaccess.nrcs.usda.gov} and query examples can be found at \url{http://sdmdataaccess.nrcs.usda.gov/QueryHelp.aspx}. A library of query examples can be found at \url{https://nasis.sc.egov.usda.gov/NasisReportsWebSite/limsreport.aspx?report_name=SDA-SQL_Library_Home}.
SSURGO (detailed soil survey) and STATSGO (generalized soil survey) data are stored together within SDA. This means that queries that don't specify an area symbol may result in a mixture of SSURGO and STATSGO records. See the examples below and the \href{http://ncss-tech.github.io/AQP/soilDB/SDA-tutorial.html}{SDA Tutorial} for details.
}

\value{A dataframe containing the results. NULL is retutned when queries result in 0 matches rows.}
\author{D.E. Beaudette}
\note{This function requires the `httr`, `jsonlite`, and `XML` packages}

\seealso{\code{\link{mapunit_geom_by_ll_bbox}}}

\examples{
\donttest{
if(requireNamespace("curl") &
curl::has_internet()) {

## get SSURGO export date for all soil survey areas in California
# there is no need to filter STATSGO
# because we are filtering on SSURGO areasymbols
q <- "SELECT areasymbol, saverest FROM sacatalog WHERE areasymbol LIKE 'CA\%';"
x <- SDA_query(q)
head(x)


## get SSURGO component data associated with the
## Amador series / major component only
# this query must explicitly filter out STATSGO data
q <- "SELECT cokey, compname, comppct_r FROM legend\n
INNER JOIN mapunit mu ON mu.lkey = legend.lkey\n
INNER JOIN component co ON mu.mukey = co.mukey\n
WHERE legend.areasymbol != 'US' AND compname = 'Amador';"

res <- SDA_query(q)
str(res)


## get component-level data for a specific soil survey area (Yolo county, CA)
# there is no need to filter STATSGO because the query contains
# an implicit selection of SSURGO data by areasymbol
q <- "SELECT \n
component.mukey, cokey, comppct_r, compname, taxclname, \n
taxorder, taxsuborder, taxgrtgroup, taxsubgrp \n
FROM legend \n
INNER JOIN mapunit ON mapunit.lkey = legend.lkey \n
LEFT OUTER JOIN component ON component.mukey = mapunit.mukey \n
WHERE legend.areasymbol = 'CA113' ;"

res <- SDA_query(q)
str(res)


## get tabular data based on result from spatial query
# there is no need to filter STATSGO because
# SDA_Get_Mukey_from_intersection_with_WktWgs84() implies SSURGO
#
# requires raster and rgeos packages because raster is suggested
# and rgeos is additional
if(require(raster) & require(rgeos)) {
# text -> bbox -> WKT
# xmin, xmax, ymin, ymax
b <- c(-120.9, -120.8, 37.7, 37.8)
p <- writeWKT(as(extent(b), 'SpatialPolygons'))
q <- paste0("SELECT mukey, cokey, compname, comppct_r FROM component \n
WHERE mukey IN (SELECT DISTINCT mukey FROM\n
SDA_Get_Mukey_from_intersection_with_WktWgs84('"
, p, "')) ORDER BY mukey, cokey, comppct_r DESC")

x <- SDA_query(q)
str(x)
}
}
}
}

\keyword{manip}

78 changes: 41 additions & 37 deletions man/SSURGO_spatial_query.Rd
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
\name{SoilWeb_spatial_query}
\alias{SoilWeb_spatial_query}

\title{Get SSURGO Data via Spatial Query}

\description{Get SSURGO Data via Spatial Query to SoilWeb}
\usage{
SoilWeb_spatial_query(bbox = NULL, coords = NULL, what = "mapunit", source = "soilweb")
}

\arguments{
\item{bbox}{a bounding box in WGS84 geographic coordinates, see examples}
\item{coords}{a coordinate pair in WGS84 geographic coordinates, see examples}
\item{what}{data to query, currently ignored}
\item{source}{the data source, currently ignored}
}

\note{This function should be considered experimental; arguments, results, and side-effects could change at any time. SDA now supports spatial queries, consider using \code{\link{SDA_query_features}} instead.}

\details{Data are currently available from SoilWeb. These data are a snapshot of the "official" data. The snapshot date is encoded in the "soilweb_last_update" column in the function return value. Planned updates to this function will include a switch to determine the data source: "official" data via USDA-NRCS servers, or a "snapshot" via SoilWeb.}

\value{The data returned from this function will depend on the query style. See examples below.}

\author{D.E. Beaudette}

\examples{
\donttest{
# query by bbox
SoilWeb_spatial_query(bbox=c(-122.05, 37, -122, 37.05))

# query by coordinate pair
SoilWeb_spatial_query(coords=c(-121, 38))
}
}

\keyword{manip}

\name{SoilWeb_spatial_query}
\alias{SoilWeb_spatial_query}

\title{Get SSURGO Data via Spatial Query}

\description{Get SSURGO Data via Spatial Query to SoilWeb}
\usage{
SoilWeb_spatial_query(bbox = NULL, coords = NULL, what = "mapunit", source = "soilweb")
}

\arguments{
\item{bbox}{a bounding box in WGS84 geographic coordinates, see examples}
\item{coords}{a coordinate pair in WGS84 geographic coordinates, see examples}
\item{what}{data to query, currently ignored}
\item{source}{the data source, currently ignored}
}

\note{This function should be considered experimental; arguments, results, and side-effects could change at any time. SDA now supports spatial queries, consider using \code{\link{SDA_query_features}} instead.}

\details{Data are currently available from SoilWeb. These data are a snapshot of the "official" data. The snapshot date is encoded in the "soilweb_last_update" column in the function return value. Planned updates to this function will include a switch to determine the data source: "official" data via USDA-NRCS servers, or a "snapshot" via SoilWeb.}

\value{The data returned from this function will depend on the query style. See examples below.}

\author{D.E. Beaudette}

\examples{
\donttest{
if(requireNamespace("curl") &
curl::has_internet()) {

# query by bbox
SoilWeb_spatial_query(bbox=c(-122.05, 37, -122, 37.05))

# query by coordinate pair
SoilWeb_spatial_query(coords=c(-121, 38))
}
}
}

\keyword{manip}

Loading

0 comments on commit 1bfb235

Please sign in to comment.