jma / global.R
alexdum's picture
feat: implement OpenFreeMap vector styles and a hybrid Sentinel-2 basemap with dynamic label visibility, replacing previous basemap options.
07004f3
library(shiny)
library(mapgl)
library(sf)
library(dplyr)
library(readr)
library(bslib)
library(bsicons)
library(ggplot2)
library(plotly)
library(writexl)
# OpenFreeMap Style URLs
ofm_positron_style <- "https://tiles.openfreemap.org/styles/positron"
ofm_bright_style <- "https://tiles.openfreemap.org/styles/bright"
# EOX Sentinel-2 Cloudless (Free for commercial use with attribution)
sentinel_url <- "https://tiles.maps.eox.at/wmts/1.0.0/s2cloudless-2023_3857/default/GoogleMapsCompatible/{z}/{y}/{x}.jpg"
sentinel_attribution <- '<a href="https://s2maps.eu" target="_blank">Sentinel-2 cloudless - by EOX IT Services GmbH</a> (Contains modified Copernicus Sentinel data 2023)'
# Load Data
stations_file <- "data/jma_stations_full.csv"
if (!file.exists(stations_file)) {
stop("Data file not found. Please run setup_data.R first.")
}
stations <- read_csv(
stations_file,
show_col_types = FALSE,
col_types = cols(
ID = col_character(),
prec_no = col_character(),
ObservedVariables = col_character(),
Start_Daily = col_double(),
Start_Hourly = col_double(),
Start_10min = col_double(),
Start_Monthly = col_double(),
End_Daily = col_double(),
End_Hourly = col_double(),
End_10min = col_double(),
End_Monthly = col_double()
)
)
cols_needed <- c("End_Daily", "End_Hourly", "End_10min", "End_Monthly")
for (c in cols_needed) {
if (!c %in% names(stations)) stations[[c]] <- NA_real_
}
stations <- stations %>%
mutate(
station_type = case_when(
grepl("AMeDAS", ObservedVariables, ignore.case = TRUE) ~ "a",
TRUE ~ tolower(substr(Type, 1, 1))
),
)
# JMA Prefecture Mapping (ID -> Name)
jma_pref_map <- c(
"11" = "Hokkaido (Soya)", "12" = "Hokkaido (Rumoi)", "13" = "Hokkaido (Kamikawa)",
"14" = "Hokkaido (Abashiri)", "15" = "Hokkaido (Kitami)", "16" = "Hokkaido (Tokachi)",
"17" = "Hokkaido (Kushiro)", "18" = "Hokkaido (Nemuro)", "19" = "Hokkaido (Iburi)",
"20" = "Hokkaido (Hidaka)", "21" = "Hokkaido (Oshima)", "22" = "Hokkaido (Hiyama)",
"23" = "Hokkaido (Shiribeshi)", "24" = "Hokkaido (Ishikari)", "31" = "Aomori",
"32" = "Akita", "33" = "Iwate", "34" = "Miyagi", "35" = "Yamagata", "36" = "Fukushima",
"40" = "Ibaraki", "41" = "Tochigi", "42" = "Gunma", "43" = "Saitama", "44" = "Tokyo",
"45" = "Chiba", "46" = "Kanagawa", "48" = "Nagano", "49" = "Yamanashi", "50" = "Shizuoka",
"51" = "Aichi", "52" = "Gifu", "53" = "Mie", "54" = "Niigata", "55" = "Toyama",
"56" = "Ishikawa", "57" = "Fukui", "60" = "Shiga", "61" = "Kyoto", "62" = "Osaka",
"63" = "Hyogo", "64" = "Nara", "65" = "Wakayama", "66" = "Tottori", "67" = "Shimane",
"68" = "Okayama", "69" = "Hiroshima", "71" = "Tokushima", "72" = "Kagawa",
"73" = "Ehime", "74" = "Kochi", "81" = "Yamaguchi", "82" = "Fukuoka", "83" = "Oita",
"84" = "Saga", "85" = "Nagasaki", "86" = "Kumamoto", "87" = "Miyazaki", "88" = "Kagoshima",
"91" = "Okinawa"
)
stations <- stations %>%
mutate(
station_type = case_when(
grepl("AMeDAS", ObservedVariables, ignore.case = TRUE) ~ "a",
TRUE ~ tolower(substr(Type, 1, 1))
),
DisplayName = ifelse(is.na(Name_EN), Name_JP, paste0(Name_EN, " (", Name_JP, ")")),
PrecName = jma_pref_map[prec_no],
PrecLabel = ifelse(is.na(PrecName), paste("ID:", prec_no), paste0(PrecName, " (", prec_no, ")")),
# Translate Variable Names
ObservedVariables = ObservedVariables %>%
gsub("海面気圧", "Pressure", .) %>%
gsub("現地気圧", "Pressure", .) %>%
gsub("気圧", "Pressure", .) %>%
gsub("気温", "Temperature", .) %>%
gsub("湿度", "Humidity", .) %>%
gsub("風向・風速", "Wind", .) %>%
gsub("風", "Wind", .) %>%
gsub("日照時間", "Sunshine", .) %>%
gsub("降水量", "Precipitation", .) %>%
gsub("積雪", "Snow Depth", .) %>%
gsub("天気", "Weather", .)
)
print(paste("Global stations loaded:", nrow(stations)))
print("Start_10min Summary:")
print(summary(stations$Start_10min))