Skip to content

Commit

Permalink
Merge branch 'feature/35_feature' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
gklarenberg committed Jun 28, 2024
2 parents 8575d31 + 322ce9e commit f9c610b
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 46 deletions.
4 changes: 3 additions & 1 deletion 02_Cleaning_scripts/WQ_GTMNERR.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ WQ_locations <- WQ %>%
mutate(Year = year(SampleDate)) %>%
select(site_friendly, Year, site_acronym, lat, long, wbid, location) %>%
group_by(site_friendly, site_acronym, lat, long, wbid, location) %>%
summarize(maxYear = max(Year), minYear = min(Year))
summarize(maxYear = max(Year), minYear = min(Year)) %>%
mutate(type = "Water quality",
dataset = "Guana Water Quality Monitoring (GTMNERR)")

WQ_data_available <- WQ %>%
mutate(Year = year(SampleDate)) %>%
Expand Down
9 changes: 4 additions & 5 deletions 02_Cleaning_scripts/shapefile_cleanup.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pt4 <- st_point(c(xmax, ymax))
# Put together as sf object and get bounding box
bound_box <- st_bbox(st_sfc(pt1, pt3, pt4, pt2, crs = st_crs(GTMNERR)))

##### Counties #####
#### Counties #####
counties <- st_read("01_Data_raw/shapefiles/countyshore_areas_sep15/countyshore_areas_sep15.shp")
# CRS: Albers Conical Equal Area
counties <- st_transform(counties, crs = st_crs(GTMNERR))
Expand All @@ -79,7 +79,7 @@ st_write(counties_select, "03_Data_for_app/shapefiles_new/counties_GTMNERR.shp",
append = FALSE)
# Ignore warnings when writing data: checked saved shapefile and it is correct

##### Salt marshes #####
#### Salt marshes #####
salt_marsh <- st_read("01_Data_raw/shapefiles/salt_marsh_2020/salt_marsh_2020.shp")
# CRS: NAD83(HARN) / Florida GDL Albers -> for visualization this is not a problem,
# for consistency I am still changing the CRS
Expand All @@ -95,7 +95,7 @@ ggplot()+
st_write(salt_marsh, "03_Data_for_app/shapefiles_new/salt_marsh_GTMNERR.shp",
append = FALSE)

##### Hydrology (several shapefiles) #####
#### Hydrology (several shapefiles) #####
hydro_6 <- st_read("01_Data_raw/shapefiles/nhdwbd_huc6_dec17/nhdwbd_huc6_dec17.shp")
# CRS: Albers Conical Equal Area
hydro_6 <- st_transform(hydro_6, crs = st_crs(GTMNERR))
Expand Down Expand Up @@ -159,8 +159,7 @@ ggplot()+
# Look up differences between HUCs to add to metadata
st_write(hydro_10_select, "03_Data_for_app/shapefiles_new/nhdwbd_huc10_dec17_GTMNERR.shp", append = FALSE)
st_write(hydro_12_select, "03_Data_for_app/shapefiles_new/nhdwbd_huc12_dec17_GTMNERR.shp", append = FALSE)
#### CHECK WARNINGS of some values not being written (too long) #####

#### Checked warnings of some values not being written (too long): looks fine

#### Water bodies ####
waterbodies <- st_read("01_Data_raw/shapefiles/nhd24waterbody_dec17/nhd24waterbody_dec17.shp")
Expand Down
Binary file modified 03_Data_for_app/WQ_locations.Rds
Binary file not shown.
15 changes: 13 additions & 2 deletions 04_Tests/leaflet_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ GTMNERR <- st_read("03_Data_for_app/shapefiles_new/GTMNERR.shp")# CRS: Albers Co

GTMNERR <- st_transform(GTMNERR, crs = 4326)

m <- leaflet(data = GTMNERR, options = leafletOptions(minZoom = 9, maxZoom = 18)) %>%
# Leaflet uses EPSG 3857, change this
# https://gis.stackexchange.com/questions/48949/epsg-3857-or-4326-for-web-mapping
epsg4326 <- leafletCRS(crsClass = "L.CRS.EPSG4326")
# BUT! It actually seems that Leaflet does still want shapefiles in 4326, see
# https://github.com/Leaflet/Leaflet/issues/4146 I also tried to change GTMNERR
# to crs 3857 but that threw errors. See second paragraph here:
# https://rstudio.github.io/leaflet/articles/projections.html

