Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions R/ContinuousDataFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @description
#' Retrieves available metadata from USGS National Water Information System (NWIS) based on
#' different spatial queries: area of interest (AOI), specific sites, or state boundaries.
#' Returns a spatial sf object containing continuous monitoring site information and
#' Returns a spatial sf object containing continuous monitoring site information and
#' available parameters and statistics.
#' If no data is found, returns an empty sf object with appropriate column structure.
#'
Expand All @@ -15,7 +15,7 @@
#' @param statecode Character vector of two-letter state codes (e.g., c("CA", "OR")).
#' @param siteid Character vector of USGS site numbers.
#'
#' @return An sf object containing NWIS continuous monitoring site summary
#' @return An sf object containing NWIS continuous monitoring site summary
#' information including:
#' \itemize{
#' \item site_no: USGS site identification number
Expand Down Expand Up @@ -129,22 +129,20 @@ TADA_listNWIS <- function(aoi_sf = "null", statecode = "null", siteid = "null")
}

# Daily stats info grabber:

stats_table <- function() {

site_url <- "https://help.waterdata.usgs.gov/stat_code"

table <- rvest::read_html(site_url) %>%
rvest::html_nodes("table") %>%
rvest::html_table() %>%
.[[1]] %>%
dplyr::mutate(stat_cd = sprintf("%05d", `Statistic Type Code`)) %>%
dplyr::select(stat_cd, stat_type = `Statistic Type Description`)

return(table)

}

# Grab NWIS by an area of interest:
if ((unlist(aoi_sf)[1] != "null")) {
og_epsg <- sf::st_crs(aoi_sf)$epsg
Expand Down Expand Up @@ -388,7 +386,7 @@ TADA_listNWIS <- function(aoi_sf = "null", statecode = "null", siteid = "null")
) %>%
# Remove any duplicates if they exist (precautionary - they shouldn't!)
dplyr::distinct(., .keep_all = TRUE)

