Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/USEPA/StreamCatTools
Browse files Browse the repository at this point in the history
  • Loading branch information
mhweber committed Jan 17, 2024
2 parents 7d9be2f + a9c568b commit ce390d3
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 77 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
^pkgdown$
.github
^LICENSE\.md$
^\.github$
75 changes: 20 additions & 55 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches:
- main
- master
branches: [main, master]
pull_request:
branches:
- main
- master
branches: [main, master]

name: R-CMD-check

Expand All @@ -22,63 +18,32 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest", http-user-agent: "R/4.1.0 (ubuntu-20.04) R (4.1.0 x86_64-pc-linux-gnu x86_64 linux-gnu) on GitHub Actions" }
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v1
- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-pandoc@v1

- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}

- name: Restore R package cache
uses: actions/cache@v2
- uses: r-lib/actions/setup-r-dependencies@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
shell: Rscript {0}

- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: |
options(crayon.enabled = TRUE)
rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}
extra-packages: any::rcmdcheck
needs: check

- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
- uses: r-lib/actions/check-r-package@v2
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
upload-snapshots: true
203 changes: 203 additions & 0 deletions R/lc_get_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#' @title Get LakeCat data
#'
#' @description
#' Function to return LakeCat metrics using the StreamCat API. The function allows a user to get
#' specific metric data aggregated by area of interest, returned by comid(s), hydroregion(s), state(s), or county(ies).
#'
#' @author
#' Marc Weber
#'
#' @param metric Name(s) of metrics to query
#' Syntax: name=<name1>,<name2>
#'
#' @param aoi Specify the area of interest described by a metric. By default, all available areas of interest
#' for a given metric are returned.
#' Syntax: areaOfInterest=<value1>,<value2>
#' Values: catchment|watershed|
#'
#' @param comid Return metric information for specific COMIDs. Needs to be a character string
#' and function will convert to this format if needed.
#' Syntax: comid=<comid1>,<comid2>
#'
#' @param showAreaSqKm Return the area in square kilometers of a given area of interest.
#' The default value is false.
#' Values: true|false
#'
#' @param showPctFull Return the pctfull for each dataset. The default value is false.
#' Values: true|false
#'
#' @param countOnly Return a CSV containing only the row count (ROWCOUNT) and the column
#' count (COLUMNCOUNT) that the server expects to return in a request. The default value is false.
#' Values: true|false
#'
#' @return A tibble of desired StreamCat metrics
#'
#' @examples
#' \donttest{
#' df <- lc_get_data(comid='23794487', aoi='catchment', metric='fert')
#'
#' df <- lc_get_data(metric='PctUrbMd2006', aoi='watershed',
#' comid='24083377')
#'
#' df <- lc_get_data(metric='PctUrbMd2006', aoi='watershed',
#' comid='24083377', showAreaSqKm=FALSE, showPctFull=TRUE)
#'
#' df <- lc_get_data(metric='PctUrbMd2006,DamDens',
#' aoi='catchment,watershed', comid='23783629,23794487,23812618')
#'
#' df <- lc_get_data(metric='PctUrbMd2006,DamDens',
#' aoi='catchment,watershed', comid='23783629,23794487,23812618',
#' countOnly=TRUE)
#'
#' }
#' @export

lc_get_data <- function(metric = NULL,
aoi = NULL,
comid = NULL,
showAreaSqKm = NULL,
showPctFull = NULL,
conus = NULL,
countOnly = NULL) {
# Base API URL.
req <- httr2::request("https://java.epa.gov/StreamCAT/LakeCat/metrics?")
# Collapse comids into a single string separated by a comma.
if (!is.null(comid))
comid <- paste(comid, collapse = ",")
# Create the query based on user inputs.
# req_url_query silently ignores NULLs.
query <- httr2::req_url_query(
.req = req,
name = metric,
comid = comid,
areaOfInterest = aoi,
showAreaSqKm = showAreaSqKm,
showPctFull = showPctFull,
conus = conus,
countOnly = countOnly
)
# Send HTTP request
resp <- httr2::req_perform(req = query)
# Extract the body of the response.
resp_body <- httr2::resp_body_string(resp, encoding = "UTF-8")
# Transform the string response into a data frame.
final_df <- utils::read.csv(text = resp_body,
fileEncoding = "UTF8")
# End of function. Return a data frame.
return(final_df)
}

#' @title Get NLCD Data
#'
#' @description
#' Function to specifically retrieve all NLCD metrics for a given year using the StreamCat API.
#'
#' @author
#' Marc Weber
#'
#' @param year Years(s) of NLCD metrics to query.
#' Only valid NLCD years are accepted (i.e. 2001, 2004, 2006, 2008,
#' 2011, 2013, 2016, 2019)
#' Syntax: year=<year1>,<year2>
#'
#' @param aoi Specify the area of interest described by a metric. By default, all available areas of interest
#' for a given metric are returned.
#' Syntax: areaOfInterest=<value1>,<value2>
#' Values: catchment|watershed|riparian_catchment|riparian_watershed|other
#'
#' @param comid Return metric information for specific COMIDs
#' Syntax: comid=<comid1>,<comid2>
#'
#' @param showAreaSqKm Return the area in square kilometers of a given area of interest.
#' The default value is false.
#' Values: true|false
#'
#' @param showPctFull Return the pctfull for each dataset. The default value is false.
#' Values: true|false
#'
#' @param countOnly Return a CSV containing only the row count (ROWCOUNT) and the column
#' count (COLUMNCOUNT) that the server expects to return in a request. The default value is false.
#' Values: true|false
#'
#' @return A tibble of desired StreamCat metrics
#'
#' @examples
#' \donttest{
#'
#' df <- lc_nlcd(comid='23783629', year='2019', aoi='watershed')
#'
#' df <- lc_nlcd(year='2016', aoi='catchment',
#' comid='23783629,23794487,23812618', showAreaSqKm=FALSE, showPctFull=TRUE)
#'
#' df <- lc_nlcd(year='2016', aoi='catchment',
#' comid='23783629,23794487,23812618', countOnly=TRUE)
#'
#' df <- lc_nlcd(year='2016, 2019', aoi='catchment,watershed',
#' comid='23783629,23794487,23812618')
#' }
#' @export