m <- leaflet(data = GTMNERR,
options = leafletOptions(#crs = epsg4326,
minZoom = 9,
maxZoom = 18)) %>%
#setView(lng=-81.347388, lat=30.075, zoom = 11) %>%
clearBounds() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
Expand All @@ -32,7 +43,7 @@ HAB_data_locations <- HAB_data %>%
distinct() %>%
st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)

leaflet(options = leafletOptions(minZoom = 9, maxZoom = 18)) %>%
leaflet(options = leafletOptions(crs = epsg4326, minZoom = 9, maxZoom = 18)) %>%
setView(lng=-81.347388, lat=30.075, zoom = 11) %>%
# Base map
addTiles() %>% # Add default OpenStreetMap map tiles
Expand Down
23 changes: 18 additions & 5 deletions global.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,23 @@ try({
print(paste0("Working dir: ", getwd()))
}, silent = FALSE) # Setting silent = FALSE will print the error message to the console

#### GTMNERR shapefile ------------------------------------------------
GTMNERR <- st_read("./03_Data_for_app/shapefiles_new/GTMNERR.shp")
GTMNERR <- st_transform(GTMNERR, crs = 4326)
### Load shapefiles ------------------------------------------------

#### county shapefiles ------------------------------------------------
##### GTMNERR shapefile ------------------------------------------------
GTMNERR <- st_read("./03_Data_for_app/shapefiles_new/GTMNERR_small_nosea.shp")

##### county shapefiles ------------------------------------------------
counties_select <- st_read("./03_Data_for_app/shapefiles_new/counties_GTMNERR.shp")
counties_select <- st_transform(counties_select, crs = 4326)

##### HUC shapefiles ------------------------------------------------
HUC10 <- st_read("./03_Data_for_app/shapefiles_new/nhdwbd_huc10_dec17_GTMNERR.shp")
HUC12 <- st_read("./03_Data_for_app/shapefiles_new/nhdwbd_huc12_dec17_GTMNERR.shp")

##### Outstanding Florida Waters (OFW) shapefiles ------------------------------
ofw <- st_read("./03_Data_for_app/shapefiles_new/ofw_GTMNERR.shp")

##### mangrove shapefiles -----------------------------------------------------
mangroves <- st_read("./03_Data_for_app/shapefiles_new/mangrove_GTMNERR.shp")

##### salt marsh shapefiles -----------------------------------------------------
saltmarsh <- st_read("./03_Data_for_app/shapefiles_new/salt_marsh_GTMNERR.shp")
142 changes: 109 additions & 33 deletions modules/main_page.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@

