Skip to content

Commit

Permalink
Merge branch 'feature/39_feature' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gklarenberg committed Sep 6, 2024
2 parents d3ffdb6 + 462e688 commit d5658d4
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 141 deletions.
115 changes: 115 additions & 0 deletions 02_Cleaning_scripts/Create_location_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
########################################################################
########## NERRS Science Transfer project - GTMNERR #############
########################################################################

# Geraldine Klarenberg, PhD
# [email protected]
# 6 Sep 2024

# Load packages
library(tidyverse)

# Script to create file with locations, for mapping

#### Water Quality ####
# import all WQ data
WQ_df <- readRDS("./03_Data_for_app/WQ_all.Rds")

WQ_years <- WQ_df %>% # site friendly and station code are the names in common
filter(variable %in% c("StationCode", "site_friendly",
"geometry",
"SampleDate", # changed, from StartDate - but also not necessary for locations
"Latitude",
"Longitude",
"data_source")
) %>%
#select(c(RowID, variable, value)) %>% # not necessary
#distinct(RowID, variable, value) %>% # not necessary
pivot_wider(
names_from = variable,
values_from = value,
values_fill = list(value = NA)
) %>%
mutate(
SampleDate = parse_date_time(SampleDate, c("ymd_HMS", "ymd")), # get a warning about 311 failed to parse, but they did, only as ymd
year = year(SampleDate),
Latitude = as.numeric(Latitude),
Longitude = as.numeric(Longitude)
) %>%
group_by(Latitude, Longitude, site_friendly) %>%
summarize(minYear = min(year(SampleDate)),
maxYear = max(year(SampleDate)))

# make dataframe for map display and hover data
WQ_data_locations = WQ_df %>% # site friendly and station code are the names in common
filter(variable %in% c("StationCode", "site_friendly",
"geometry",
"SampleDate", # changed, from StartDate - but also not necessary for locations
"Latitude",
"Longitude",
"data_source")
) %>%
#select(c(RowID, variable, value)) %>% # not necessary
#distinct(RowID, variable, value) %>% # not necessary
pivot_wider(
names_from = variable,
values_from = value,
values_fill = list(value = NA)
) %>%
mutate(
#SampleDate = ymd_hms(SampleDate), # get a warning about 311 failed to parse, but they did, only as ymd
Latitude = as.numeric(Latitude),
Longitude = as.numeric(Longitude)
) %>%
select(-SampleDate) %>%
distinct(Latitude, Longitude, geometry, data_source, StationCode, site_friendly)

WQ_data_locations <- WQ_data_locations %>%
left_join(WQ_years)

# Save data
saveRDS(WQ_data_locations, "03_Data_for_app/WQ_data_locations.Rds")

WQ_data_locations <- WQ_data_locations %>%
select(-geometry)

#### Algae ####
HAB_df <- readRDS("03_Data_for_app/HAB.Rds")

# Get min/max years of measurements
HAB_years <- HAB_df %>%
mutate(date = dmy(`Sample Date`),
year = year(date)) %>%
group_by(Latitude, Longitude, Site) %>%
summarize(minYear = min(year),
maxYear = max(year)) %>%
rename(site_friendly = Site)

HAB_data_locations <- HAB_df %>%
distinct(Latitude, Longitude, Site) %>%
rename(site_friendly = Site) %>%
mutate(data_source = "FWC") # or GTMNERR?

HAB_data_locations <- HAB_data_locations %>%
left_join(HAB_years)

saveRDS(HAB_data_locations, "03_Data_for_app/HAB_data_locations.Rds")


#### Shellfish ####



#### All - main page ####
# Add dataset type to dfs
WQ_data_locations["type"] <- "Water Quality"
HAB_data_locations["type"] <- "Algae"


# Merge
all_data_locations <- WQ_data_locations %>%
full_join(HAB_data_locations)

# Save
saveRDS(all_data_locations, "03_Data_for_app/all_data_locations.Rds")

2 changes: 1 addition & 1 deletion 02_Cleaning_scripts/shapefile_cleanup.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,5 @@ ggplot()+
geom_sf(data = GTMNERR, fill = "blue", color = NA, alpha = 0.3)+
geom_sf(data = ofw_select, fill = "darkred", color = NA)