lc_nlcd <- function(year = '2019', aoi = NULL, comid = NULL,
showAreaSqKm = NULL, showPctFull = NULL,
countOnly = NULL) {
# year must be a character string.
year_chr <- as.character(year)
# split multiple years supplied as a single string into
# a vector of years.
year_vec <- unlist(strsplit(x = year_chr,
split = ",|, "))
# Vector of valid NLCD years to check inputs against.
valid_years <- c('2001',
'2004',
'2006',
'2008',
'2011',
'2013',
'2016',
'2019')
# Stop early if any of the year(s) supplied are not found in the valid
# years vec.
stopifnot(
"year must be a valid NLCD land cover year: 2001, 2004,
2006, 2008, 2011, 2013, or 2019" = any(year_vec %in% valid_years)
)
# Vector of NLCD metric names.
nlcd <- c(
'PctMxFst',
'PctOw',
'PctShrb',
'PctUrbHi',
'PctUrbLo',
'PctUrbMd',
'PctUrbOp',
'PctWdWet',
'PctBl',
'PctConif',
'PctCrop',
'PctDecid',
'PctGrs',
'PctHay',
'PctHbWet',
'PctIce'
)
# Create a data frame of all NLCD Metric and year combinations.
all_comb <- expand.grid(nlcd, year_vec)
# Concatenate the NLCD metric name with the supplied year(s) to create
# valid metric names to submit to the API.
nlcd_mets <- paste0(all_comb$Var1,
all_comb$Var2,
collapse = ",",
recycle0 = TRUE)
# Query the API.
final_df <- lc_get_data(
metric = nlcd_mets,
aoi = aoi,
comid = comid,
showAreaSqKm = showAreaSqKm,
showPctFull = showPctFull,
countOnly = countOnly
)
# End of function. Return a data frame.
return(final_df)
}
50 changes: 50 additions & 0 deletions R/lc_get_params.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' @title Get LakeCat Parameters
#'
#' @description
#' Function to return available LakeCat parameters using the StreamCat API.
#'
#' @author
#' Marc Weber
#'
#' @param param Either name or area to grab JSON of parameters in API
#' Syntax: param=<value1>,<value2>
#' Values: name|area
#'
#' @return A list of all the current LakeCat values for a given parameter
#' @export
#'
#' @examples
#' params <- lc_get_params(param='name')
#' params <- lc_get_params(param='areaOfInterest')

lc_get_params <- function(param = NULL) {
resp <- jsonlite::fromJSON("https://java.epa.gov/StreamCAT/LakeCat/metrics")
if (param=='areaOfInterest') params <- resp$parameters$areaOfInterest$options else{
params <- resp$parameters$name$options
}
return(params)
}

#' @title Lookup Full Metric Name
#'
#' @description
#' Function to retrieve a full metric name based on the short name using the LakeCat API.
#'
#' @author
#' Marc Weber
#'
#' @param metric Short metric name
#' Syntax: metric=value1
#' Values: metric
#'
#' @return A lookup of the full name for a given LakeCat metric
#' @export
#'
#' @examples
#' fullname <- lc_fullname(metric='name')

lc_fullname <- function(metric = NULL) {
resp <- as.data.frame(jsonlite::fromJSON("https://java.epa.gov/StreamCAT/LakeCat/metrics/datadictionary"))
result <- resp[resp$dictionary.metric_prefix==metric,1]
return(result)
}
16 changes: 15 additions & 1 deletion R/sc_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
#'
#' df <- sc_get_data(metric='PctUrbMd2006,DamDens',
#' aoi='catchment,watershed', comid='179,1337,1337420')
#'
#' df <- sc_get_data(metric='PctUrbMd2006,DamDens',
#' aoi='catchment,watershed', comid='179,1337,1337420',
#' showAreaSqKm=FALSE, showPctFull=TRUE)
#'
#' df <- sc_get_data(metric='PctUrbMd2006,DamDens',
#' aoi='catchment,watershed', comid='179,1337,1337420', countOnly=TRUE)
#'
#' }
#' @export

Expand Down Expand Up @@ -155,7 +163,13 @@ sc_get_data <- function(metric = NULL,
#' \donttest{
#' df <- sc_nlcd(year='2001', aoi='catchment',comid='179,1337,1337420')
#'
#' df <- sc_nlcd(year='2001', aoi='watershed', region='01')
#' df <- sc_nlcd(comid='1337420', year='2001', aoi='watershed', region='01')
#'
#' df <- sc_nlcd(year='2001', aoi='watershed', region='01',
#' countOnly=TRUE)
#'
#' df <- sc_nlcd(year='2001', aoi='watershed', region='01',
#' showAreaSqKm=FALSE, showPctFull=TRUE)
#'
#' df <- sc_nlcd(year='2001, 2006', aoi='catchment,watershed',
#' comid='179,1337,1337420')
Expand Down
Loading

0 comments on commit ce390d3

Please sign in to comment.