#### Get WIN data locations
# 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_data_locations = WIN_df %>%
filter(variable %in% c("geometry",
"HUC12.Name",
"Start.Date",
"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, HUC12.Name, Start.Date, latitude, longitude) %>%
mutate(
lat = as.numeric(latitude),
long = as.numeric(longitude),
type = "Water quality",
dataset = "Watershed Information Network (DEP)"
) %>%
select(-geometry, - latitude, -longitude)

#### WQ locations data ------------------------------------------------
WQ_data_locations <- readRDS("./03_Data_for_app/WQ_locations.Rds")

# 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("long", "lat"), crs = 4326)

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

### Define the UI -------------------------------------------------------------
mainPageUI <- function(id) {
ns <- NS(id) # This is an important part to add to all subpages so they use the
# correct sessions / ID's that connect the ui and server here
tagList(
h2("Main Page"),
p("This is the main page of the Guana River Data Dashboard."),
# Dropdown menu for markers is above the map
fluidRow(
# Map occupies 1st column
column(width = 10, leafletOutput(ns("map"), height=750)),
# histogram occupies rows in the 2nd column
# column(width = 5, plotOutput(ns("distPlot")),
# sliderInput(ns("bins"), "Number of bins:",
# min = 1, max = 50, value = 30)
# )
column(width = 7, uiOutput(ns("dropdown_ui"))),
),
fluidRow(
column(width = 10, leafletOutput(ns("map"), height="500px")),
),
# fluidRow(
# column(12,
Expand All @@ -21,23 +60,21 @@ mainPageUI <- function(id) {
)
}

### Define the server logic ----------------------------------------------------

mainPageServer <- function(input, output, session) {
ns <- session$ns

# Create the histogram
# output$distPlot <- renderPlot({
# # generate bins based on input$bins from ui.R
# x <- filter(WQ, ComponentShort == "ATEMP") %>% select(Result) %>% pull()
# x <- as.numeric(x) # stop-gap measure because everything is characters
# bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE),
# length.out = input$bins + 1)
#
# # draw the histogram with the specified number of bins
# hist(x, breaks = bins, col = 'darkgray', border = 'white',
# xlab = 'Air temperature (degrees Celsius)',
# main = 'Histogram of air temperatures')
# })
# Create the dropdown UI
output$dropdown_ui <- renderUI({
print("Rendering dropdown UI")
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]
)
})

# Create the map
output$map <- renderLeaflet({
Expand All @@ -55,22 +92,61 @@ mainPageServer <- function(input, output, session) {
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE),
group = "Counties", popup = ~NAME) %>%
# addMarkers(data = HAB_data_locations,
# popup = ~paste("Site: ", Site, "<br>",
# "County: ", County),
# group = "HAB") %>%
addMarkers(data = WQ_data_locations,
popup = ~paste("Station: ", site_friendly, "<br>",
"Location: ", wbid, "<br>",
"Latest year of sampling: ", maxYear, "<br",
"Sampling start year: ", minYear, "<br"),
group = "WQ") %>%
# # Layers control (turning layers on and off)
addLayersControl(overlayGroups = c("Counties", "GTMNERR boundaries", "WQ"),
options = layersControlOptions(collapsed = FALSE)) %>%
addPolygons(data = mangroves,
color = "darkgreen", weight = 2, opacity = 1,
fill = TRUE, fillColor = "darkgreen", fillOpacity = 0.4,
group = "Mangroves") %>% #, popup = ~Area_ha
addPolygons(data = ofw,
color = "darkorange", weight = 2, opacity = 1,
fill = TRUE, fillColor = "darkorange", fillOpacity = 0.4,
group = "Outstanding Florida Waters") %>%
addPolygons(data = saltmarsh,
color = "darkslateblue", weight = 2, opacity = 1,
fill = TRUE, fillColor = "darkslateblue", fillOpacity = 0.4,
group = "Salt marshes") %>%
addPolygons(data = HUC10,
color = "royalblue", weight = 2, opacity = 1,
fill = TRUE, fillColor = "royalblue", fillOpacity = 0.2,
group = "HUC10", popup = ~NAME) %>%
addPolygons(data = HUC12,
color = "darkblue", weight = 2, opacity = 1,
fill = TRUE, fillColor = "darkblue", fillOpacity = 0.2,
group = "HUC12", popup = ~NAME) %>%
# Layers control (turning layers on and off)
addLayersControl(overlayGroups = c("GTMNERR boundaries", "Counties",
"Mangroves", "Salt marshes",
"Outstanding Florida Waters", "HUC10",
"HUC12"),
options = layersControlOptions(collapsed = TRUE)) %>%
hideGroup(c("Counties", "Mangroves", "Salt marshes",
"Outstanding Florida Waters", "HUC10", "HUC12")) %>%
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,]

# Add markers to the map - commented out the popup bc this only works for WQ data
print("Adding markers")
leafletProxy(ns("map")) %>%
clearMarkers() %>%
addCircleMarkers(
data = filtered_data,
color = ~color_palette(type),
opacity = 1,
fillOpacity = 0.5,
fillColor = ~color_palette(type),
fill = TRUE,
weight = 3,
radius = 8,
# popup = ~paste("Station: ", site_friendly, "<br>",
# "Location: ", wbid, "<br>",
# "Latest year of sampling: ", maxYear, "<br",
# "Sampling start year: ", minYear, "<br")
)
})
# Add buttons to go to other pages
# observeEvent(input[[ns("go_to_subpage")]], {
# print("Go to subpage button clicked")
Expand Down

0 comments on commit f9c610b

Please sign in to comment.