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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export(TADA_FlagMeasureQualifierCode)
export(TADA_FlagMethod)
export(TADA_FlagResultUnit)
export(TADA_FlagSpeciation)
export(TADA_FlaggedSitesMap)
export(TADA_FormatDelimitedString)
export(TADA_GetATTAINS)
export(TADA_GetActivityTypeRef)
export(TADA_GetCharacteristicRef)
Expand All @@ -47,6 +49,7 @@ export(TADA_GetNutrientSummationRef)
export(TADA_GetSynonymRef)
export(TADA_GetTemplate)
export(TADA_GetUSGSSynonymRef)
export(TADA_GetUniqueNearbySites)
export(TADA_GetWQXCharValRef)
export(TADA_HarmonizeSynonyms)
export(TADA_Histogram)
Expand Down
87 changes: 87 additions & 0 deletions R/Figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,93 @@ TADA_OverviewMap <- function(.data) {
}))
}

#' Create Flagged Sites Map
#'
#' @param .data TADA data frame containing the data downloaded from the WQP, where
#' each row represents a unique data record. Data frame must include the columns
#' 'MonitoringLocationIdentifier','MonitoringLocationName','TADA.LatitudeMeasure',
#' and 'TADA.LongitudeMeasure' to run this function.
#'
#' @return A leaflet map that shows all sites in the data frame that contain
#' flagged data in the form of:
#' 1) imprecise coordinates - latitudes and/or longitudes that contain fewer
#' then 3 decimal places.
#' 2) outside USA - coordinates that fall outside the bounds of the USA.
#' 3) near other sites - groups of sites that are spatially located within
#' a threshhold distance (defaulting to 100 m) from each other.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # Load example data frame:
#' data(Data_Nutrients_UT)
#' data(Data_NCTCShepherdstown_HUC12)
#' data(Data_6Tribes_5y_Harmonized)
#'
#' # Create maps:
#' TADA_FlaggedSitesMap(Data_Nutrients_UT)
#' TADA_FlaggedSitesMap(Data_NCTCShepherdstown_HUC12)
#' TADA_FlaggedSitesMap(Data_6Tribes_5y_Harmonized)
#' }
#'
#'
TADA_FlaggedSitesMap <- function(.data) {
invalid <- TADA_FlagCoordinates(.data, flaggedonly = TRUE)
lowres <- invalid[invalid$TADA.InvalidCoordinates.Flag == "Imprecise_lessthan3decimaldigits",]
outsideusa <- invalid[invalid$TADA.InvalidCoordinates.Flag %in% c("LAT_OutsideUSA", "LONG_OutsideUSA"),]
nearby <- TADA_FindNearbySites(.data)
print(colnames(nearby))
nearby <- TADA_GetUniqueNearbySites(nearby)

lowresIcon <- leaflet::makeAwesomeIcon(icon = "circle", library = "fa", iconColor = "#ffffff", markerColor = "green")
outsideIcon <- leaflet::makeAwesomeIcon(icon = "circle", library = "fa", iconColor = "#ffffff", markerColor = "darkblue")
nearbyIcon <- leaflet::makeAwesomeIcon(icon = "circle", library = "fa", iconColor = "#ffffff", markerColor = "pink")

map <- leaflet::leaflet() %>%
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(outsideusa) > 0) {
map <- map %>% leaflet::addAwesomeMarkers(~TADA.LongitudeMeasure,
~TADA.LatitudeMeasure,
icon = outsideIcon,
# label = ~as.character(MonitoringLocationIdentifier),
popup = paste0(
"Site ID: ", outsideusa$MonitoringLocationIdentifier,
"<br> Site Name: ", outsideusa$MonitoringLocationName,
"<br> Latitude: ", outsideusa$TADA.LatitudeMeasure,
"<br> Longitude: ", outsideusa$TADA.LongitudeMeasure),
data = outsideusa)
}
if (nrow(lowres) > 0) {
map <- map %>% leaflet::addAwesomeMarkers(~TADA.LongitudeMeasure,
~TADA.LatitudeMeasure,
icon = lowresIcon,
# label = ~as.character(MonitoringLocationIdentifier),
popup = paste0(
"Site ID: ", lowres$MonitoringLocationIdentifier,
"<br> Site Name: ", lowres$MonitoringLocationName,
"<br> Latitude: ", lowres$TADA.LatitudeMeasure,
"<br> Longitude: ", lowres$TADA.LongitudeMeasure),
data = lowres)
}
if (nrow(nearby) > 0) {
map <- map %>% leaflet::addAwesomeMarkers(~TADA.LongitudeMeasure,
~TADA.LatitudeMeasure,
icon = nearbyIcon,
# label = ~as.character(TADA.MonitoringLocationIdentifier),
popup = paste0(
"Nearby Group Name: ", nearby$TADA.MonitoringLocationIdentifier,
"<br> Site ID: ", nearby$MonitoringLocationIdentifier,
"<br> Site Name: ", nearby$MonitoringLocationName,
"<br> Latitude: ", nearby$TADA.LatitudeMeasure,
"<br> Longitude: ", nearby$TADA.LongitudeMeasure),
data = nearby)
}

return(map)
}

