library(tidyverse)
library(here)
library(patchwork)
library(sf)
library(leaflet)
library(tbeptools)
data(fimstations, package = 'tbeptools')
data(tbseg, package = 'tbeptools')
load(file = here('data/tbniscr.RData'))
load(file = here('data/sgdat2024.RData'))
hbsgdat <- sgdat2024 |>
filter(FLUCCSCODE %in% c(9113, 9116)) |>
st_transform(st_crs(fimstations)) |>
st_make_valid() |>
mutate(
FLUCCSCODE = case_when(
FLUCCSCODE == 9113 ~ 'Patchy',
FLUCCSCODE == 9116 ~ 'Continuous'
)
)
hbsgdat <- hbsgdat[tbseg |> filter(bay_segment == 'HB'), ]
segshr <- c('OTB', 'HB', 'MTB', 'LTB')
hydrolab <- read.csv(url('https://github.com/tbep-tech/tbni-proc/raw/refs/heads/master/data/FIM_HydroLab_1996-2024.csv'))
bssize <- 13
yrs <- c(2022:2024)
# fim station salinity, temp
hydrodat <- hydrolab |>
filter(Depth == min(Depth), .by = c(Reference, Year, Month)) |>
summarise(
temp = mean(Temperature, na.rm = T),
sal = mean(Salinity, na.rm = T),
.by = c(Reference, Year, Month)
)
#tbni by month, segment
tbnihydro <- tbniscr |>
rename(tbni = TBNI_Score) |>
left_join(hydrodat, by = c('Reference', 'Year', 'Month'))
tbniscrsum <- tbnihydro |>
summarize(
tbni = mean(tbni, na.rm = TRUE),
sal = mean(sal, na.rm = TRUE),
temp = mean(temp, na.rm = TRUE),
Count = n(),
.by = c(Month, Year, bay_segment)
) |>
mutate(
bay_segment = factor(bay_segment, levels = segshr),
date = as.Date(paste(Year, Month, "01", sep = "-"))
)
perc <- c(32, 46)
tomap <- tbniscr |>
mutate(date = as.Date(paste(Year, Month, "01", sep = "-"))) |>
filter(date >= as.Date('2023-12-01') & date <= as.Date('2024-03-01')) |>
filter(bay_segment == 'HB') |>
mutate(
Action = findInterval(TBNI_Score, perc),
outcome = factor(Action, levels = c('0', '1', '2'), labels = c('red', 'yellow', 'green')),
outcome = dplyr::case_when(
outcome == 'green' ~ '#2DC938',
outcome == 'yellow' ~ '#E9C318',
outcome == 'red' ~ '#CC3231'
)
) |>
select(Reference, date, TBNI_Score, outcome, NumTaxa)
tomap <- inner_join(fimstations |> select(-bay_segment), tomap, by = 'Reference')
bsmap <- tbeptools::util_map(tomap, minimap = NULL)
map_fun <- function(tomap, date, bsmap, hbsgdat, show_richness = FALSE){
tomapdt <- tomap |>
mutate(
radius_scaled = scales::rescale(NumTaxa, to = c(5, 18))
) |>
filter(date == !!date)
# Create color palette function for FLUCCSCODE
flucc_pal <- colorFactor(
palette = c("#228B22", "#90EE90"),
domain = c("Patchy", "Continuous")
)
# Set up circle marker parameters based on show_richness
if (show_richness) {
# Create color palette for richness
richness_pal <- colorNumeric(
palette = "Blues",
domain = tomap$NumTaxa
)
circle_params <- list(
fillColor = ~richness_pal(NumTaxa),
radius = ~radius_scaled,
label = ~paste0('Site ', Reference, ' (Richness: ', NumTaxa, ')')
)
# Add richness legend
legend_richness <- TRUE
} else {
circle_params <- list(
fillColor = ~outcome,
radius = 5,
label = ~paste0('Site ', Reference, ' (TBNI: ', round(TBNI_Score, 1), ')')
)
legend_richness <- FALSE
}
map_result <- bsmap |>
addPolygons(
data = hbsgdat,
fillColor = ~flucc_pal(FLUCCSCODE),
fillOpacity = 0.7,
weight = 0, # border width
opacity = 1
) |>
addCircleMarkers(
data = tomapdt,
layerId = ~Reference,
stroke = T,
color = 'black',
fill = TRUE,
fillOpacity = 1,
weight = 1,
fillColor = circle_params$fillColor,
radius = circle_params$radius,
label = circle_params$label
) |>
addLegend(
pal = flucc_pal,
values = hbsgdat$FLUCCSCODE,
title = "Seagrass",
position = "bottomright",
opacity = 1
)
# Add richness legend if needed
if (legend_richness) {
map_result <- map_result |>
addLegend(
pal = richness_pal,
values = tomap$NumTaxa,
title = "Total Richness",
position = "topright",
opacity = 1
)
}
return(map_result)
}