Installing and loading StreamCatTools
To install, currently you need to install from GitHub using devtools
library(devtools)
install_github('USEPA/StreamCatTools')
After installing load the library
Background
The StreamCatTools
package was designed to simplify the
use of StreamCat
data in R, leveraging the new API for
StreamCat.
StreamCat API
We can actually pull data into R from the StreamCat
API by simply using the read_csv
function from the
readr
package. We have to hard-wire parameters and are
limited in the number of records returned through a GET
request.
df <- readr::read_csv("https://java.epa.gov/StreamCAT/metrics?name=fert&areaOfInterest=catchment&comid=179", show_col_types = FALSE)
knitr::kable(df)
COMID | WSAREASQKM | CATAREASQKM | FERTCAT |
---|---|---|---|
179 | 3.555 | 3.555 | 1.438 |
List API parameters
List StreamCat parameters: Get a list of available StreamCat values
for certain parameters using the sc_get_params
function
(right now just metric names and areas of interest for this function)
via the API
region_params <- sc_get_params(param='areaOfInterest')
name_params <- sc_get_params(param='name')
print(paste0('region parameters are: ', paste(region_params,collapse = ', ')))
#> [1] "region parameters are: catchment, other, riparian_catchment, riparian_watershed, watershed"
print(paste0('A selection of available StreamCat metrics include: ',paste(name_params[1:10],collapse = ', ')))
#> [1] "A selection of available StreamCat metrics include: agkffact, al2o3, bankfull_depth_m, bankfull_width_m, bfi, canaldens, cao, cbnf, chem_v2_1, clay"
Look up the display name or names for a metric using the
sc_fullname
function via the API
metric='pcthbwet2011'
fullname <- sc_fullname(metric)
fullname
#> [1] "Herbaceous Wetland Percentage 2011"
metric='pctdecid2019,fert'
fullname <- sc_fullname(metric)
fullname
#> [1] "Deciduous Forest Percentage 2019"
#> [2] "Synthetic Nitrogen Fertilizer Application Mean Rate"
Get data for COMIDs
In this example we access several variables, for several areas of
interest, and for several COMIDs using the sc_get_data
function. Loads data into a tibble we can view.
df <- sc_get_data(metric='PctUrbMd2006,DamDens,TRIDens', aoi='riparian_catchment,catchment,watershed', comid='179,1337,1337420')
knitr::kable(df)
COMID | WSAREASQKM | WSAREASQKMRP100 | CATAREASQKM | CATAREASQKMRP100 | TRIDENSCATRP100 | PCTURBMD2006CATRP100 | TRIDENSCAT | TRIDENSWS | PCTURBMD2006CAT | PCTURBMD2006WS | DAMDENSCAT | DAMDENSWS |
---|---|---|---|---|---|---|---|---|---|---|---|---|
179 | 3.5550 | 0.5373 | 3.5550 | 0.5373 | 0 | 0 | 0 | 0 | 0.00 | 0.00 | 0 | 0.0000 |
1337 | 2.5803 | 0.2781 | 2.5803 | 0.2781 | 0 | 0 | 0 | 0 | 0.07 | 0.07 | 0 | 0.0000 |
1337420 | 60.8517 | 8.0343 | 3.4101 | 0.2925 | 0 | 0 | 0 | 0 | 0.00 | 0.01 | 0 | 0.0329 |
Get data for county
In this example we access a couple variables at the watershed scale
for the area of interest of a county (Benton County in this case) using
the sc_get_data
function.
df <- sc_get_data(metric='pctwdwet2006', aoi='watershed', county='41003')
knitr::kable(head(df))
COMID | FIPS | WSAREASQKM | PCTWDWET2006WS |
---|---|---|---|
23764519 | 41003 | 12.5856 | 0.09 |
23762751 | 41003 | 213.6753 | 1.35 |
23763039 | 41003 | 5.8509 | 1.52 |
23762873 | 41003 | 13.6341 | 0.46 |
23763313 | 41003 | 81.8478 | 0.16 |
23763023 | 41003 | 19.9863 | 1.08 |
Get NLCD data
In this example we access National Land Cover Dataset (NLCD) data for
2001, just at the catchment level for several COMIDs using the
sc_nlcd
function. Loads data into a tibble we can view.
COMID | WSAREASQKM | CATAREASQKM | PCTHBWET2001CAT | PCTURBMD2001CAT | PCTCONIF2001CAT | PCTDECID2001CAT | PCTICE2001CAT | PCTCROP2001CAT | PCTMXFST2001CAT | PCTOW2001CAT | PCTSHRB2001CAT | PCTURBOP2001CAT | PCTGRS2001CAT | PCTHAY2001CAT | PCTURBHI2001CAT | PCTBL2001CAT | PCTURBLO2001CAT | PCTWDWET2001CAT |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
179 | 3.5550 | 3.5550 | 1.95 | 0 | 4.89 | 3.82 | 0 | 1.52 | 48.76 | 0.0 | 0.73 | 0.00 | 0.23 | 1.22 | 0 | 0.00 | 0.00 | 36.89 |
1337 | 2.5803 | 2.5803 | 0.56 | 0 | 5.65 | 11.79 | 0 | 0.00 | 51.55 | 0.1 | 0.66 | 3.98 | 1.60 | 2.83 | 0 | 0.28 | 0.24 | 20.75 |
1337420 | 60.8517 | 3.4101 | 1.64 | 0 | 4.06 | 83.06 | 0 | 0.00 | 3.77 | 0.0 | 3.38 | 1.77 | 0.87 | 0.11 | 0 | 0.00 | 0.40 | 0.95 |
We can also pass a couple years for a different area of interest for another region like a county.
COMID | FIPS | WSAREASQKM | PCTSHRB2006WS | PCTURBOP2006WS | PCTURBMD2006WS | PCTCROP2006WS | PCTHAY2006WS | PCTGRS2006WS | PCTURBHI2006WS | PCTHBWET2006WS | PCTMXFST2006WS | PCTOW2006WS | PCTICE2006WS | PCTDECID2006WS | PCTWDWET2006WS | PCTBL2006WS | PCTURBLO2006WS | PCTCONIF2006WS | PCTOW2019WS | PCTBL2019WS | PCTWDWET2019WS | PCTURBLO2019WS | PCTDECID2019WS | PCTICE2019WS | PCTURBMD2019WS | PCTHBWET2019WS | PCTCONIF2019WS | PCTURBOP2019WS | PCTSHRB2019WS | PCTCROP2019WS | PCTHAY2019WS | PCTMXFST2019WS | PCTGRS2019WS | PCTURBHI2019WS |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
23764519 | 41003 | 12.5856 | 14.65 | 3.82 | 0.00 | 0.00 | 0.69 | 8.00 | 0.00 | 0.19 | 16.09 | 0.16 | 0 | 0.54 | 0.09 | 0.00 | 0.04 | 55.74 | 0.16 | 0.00 | 0.09 | 0.04 | 0.86 | 0 | 0.01 | 0.19 | 65.62 | 3.81 | 11.82 | 0.00 | 0.76 | 14.05 | 2.60 | 0.00 |
23762751 | 41003 | 213.6753 | 25.75 | 5.11 | 0.05 | 0.04 | 8.21 | 7.31 | 0.00 | 0.39 | 17.17 | 0.04 | 0 | 0.97 | 1.35 | 0.00 | 0.42 | 33.16 | 0.04 | 0.00 | 1.45 | 0.45 | 1.85 | 0 | 0.09 | 0.30 | 54.23 | 5.12 | 5.83 | 0.37 | 7.53 | 20.03 | 2.72 | 0.01 |
23763039 | 41003 | 5.8509 | 0.00 | 0.55 | 0.74 | 55.90 | 35.92 | 0.05 | 0.08 | 0.71 | 0.00 | 1.17 | 0 | 0.00 | 1.52 | 0.02 | 3.35 | 0.00 | 1.18 | 0.02 | 1.55 | 3.21 | 0.00 | 0 | 0.94 | 0.68 | 0.00 | 0.43 | 0.00 | 57.24 | 34.55 | 0.00 | 0.06 | 0.14 |
23762873 | 41003 | 13.6341 | 3.09 | 9.90 | 29.84 | 0.00 | 7.30 | 1.06 | 6.03 | 0.22 | 7.56 | 0.01 | 0 | 1.52 | 0.46 | 0.10 | 28.69 | 4.22 | 0.01 | 0.09 | 0.55 | 28.38 | 1.54 | 0 | 30.43 | 0.13 | 6.83 | 9.58 | 0.84 | 0.00 | 7.05 | 7.74 | 0.44 | 6.38 |
23763313 | 41003 | 81.8478 | 0.00 | 3.47 | 8.01 | 26.41 | 49.49 | 0.14 | 2.49 | 0.60 | 0.00 | 0.48 | 0 | 0.00 | 0.16 | 0.00 | 8.70 | 0.05 | 0.48 | 0.00 | 0.28 | 8.66 | 0.00 | 0 | 8.95 | 0.48 | 0.03 | 3.08 | 0.00 | 31.68 | 43.16 | 0.00 | 0.17 | 3.02 |
23763023 | 41003 | 19.9863 | 0.00 | 0.86 | 0.71 | 49.48 | 41.65 | 0.03 | 0.13 | 1.40 | 0.12 | 1.08 | 0 | 0.03 | 1.08 | 0.01 | 3.12 | 0.31 | 1.03 | 0.01 | 1.15 | 2.90 | 0.03 | 0 | 0.97 | 1.42 | 0.32 | 0.70 | 0.00 | 60.32 | 30.72 | 0.12 | 0.04 | 0.27 |
Get COMIDs
In this example we use the sc_get_comid
function to find
COMIDs for USGS stream gages we load into R. We use a .csv file with
coordinate columns and a known coordinate reference system. We’ll use an
example dataset of large dams in Oregon from the [Oregon Spatial Data
Library]
gages = readr::read_csv(system.file("extdata","Gages_flowdata.csv", package = "StreamCatTools"),show_col_types = FALSE)
# we'll just grab a few variables to keep things simple
gages <- gages[,c('SOURCE_FEA','STATION_NM','LON_SITE','LAT_SITE')]
gages_coms <- sc_get_comid(gages, xcoord='LON_SITE', ycoord='LAT_SITE', crsys=4269)
# Add the COMID we found back to gages data frame
gages$COMID <- strsplit(gages_coms, ",")[[1]]
df <- sc_get_data(metric='huden2010', aoi='watershed', comid=gages_coms)
df$COMID <- as.character(df$COMID)
gages <- dplyr::left_join(gages, df, by='COMID')
knitr::kable(head(gages))
Get data for a hydroregion
In this example we access a couple watershed-only metrics for a
particular NHDPlus hydroregion using the sc_get_data
function.
df <- sc_get_data(metric='PctWdWet2006', aoi='watershed', region='17')
knitr::kable(head(df))
COMID | REGIONID | WSAREASQKM | PCTWDWET2006WS |
---|---|---|---|
22988611 | Region17 | 2194.9371 | 0.99 |
24114311 | Region17 | 441.9549 | 0.29 |
24114309 | Region17 | 459.0153 | 0.27 |
24116239 | Region17 | 13.3479 | 0.42 |
22988639 | Region17 | 50.2722 | 0.00 |
22988711 | Region17 | 42.4404 | 0.02 |