#' Field Values Pie Chart
#'
#' Function creates a ggplot2 pie chart showing the relative proportions of values in a given field in a TADA dataset.
Expand Down
2 changes: 1 addition & 1 deletion R/RequiredCols.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ require.cols <- c(
"TADA.InvalidCoordinates.Flag", # generated
"HUCEightDigitCode",
"MonitoringLocationIdentifier", # required
"TADA.NearbySiteGroups",
"TADA.MonitoringLocationIdentifier",

# Groundwater fields
"AquiferName", # filter, groundwater
Expand Down
24 changes: 12 additions & 12 deletions R/ResultFlagsIndependent.R
Original file line number Diff line number Diff line change
Expand Up @@ -1182,9 +1182,9 @@ TADA_FlagCoordinates <- function(.data,
#' issues in the data analysis.
#'
#' This function runs TADA_FindNearbySites within it which adds the
#' TADA.NearbySiteGroups field. Duplicates are only flagged as duplicates if
#' TADA.MonitoringLocationIdentifier field. Duplicates are only flagged as duplicates if
#' the distance between sites is less than the function input dist_buffer
#' (default is 100m). Each group in the TADA.NearbySiteGroups field indicates
#' (default is 100m). Each group in the TADA.MonitoringLocationIdentifier field indicates
#' that the sites within each group are within the specified distance from each other.
#'
#' We recommend running TADA_FindPotentialDuplicatesMultipleOrgs after running
Expand All @@ -1208,7 +1208,7 @@ TADA_FlagCoordinates <- function(.data,
#' containing a number unique to results that may represent duplicated
#' measurement events, a TADA.ResultSelectedMultipleOrgs column indicating
#' which rows are selected to keep (Y) and remove (N) based on the
#' org hierarchy, and a TADA.NearbySiteGroups column indicating which
#' org hierarchy, and a TADA.MonitoringLocationIdentifier column indicating which
#' monitoring locations are within the distance buffer from each other.
#'
#' @export
Expand All @@ -1225,29 +1225,29 @@ TADA_FlagCoordinates <- function(.data,
#'
TADA_FindPotentialDuplicatesMultipleOrgs <- function(.data, dist_buffer = 100, org_hierarchy = "none") {
# from those datapoints, determine which are in adjacent sites
if (!"TADA.NearbySiteGroups" %in% names(.data)) {
if (!"TADA.MonitoringLocationIdentifier" %in% names(.data)) {
.data <- TADA_FindNearbySites(.data, dist_buffer = dist_buffer)
}

dupsites <- unique(.data[, c("MonitoringLocationIdentifier", "TADA.LatitudeMeasure", "TADA.LongitudeMeasure", "TADA.NearbySiteGroups")])
dupsites <- unique(.data[, c("MonitoringLocationIdentifier", "TADA.LatitudeMeasure", "TADA.LongitudeMeasure", "TADA.MonitoringLocationIdentifier")])

# get rid of results with no site group added - not duplicated spatially
dupsites <- subset(dupsites, !dupsites$TADA.NearbySiteGroups %in% c("No nearby sites")) %>%
tidyr::separate_rows(TADA.NearbySiteGroups, sep = ",")
dupsites <- subset(dupsites, !dupsites$TADA.MonitoringLocationIdentifier %in% c("No nearby sites")) %>%
tidyr::separate_rows(TADA.MonitoringLocationIdentifier, sep = ",")

# remove results with no nearby sites get all data that are not NA and round to 2 digits
dupsprep <- .data %>%
dplyr::filter(MonitoringLocationIdentifier %in% dupsites$MonitoringLocationIdentifier) %>%
dplyr::select(
OrganizationIdentifier, ResultIdentifier, ActivityStartDate, ActivityStartTime.Time,
TADA.CharacteristicName, ActivityTypeCode, TADA.ResultMeasureValue, TADA.NearbySiteGroups
TADA.CharacteristicName, ActivityTypeCode, TADA.ResultMeasureValue, TADA.MonitoringLocationIdentifier
) %>%
dplyr::filter(!is.na(TADA.ResultMeasureValue)) %>%
dplyr::mutate(roundRV = round(TADA.ResultMeasureValue, digits = 2))

# group by date, time, characteristic, and rounded result value and determine the number of organizations that have those same row values, and filter to those summary rows with more than one organization
dups_sum <- dupsprep %>%
dplyr::group_by(ActivityStartDate, ActivityStartTime.Time, TADA.CharacteristicName, ActivityTypeCode, roundRV, TADA.NearbySiteGroups) %>%
dplyr::group_by(ActivityStartDate, ActivityStartTime.Time, TADA.CharacteristicName, ActivityTypeCode, roundRV, TADA.MonitoringLocationIdentifier) %>%
dplyr::mutate(numorgs = length(unique(OrganizationIdentifier))) %>%
dplyr::filter(numorgs > 1) %>%
# group duplicates
Expand All @@ -1264,7 +1264,7 @@ TADA_FindPotentialDuplicatesMultipleOrgs <- function(.data, dist_buffer = 100, o
"OrganizationIdentifier",
"ResultIdentifier",
"TADA.ResultMeasureValue",
"TADA.NearbySiteGroups"
"TADA.MonitoringLocationIdentifier"
)) %>%
dplyr::mutate(TADA.MultipleOrgDuplicate = ifelse(is.na(TADA.MultipleOrgDupGroupID), "N", "Y")) %>%
# remove results that are listed twice (as part of two groups)
Expand Down Expand Up @@ -1305,9 +1305,9 @@ TADA_FindPotentialDuplicatesMultipleOrgs <- function(.data, dist_buffer = 100, o
dplyr::slice_sample(n = 1)

dupsdat <- dupsdat %>%
dplyr::rename(SingleNearbyGroup = TADA.NearbySiteGroups) %>%
dplyr::rename(SingleNearbyGroup = TADA.MonitoringLocationIdentifier) %>%
dplyr::mutate(
TADA.NearbySiteGroups = paste(SingleNearbyGroup, sep = ","),
TADA.MonitoringLocationIdentifier = paste(SingleNearbyGroup, sep = ","),
TADA.ResultSelectedMultipleOrgs = ifelse(ResultIdentifier %in% duppicks$ResultIdentifier, "Y", "N")
) %>%
dplyr::select(-SingleNearbyGroup)
Expand Down
Loading