Skip to content

Commit

Permalink
Updates to TADA_FlaggedSitesMap
Browse files Browse the repository at this point in the history
  • Loading branch information
hillarymarler committed Sep 11, 2024
1 parent a58be61 commit dc76337
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
80 changes: 66 additions & 14 deletions R/Figures.R
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,12 @@ TADA_OverviewMap <- function(.data, identifier = "tada") {
#' each row represents a unique data record. Data frame must include the columns
#' 'TADA.MonitoringLocationIdentifier','TADA.MonitoringLocationName','TADA.LatitudeMeasure',
#' and 'TADA.LongitudeMeasure' to run this function.
#'
#' @param imprecise
#'
#' @param outside_usa
#'
#' @param nearby
#'
#' @return A leaflet map that shows all sites in the data frame that contain
#' flagged data in the form of:
Expand All @@ -606,25 +612,71 @@ TADA_OverviewMap <- function(.data, identifier = "tada") {
#' }
#'

# HRM Note (9/9/24) - flagged sites map has not been updated to use TADA.MonitoringLocationIdentifier
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)
TADA_FlaggedSitesMap <- function(.data, imprecise = "wqx", outside_usa = "wqx", nearby = "wqx") {

# check to see if TADA_FlagCoordinate has been run on TADA df
if(!"TADA.InvalidCoordinates.Flag" %in% names(.data)) {

# if TADA_FlagCoordinates has not been run, run it
.data <- TADA_FlagCoordinates(.data)
}

# create subset of imprecise sites
lowres <- .data[.data$TADA.InvalidCoordinates.Flag == "Imprecise_lessthan3decimaldigits", ]

# create subset out outside usa sites
outsideusa <- .data[.data$TADA.InvalidCoordinates.Flag %in% c("LAT_OutsideUSA", "LONG_OutsideUSA"), ]

# check to see if TADA_NearbySites has been run on TADA df
if(!"TADA.NearbySites.Flag" %in% names(.data)) {

# if TADA_FlagCoordinates has not been run, run it
.data <- TADA_FindNearbySites(.data)
}

# create subset of unique nearby sites
nearby <- TADA_GetUniqueNearbySites(.data)

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")

# function to assign/lat long
assign.coords <- function(subset, input){

# create and assign latitude name
lat_name <- paste(subset, "_lat", sep = "")
lat_value <- ifelse(input == "wqx", "LatitudeMeasure",
"TADA.LatitudeMeasure")
assign(lat_name, lat_value, envir = .GlobalEnv)

# create and assign longitude name
long_name <- paste(subset, "_long", sep = "")
long_value <- ifelse(input == "wqx", "LongitudeMeasure",
"TADA.LongitudeMeasure")
assign(long_name, long_value, envir = .GlobalEnv)

# Return lat and long variables
output <- list(lat_name = get(lat_name, envir = .GlobalEnv),
long_name = get(long_name, envir = .GlobalEnv))

return(output)
}

# assign lat/long columns based on user input for each subset
imprecise.coords <- assign.coords(subset = "imprecise", input = imprecise)
outside.coords <- assign.coords(subset = "outside", input = outside_usa)
nearby.coords <- assign.coords(subset = "nearby", input = nearby)

# need to add originals and tada prefix data to popups in code below

# create map
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,
map <- map %>% leaflet::addAwesomeMarkers(~outside.coords[2],
~outside.coords[1],
icon = outsideIcon,
# label = ~as.character(MonitoringLocationIdentifier),
popup = paste0(
Expand All @@ -637,8 +689,8 @@ TADA_FlaggedSitesMap <- function(.data) {
)
}
if (nrow(lowres) > 0) {
map <- map %>% leaflet::addAwesomeMarkers(~TADA.LongitudeMeasure,
~TADA.LatitudeMeasure,
map <- map %>% leaflet::addAwesomeMarkers(~imprecise.coords[2],
~imprecise.coords[1],
icon = lowresIcon,
# label = ~as.character(MonitoringLocationIdentifier),
popup = paste0(
Expand All @@ -651,8 +703,8 @@ TADA_FlaggedSitesMap <- function(.data) {
)
}
if (nrow(nearby) > 0) {
map <- map %>% leaflet::addAwesomeMarkers(~TADA.LongitudeMeasure,
~TADA.LatitudeMeasure,
map <- map %>% leaflet::addAwesomeMarkers(nearby.coords[2],
~nearby.coords[1],
icon = nearbyIcon,
# label = ~as.character(TADA.MonitoringLocationIdentifier),
popup = paste0(
Expand Down
10 changes: 5 additions & 5 deletions R/Utilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -1001,10 +1001,7 @@ TADA_FindNearbySites <- function(.data, dist_buffer = 100, meta_select = "random
TADA.MonitoringLocationTypeName = ifelse(!ResultIdentifier %in% grouped_resultids,
TADA.MonitoringLocationTypeName, TADA.MonitoringLocationTypeName.New),
TADA.MonitoringLocationIdentifier = ifelse(TADA.MonitoringLocationIdentifier.New1 == "",
TADA.MonitoringLocationIdentifier, TADA.MonitoringLocationIdentifier.New1),
TADA.NearbySites.Flag = ifelse(is.na(TADA.NearbySites.Flag),
"No nearby sites detected using input buffer distance.",
TADA.NearbySites.Flag)) %>%
TADA.MonitoringLocationIdentifier, TADA.MonitoringLocationIdentifier.New1)) %>%
dplyr::select(-TADA.MonitoringLocationIdentifier.New1, -TADA.MonitoringLocationName.New,
-TADA.LatitudeMeasure.New, -TADA.LongitudeMeasure.New,
-TADA.MonitoringLocationTypeName.New)
Expand All @@ -1018,7 +1015,10 @@ TADA_FindNearbySites <- function(.data, dist_buffer = 100, meta_select = "random
dplyr::mutate(TADA.NearbySites.Flag = "No nearby sites detected using input buffer distance.")
}

.data <- TADA_OrderCols(.data)
.data <- TADA_OrderCols(.data) %>%
dplyr::mutate(TADA.NearbySites.Flag = ifelse(is.na(TADA.NearbySites.Flag),
"No nearby sites detected using input buffer distance.",
TADA.NearbySites.Flag))

return(.data)
}
Expand Down

0 comments on commit dc76337

Please sign in to comment.