# If no data, return empty data frame
if (nrow(inventory) == 0) {
message("No daily USGS-NWIS data in specified query.")
Expand All @@ -401,7 +399,7 @@ TADA_listNWIS <- function(aoi_sf = "null", statecode = "null", siteid = "null")
#' Retrieve and tidy daily values from NWIS
#'
#' This function interfaces with the USGS National Water Information System (NWIS) to
#' retrieve daily values (DV) water quality data using the TADA (Tools for Automated
#' retrieve daily values (DV) water quality data using the TADA (Tools for Automated
#' Data Analysis) framework. Users can query data based on a spatial area of interest
#' (AOI), a vector of state abbreviations, or a vector of specific site ids, along
#' with relevant USGS parameter codes, statistics to return, and a date range.
Expand Down Expand Up @@ -434,16 +432,16 @@ TADA_listNWIS <- function(aoi_sf = "null", statecode = "null", siteid = "null")
#' dplyr::filter(NAME %in% c("Spokane", "Navajo Nation"))
#' sites_aoi_sf <- TADA_getNWIS(
#' aoi_sf = locs_sf,
#' parameter_codes =
#' c("00060", "00010"),
#' start_date = "2020-01-01",
#' end_date = "2020-01-31"
#' parameter_codes =
#' c("00060", "00010"),
#' start_date = "2020-01-01",
#' end_date = "2020-01-31"
#' )
#'
#' # Example 2: Query by specific site numbers
#' sites_specific <- TADA_getNWIS(
#' siteid = c("11530500", "11532500"),
#' parameter_codes = c("00060", "00010"),
#' parameter_codes = c("00060", "00010"),
#' start_date = "2020-01-01",
#' end_date = "2020-12-31"
#' )
Expand All @@ -452,7 +450,7 @@ TADA_listNWIS <- function(aoi_sf = "null", statecode = "null", siteid = "null")
#' nwis_data <- TADA_getNWIS(
#' statecode = c("RI", "CO"),
#' stat_codes = c("00001"),
#' parameter_codes = c("00010"),
#' parameter_codes = c("00010"),
#' start_date = "2020-01-01",
#' end_date = "2020-01-02"
#' )
Expand Down
20 changes: 20 additions & 0 deletions R/DataDiscoveryRetrieval.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
#' See https://www.waterqualitydata.us/Codes/project for options.
#' @param providers Leave blank to include all, or specify "STEWARDS", "STORET" (i.e., WQX), and/or
#' "NWIS". See https://www.waterqualitydata.us/Codes/providers for options.
#' @param bBox The latitude and longitude extent. Includes four numbers, e.g. bBox <- c(-xmin, ymin, -xmax, ymax).
#' @param maxrecs Maximum number of records to query at once (i.e., without breaking into smaller
#' queries).
#' @param ask A logical value (TRUE or FALSE) indicating whether the user should be asked for approval before
Expand Down Expand Up @@ -165,6 +166,9 @@
#' endDate = "2023-12-31",
#' ask = FALSE
#' )
#'
#' bbox <- c(-86.9736, 34.4883, -86.6135, 34.6562)
#' tada8 <- TADA_DataRetrieval(bBox = bbox)
#' }
#'
TADA_DataRetrieval <- function(startDate = "null",
Expand All @@ -184,6 +188,7 @@ TADA_DataRetrieval <- function(startDate = "null",
organization = "null",
project = "null",
providers = "null",
bBox = "null",
maxrecs = 350000,
ask = TRUE,
applyautoclean = TRUE) {
Expand Down Expand Up @@ -312,6 +317,14 @@ TADA_DataRetrieval <- function(startDate = "null",
} else if (providers != "null") {
WQPquery <- c(WQPquery, providers = providers)
}

# bbox
if (length(bBox) > 1) {
WQPquery <- c(WQPquery, bBox = list(bBox))
} else if (bBox != "null") {
WQPquery <- c(WQPquery, bBox = bBox)
}

# Organization
if (length(organization) > 1) {
WQPquery <- c(WQPquery, organization = list(organization))
Expand Down Expand Up @@ -744,6 +757,13 @@ TADA_DataRetrieval <- function(startDate = "null",
WQPquery <- c(WQPquery, providers = providers)
}

# bbox
if (length(bBox) > 1) {
WQPquery <- c(WQPquery, bBox = list(bBox))
} else if (bBox != "null") {
WQPquery <- c(WQPquery, bBox = bBox)
}

if (length(organization) > 1) {
WQPquery <- c(WQPquery, organization = list(organization))
} else if (organization != "null") {
Expand Down
11 changes: 11 additions & 0 deletions R/ExampleData.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,14 @@ NULL
#' @usage data(Data_HUC8_02070004_Mod1Output)
#' @format A data frame with 84 rows and 161 variables
NULL

#' cybertown
#'
#' See TADACybertown2025.Rmd in vignettes folder.
#'
#' @docType data
#' @keywords data frame
#' @name cybertown
#' @usage data(cybertown)
#' @format A data frame with 990 rows and 150 variables
NULL
98 changes: 53 additions & 45 deletions R/Figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ TADA_FlaggedSitesMap <- function(.data) {
#'
#' @param .data TADA dataframe after running TADA.FindNearbySites.
#' @param dist_buffer Distance in m to show a radius around each site marker.
#'
#'
#'
#' @return A leaflet map that shows all sites in the dataframe that contain
#' flagged data in the form of near other sites - groups of sites that are spatially located within
Expand All @@ -668,61 +668,69 @@ TADA_FlaggedSitesMap <- function(.data) {
#' }
#'
TADA_NearbySitesMap <- function(.data, dist_buffer = 100) {

if(c("TADA.NearbySiteGroup") %in% colnames(.data) == FALSE) {
if (c("TADA.NearbySiteGroup") %in% colnames(.data) == FALSE) {
.data <- TADA_FindNearbySites(.data)
}

.data <- .data %>%
dplyr::filter(!is.na(TADA.NearbySiteGroup)) %>%
dplyr::mutate(LatitudeMeasure = as.numeric(LatitudeMeasure),
LongitudeMeasure = as.numeric(LongitudeMeasure)) %>%
dplyr::select(LongitudeMeasure, LatitudeMeasure, TADA.MonitoringLocationIdentifier,
MonitoringLocationIdentifier, MonitoringLocationName, TADA.LatitudeMeasure,
TADA.LongitudeMeasure, OrganizationIdentifier, TADA.NearbySiteGroup) %>%
dplyr::distinct()

icon.colors <- grDevices::rainbow(as.numeric(length(unique(.data$TADA.NearbySiteGroup))))

pal <- leaflet::colorFactor(palette = icon.colors,
domain = .data$TADA.NearbySiteGroup)

dplyr::mutate(
LatitudeMeasure = as.numeric(LatitudeMeasure),
LongitudeMeasure = as.numeric(LongitudeMeasure)
) %>%
dplyr::select(
LongitudeMeasure, LatitudeMeasure, TADA.MonitoringLocationIdentifier,
MonitoringLocationIdentifier, MonitoringLocationName, TADA.LatitudeMeasure,
TADA.LongitudeMeasure, OrganizationIdentifier, TADA.NearbySiteGroup
) %>%
dplyr::distinct()

icon.colors <- grDevices::rainbow(as.numeric(length(unique(.data$TADA.NearbySiteGroup))))

pal <- leaflet::colorFactor(
palette = icon.colors,
domain = .data$TADA.NearbySiteGroup
)

map <- leaflet::leaflet(.data) %>%
leaflet::addProviderTiles("Esri.WorldTopoMap", group = "World topo", options = leaflet::providerTileOptions(updateWhenZooming = FALSE, updateWhenIdle = TRUE)) %>%
leaflet.extras::addResetMapButton() # button to reset to initial zoom and lat/long
if (nrow(.data) > 0) {
map <- map %>% leaflet::addCircleMarkers(~LongitudeMeasure,
~LatitudeMeasure,
color = ~pal(TADA.NearbySiteGroup),
opacity = 1,
fillColor = ~pal(TADA.NearbySiteGroup),
fillOpacity = 1,
radius = ifelse(dist_buffer > 200,
dist_buffer/10,
20),
weight = 1,
# label = ~as.character(TADA.MonitoringLocationIdentifier),
popup = paste0(
"Nearby Group Name: ", .data$TADA.MonitoringLocationIdentifier,
"<br> Nearby Site Group: ", .data$TADA.NearbySiteGroup,
"<br> Site ID: ", .data$MonitoringLocationIdentifier,
"<br> Site Name: ", .data$MonitoringLocationName,
"<br> Latitude: ", .data$LatitudeMeasure,
"<br> Longitude: ", .data$LongitudeMeasure
),
data = .data,
clusterOptions = leaflet::markerClusterOptions(),
) %>%
map <- map %>%
leaflet::addCircleMarkers(~LongitudeMeasure,
~LatitudeMeasure,
color = ~ pal(TADA.NearbySiteGroup),
opacity = 1,
fillColor = ~ pal(TADA.NearbySiteGroup),
fillOpacity = 1,
radius = ifelse(dist_buffer > 200,
dist_buffer / 10,
20
),
weight = 1,
# label = ~as.character(TADA.MonitoringLocationIdentifier),
popup = paste0(
"Nearby Group Name: ", .data$TADA.MonitoringLocationIdentifier,
"<br> Nearby Site Group: ", .data$TADA.NearbySiteGroup,
"<br> Site ID: ", .data$MonitoringLocationIdentifier,
"<br> Site Name: ", .data$MonitoringLocationName,
"<br> Latitude: ", .data$LatitudeMeasure,
"<br> Longitude: ", .data$LongitudeMeasure
),
data = .data,
clusterOptions = leaflet::markerClusterOptions(),
) %>%
leaflet::addCircles(~LongitudeMeasure,
~LatitudeMeasure,
color = ~pal(TADA.NearbySiteGroup),
opacity = 0.1,
fillColor = ~pal(TADA.NearbySiteGroup),
fillOpacity = 0.1,
radius = dist_buffer,
weight = 1)
~LatitudeMeasure,
color = ~ pal(TADA.NearbySiteGroup),
opacity = 0.1,
fillColor = ~ pal(TADA.NearbySiteGroup),
fillOpacity = 0.1,
radius = dist_buffer,
weight = 1
)
}

return(map)
}

Expand Down
Loading