st_write(mangrove_select, "03_Data_for_app/shapefiles_new/ofw_GTMNERR.shp",
st_write(ofw_select, "03_Data_for_app/shapefiles_new/ofw_GTMNERR.shp",
append = FALSE)
Binary file added 03_Data_for_app/HAB_data_locations.Rds
Binary file not shown.
Binary file added 03_Data_for_app/WQ_data_locations.Rds
Binary file not shown.
Binary file added 03_Data_for_app/all_data_locations.Rds
Binary file not shown.
Binary file modified 03_Data_for_app/shapefiles_new/mangrove_GTMNERR.dbf
Binary file not shown.
Binary file modified 03_Data_for_app/shapefiles_new/ofw_GTMNERR.dbf
Binary file not shown.
Binary file modified 03_Data_for_app/shapefiles_new/ofw_GTMNERR.shp
Binary file not shown.
Binary file modified 03_Data_for_app/shapefiles_new/ofw_GTMNERR.shx
Binary file not shown.
Binary file modified 03_Data_for_app/shapefiles_new/salt_marsh_GTMNERR.dbf
Binary file not shown.
119 changes: 66 additions & 53 deletions modules/main_page.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,46 @@
# I am putting this here right now, but I feel we should move this to a cleaning
# script so it doesn't need to be run every time someone uses the app (as with
# WQ locations)
WIN_df <- readRDS("./03_Data_for_app/WIN.Rds")
# WIN_df <- readRDS("./03_Data_for_app/WIN.Rds")
#
# WIN_data_locations = WIN_df %>%
# filter(variable %in% c("geometry",
# "StationCode",
# "SampleDate",
# "Latitude",
# "Longitude")
# ) %>%
# select(c(RowID, variable, value)) %>%
# distinct(RowID, variable, value) %>%
# pivot_wider(
# names_from = variable,
# values_from = value,
# values_fill = list(value = NA)
# ) %>%
# distinct(geometry, StationCode, SampleDate, Latitude, Longitude) %>%
# mutate(
# SampleDate = ymd_hms(SampleDate),
# Latitude = as.numeric(Latitude),
# Longitude = as.numeric(Longitude),
# type = "Water quality",
# dataset = "Watershed Information Network (DEP)", # Update this so we use data_source
# minYear = min(year(SampleDate)),
# maxYear = max(year(SampleDate))
# ) %>%
# select(-geometry, -SampleDate)

WIN_data_locations = WIN_df %>%
filter(variable %in% c("geometry",
"StationCode",
"SampleDate",
"Latitude",
"Longitude")
) %>%
select(c(RowID, variable, value)) %>%
distinct(RowID, variable, value) %>%
pivot_wider(
names_from = variable,
values_from = value,
values_fill = list(value = NA)
) %>%
distinct(geometry, StationCode, SampleDate, Latitude, Longitude) %>%
mutate(
SampleDate = ymd_hms(SampleDate),
Latitude = as.numeric(Latitude),
Longitude = as.numeric(Longitude),
type = "Water quality",
dataset = "Watershed Information Network (DEP)", # Update this so we use data_source
minYear = min(year(SampleDate)),
maxYear = max(year(SampleDate))
) %>%
select(-geometry, -SampleDate)
#### Location data ------------------------------------------------
all_data_locations <- readRDS("./03_Data_for_app/all_data_locations.Rds")

#### WQ locations data ------------------------------------------------
WQ_data_locations <- readRDS("./03_Data_for_app/WQ_locations.Rds")
# add info for icons and colors
all_data_locations <- all_data_locations %>%
mutate(group_icon = case_when(
type == "Water Quality" ~ "flask",
type == "Algae" ~ "microscope"),
group_color = case_when(
type == "Water Quality" ~ "orange",
type == "Algae" ~ "purple"))

# For now, for testing, make WQ_locations the dataframe to be used for filtering
datasets_location <- full_join(WQ_data_locations, WIN_data_locations)
datasets_location <- st_as_sf(datasets_location, coords = c("Longitude", "Latitude"), crs = 4326)
datasets_location <- datasets_location[!duplicated(datasets_location),]

color_palette <- colorFactor(palette = c("red", "goldenrod1"), #, "blue", "green"
domain = datasets_location$dataset)

#### HAB locations data ------------------------------------------------
# Add this at some point so there is another dataset that shows up in the dropdown
# menu

### Define the UI -------------------------------------------------------------
mainPageUI <- function(id) {
Expand All @@ -64,8 +62,8 @@ mainPageUI <- function(id) {
tagList(
h2("Welcome!"),
p("This is the main page of the Guana River Data Dashboard. Use the dropdown menu
below to see locations with data. To view these data, use the menu on the
left of the screen."),
below to see locations with a certain data type. To view these data, use
the menu on the left of the screen."),
# Dropdown menu for markers is above the map
fluidRow(
column(width = 7, uiOutput(ns("dropdown_ui")), style = "position:relative;z-index:10000;"),
Expand All @@ -92,8 +90,8 @@ mainPageServer <- function(input, output, session) {
selectInput(
inputId = ns("datatype_selector"),
label = "Select a type of data to see locations with data availability",
choices = unique(datasets_location$type),
selected = unique(datasets_location$type)[1]
choices = unique(all_data_locations$type),
selected = unique(all_data_locations$type)[1]
)
})

Expand Down Expand Up @@ -128,34 +126,49 @@ mainPageServer <- function(input, output, session) {
addPolygons(data = HUC10,
color = "royalblue", weight = 2, opacity = 1,
fill = TRUE, fillColor = "royalblue", fillOpacity = 0.2,
group = "HUC10", popup = ~NAME) %>%
group = "Watershed Basins", popup = ~NAME) %>%
addPolygons(data = HUC12,
color = "darkblue", weight = 2, opacity = 1,
fill = TRUE, fillColor = "darkblue", fillOpacity = 0.2,
group = "HUC12", popup = ~NAME) %>%
group = "Watershed Subbasins", popup = ~NAME) %>%
# Layers control (turning layers on and off)
addLayersControl(overlayGroups = c("GTMNERR boundaries", "Counties",
"Mangroves", "Salt marshes",
"Outstanding Florida Waters", "HUC10",
"HUC12"),
"Mangroves", "Outstanding Florida Waters",
"Salt marshes", "Watershed Basins",
"Watershed Subbasins"),
options = layersControlOptions(collapsed = TRUE)) %>%
hideGroup(c("Counties", "Mangroves", "Salt marshes",
"Outstanding Florida Waters", "HUC10", "HUC12")) %>%
hideGroup(c("Counties", "Mangroves", "Outstanding Florida Waters",
"Salt marshes", "Watershed Basins", "Watershed Subbasins")) %>%
addMeasure(primaryLengthUnit = "miles", primaryAreaUnit = "sqmiles")
})

# Select dataset to add markers to the plot
observeEvent(input$datatype_selector, {
# Filter data based on selected group
filtered_data <- datasets_location[datasets_location$type == input$datatype_selector,]
filtered_data <- all_data_locations[all_data_locations$type == input$datatype_selector,]
print(filtered_data)
# Add markers to the map - commented out the popup bc this only works for WQ data
print("Adding markers")
leafletProxy(ns("map")) %>%
clearMarkers() %>%
addAwesomeMarkers(icon = makeAwesomeIcon(icon = "flask", markerColor = "orange", library = "fa",
addAwesomeMarkers(
data = filtered_data,
icon = makeAwesomeIcon(icon = ~group_icon, markerColor = ~group_color, library = "fa",
iconColor = "black"),
data = filtered_data) #%>%
options = markerOptions(riseOnHover = TRUE), # Brings marker forward when hovering
popup = ~paste("<b>Station:</b> ", site_friendly, "<br>", # popups appear when clicking
"<b>Sampling start year:</b> ", minYear, "<br>",
"<b>Latest year of sampling:</b> ", maxYear, "<br"),
label = ~paste("Station: ", site_friendly), # labels appear when hovering
labelOptions = labelOptions(direction = "auto",
style = list(
"color" = "gray27",
"font-style" = "italic",
"font-size" = "12px",
"border-color" = "rgba(0,0,0,0.5)"
)
)
)#%>%
# addCircleMarkers(
# data = filtered_data,
# color = ~color_palette(dataset),
Expand Down
Loading

0 comments on commit d5658d4

Please sign in to comment.