library(sf)
library(tmap)
## Warning: package 'tmap' was built under R version 4.4.3
library(readr)
library(dplyr)
library(purrr)
library(scales)
##
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
##
## discard
## The following object is masked from 'package:readr':
##
## col_factor
library(plotly)
## Warning: package 'plotly' was built under R version 4.4.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.4.3
library(mapview)
library(stringr)
library(ggplot2)
library(tidyverse)
library(lubridate)
hurricane_data <- read_csv("./F_Project/HURRICANE/use_Hurricanes.csv") %>%
mutate(
Name = str_trim(Name),
Year = as.integer(Year),
STORM_ID = str_trim(toupper(STORM_ID))
)
## Rows: 65 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): Name, STORM_ID, Start_Date, Peak_Date, End_Date, Ocean, Areas_affec...
## dbl (7): Year, Duration_h, Max_Wind_Speed, Max_Wind_Speed_(mph), Highest_Cat...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
meta_path <- "./F_Project/HURRICANE/L_shapefile_meta_Hurricanes.csv"
meta_data <- read_csv("./F_Project/HURRICANE/L_shapefile_meta_Hurricanes.csv") %>%
mutate(
STORM_ID = str_trim(toupper(STORM_ID))
)
## Rows: 65 Columns: 9
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): Name, STORM_ID, Folder, Basin, PtsFile, LinFile, RadiiFile, windswa...
## dbl (1): Year
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
combined_meta <- meta_data %>%
left_join(hurricane_data, by = "STORM_ID")
base_path <- "./F_Project/HURRICANE/hurricanes"
combined_meta <- combined_meta %>%
mutate(
LinPath = file.path(base_path, Folder, LinFile),
PtsPath = file.path(base_path, Folder, PtsFile),
WindsPath = file.path(base_path, Folder, windswathFile)
)
safe_read_sf <- function(path) {
if (!is.na(path) && file.exists(path)) {
tryCatch(read_sf(path), error = function(e) NULL)
} else {
NULL
}
}
New_Hurricane_base <- combined_meta %>%
mutate(
PointsSF = map(PtsPath, safe_read_sf),
LinesSF = map(LinPath, safe_read_sf),
WindwathSF = map(WindsPath, safe_read_sf)
)
Debby <- New_Hurricane_base %>% filter(STORM_ID == "DEBBY2024") # Ensure correct case
if (nrow(Debby) > 0) {
if (length(Debby$LinesSF) > 0 && !is.null(Debby$LinesSF[[1]])) {
if (length(Debby$PointsSF) > 0 && !is.null(Debby$PointsSF[[1]])) {
if (length(Debby$WindwathSF) > 0 && !is.null(Debby$WindwathSF[[1]])) {
mapview(Debby$LinesSF[[1]], color = "blue") +
mapview(Debby$PointsSF[[1]], col.region = "red") +
mapview(Debby$WindwathSF[[1]], col.region = "yellow")
} else {
message("No valid point spatial data available for DEBBY2024.")
}
} else {
message("No valid line spatial data available for DEBBY2024.")
}
} else {
message("No valid Windwath spatial data available for DEBBY2024.")
}
} else {
message("No data found for hurricane DEBBY2024.")
}
New_Hurricane_base <- combined_meta %>%
mutate(
PointsSF = map(PtsPath, safe_read_sf),
LinesSF = map(LinPath, safe_read_sf),
WindwathSF = map(WindsPath, safe_read_sf)
)
Debby <- New_Hurricane_base %>% filter(STORM_ID == "DEBBY2024")
if (nrow(Debby) > 0) {
debby_map <- mapview()
if (length(Debby$LinesSF) > 0 && !is.null(Debby$LinesSF[[1]])) {
debby_map <- debby_map + mapview(Debby$LinesSF[[1]], color = "blue", layer.name = "Debby Track")
} else {
message("No valid line spatial data available for DEBBY2024.")
}
if (length(Debby$PointsSF) > 0 && !is.null(Debby$PointsSF[[1]])) {
debby_map <- debby_map + mapview(Debby$PointsSF[[1]], col.region = "red", layer.name = "Debby Points")
} else {
message("No valid point spatial data available for DEBBY2024.")
}
if (length(Debby$WindwathSF) > 0 && !is.null(Debby$WindwathSF[[1]])) {
debby_map <- debby_map + mapview(Debby$WindwathSF[[1]], color = "yellow", layer.name = "Debby Windwath")
} else {
message("No valid Windwath spatial data available for DEBBY2024.")
}
if (length(debby_map@map) > 0) {
debby_map
} else {
message("No spatial data to display for DEBBY2024.")
}
} else {
message("No data found for hurricane DEBBY2024.")
}
New_Hurricane_last <- New_Hurricane_base %>%
select(-Name.x, -Year.x) %>%
rename(
Name = Name.y,
Year = Year.y
)
selected_year <- 2020
hurricanes_in_year <- New_Hurricane_last %>%
filter(Year == selected_year)
print(hurricanes_in_year %>% select(STORM_ID, Name, Year))
## # A tibble: 9 × 3
## STORM_ID Name Year
## <chr> <chr> <int>
## 1 DELTA2020 Delta 2020
## 2 DOUGLAS2020 Douglas 2020
## 3 ETA2020 Eta 2020
## 4 HANNA2020 Hanna 2020
## 5 ISAIAS2020 Isaias 2020
## 6 LAURA2020 Laura 2020
## 7 MARCO2020 Marco 2020
## 8 SALLY2020 Sally 2020
## 9 ZETA2020 Zeta 2020
mapview::mapviewOptions(fgb = FALSE)
map_year <- mapview()
for (i in seq_len(nrow(hurricanes_in_year))) {
hurricane_name <- hurricanes_in_year$Name[i]
if (!is.null(hurricanes_in_year$LinesSF[[i]])) {
map_year <- map_year +
mapview(hurricanes_in_year$LinesSF[[i]], color = "blue",
layer.name = hurricane_name)
}
if (!is.null(hurricanes_in_year$PointsSF[[i]])) {
map_year <- map_year +
mapview(hurricanes_in_year$PointsSF[[i]], col.region = "red",
layer.name = paste(hurricane_name, "Points"))
}
if (!is.null(hurricanes_in_year$WindwathSF[[i]])) {
map_year <- map_year +
mapview(hurricanes_in_year$WindwathSF[[i]], col.region = "yellow",
layer.name = paste(hurricane_name, "Windwath"))
}
}
map_year
mapview::mapviewOptions(fgb = FALSE)
map_year <- mapview()
for (i in seq_len(nrow(hurricanes_in_year))) {
hurricane_name <- hurricanes_in_year$Name[i]
random_color <- sample(colors(), 1)
if (!is.null(hurricanes_in_year$LinesSF[[i]])) {
map_year <- map_year +
mapview(hurricanes_in_year$LinesSF[[i]],
color = random_color,
layer.name = hurricane_name,
label = hurricane_name)
}
}
map_year
mapview::mapviewOptions(fgb = FALSE)
map_year <- mapview()
for (i in seq_len(nrow(hurricanes_in_year))) {
hurricane_name <- hurricanes_in_year$Name[i]
random_color <- sample(colors(), 1)
if (!is.null(hurricanes_in_year$WindwathSF[[i]])) {
map_year <- map_year +
mapview(hurricanes_in_year$WindwathSF[[i]],
color = random_color,
layer.name = paste(hurricane_name),
label = hurricane_name)
}
}
map_year
strongest_hurricanes <- New_Hurricane_last %>%
filter(Max_Wind_Speed == max(Max_Wind_Speed, na.rm = TRUE))
print(strongest_hurricanes %>% select(Name, Year, Max_Wind_Speed, Min_Pressure, Damage, Deaths))
## # A tibble: 2 × 6
## Name Year Max_Wind_Speed Min_Pressure Damage Deaths
## <chr> <int> <dbl> <dbl> <chr> <dbl>
## 1 Dorian 2019 160 910 5,000,000,000 78
## 2 Wilma 2005 160 882 21,007,000,000 23
longest_duration <- New_Hurricane_last %>%
filter(Duration_h == max(Duration_h, na.rm = TRUE))
print(longest_duration %>% select(Name, Year, Duration_h))
## # A tibble: 1 × 3
## Name Year Duration_h
## <chr> <int> <dbl>
## 1 Ivan 2004 522
New_Hurricane_stats <- New_Hurricane_last %>%
mutate(
Max_Wind_Speed_mph = Max_Wind_Speed * 1.15078,
Category = cut(Max_Wind_Speed_mph,
breaks = c(-Inf, 38, 73, 96, 113, 136, Inf),
labels = c("TD/TS", "Cat 1", "Cat 2", "Cat 3", "Cat 4", "Cat 5"),
right = FALSE,
include.lowest = TRUE,
ordered_results = TRUE)
)
ggplotly(ggplot(New_Hurricane_stats, aes(x = Year)) +
geom_histogram(binwidth = 1, fill = "red", color = "white") +
labs(title = "Number of US-Affected Hurricanes per Year",
x = "Year",
y = "Number of Hurricanes") +
theme_minimal())
ggplot(New_Hurricane_stats, aes(x = Year)) +
geom_histogram(binwidth = 1, fill = "steelblue", color = "white") +
labs(title = "Number of US-Affected Hurricanes per Year",
x = "Year",
y = "Number of Hurricanes") +
theme_minimal()

ggplotly(ggplot(New_Hurricane_stats, aes(x = Year)) +
geom_histogram(binwidth = 1, fill = "green", color = "white") +
labs(title = "Number of US-Affected Hurricanes per Year",
x = "Year",
y = "Number of Hurricanes") +
theme_minimal())
ggplot(New_Hurricane_stats %>% filter(!is.na(Category)), aes(x = Category, fill = Category)) +
geom_bar() +
labs(title = "Number of US-Affected Hurricanes by Saffir-Simpson Category",
x = "Category",
y = "Number of Hurricanes") +
theme_minimal() +
theme(legend.position = "none")

hurricanes_by_year <- New_Hurricane_stats %>%
group_by(Year) %>%
summarize(Hurricanes = paste(Name, collapse = ", "))
interactive_year_plot_names <- ggplot(New_Hurricane_stats, aes(x = Year)) +
geom_histogram(binwidth = 1, fill = "green", color = "white",
aes(text = paste("Year:", Year, "<br>Hurricanes:",
hurricanes_by_year$Hurricanes[match(Year, hurricanes_by_year$Year)]))) +
labs(title = "Number of US-Affected Hurricanes per Year",
x = "Year",
y = "Number of Hurricanes") +
theme_minimal()
## Warning in geom_histogram(binwidth = 1, fill = "green", color = "white", :
## Ignoring unknown aesthetics: text
ggplotly(interactive_year_plot_names, tooltip = "text")
avg_wind_speed_per_year <- New_Hurricane_stats %>%
group_by(Year) %>%
summarize(Avg_Max_Wind_Speed_mph = mean(Max_Wind_Speed_mph, na.rm = TRUE))
ggplot(avg_wind_speed_per_year, aes(x = Year, y = Avg_Max_Wind_Speed_mph)) +
geom_line(color = "firebrick") +
geom_point(color = "firebrick") +
labs(title = "Average Maximum Wind Speed of US-Affected Hurricanes per Year",
x = "Year",
y = "Average Maximum Wind Speed (MPH)") +
theme_minimal()

wind_speed_bins <- seq(floor(min(New_Hurricane_stats$Max_Wind_Speed_mph, na.rm = TRUE) / 10) * 10,
ceiling(max(New_Hurricane_stats$Max_Wind_Speed_mph, na.rm = TRUE) / 10) * 10,
by = 10)
hurricane_names_by_wind_speed <- New_Hurricane_stats %>%
mutate(Wind_Speed_Bin = cut(Max_Wind_Speed_mph, breaks = wind_speed_bins, include.lowest = TRUE)) %>%
group_by(Wind_Speed_Bin) %>%
summarize(Hurricanes = paste(Name, collapse = ", "))
interactive_wind_speed_hist <- ggplot(New_Hurricane_stats, aes(x = Max_Wind_Speed_mph)) +
geom_histogram(binwidth = 10, fill = "darkorange", color = "white",
aes(text = paste("Wind Speed (MPH): [", floor(..x..), ", ", ceiling(..x..), ")<br>",
"Count:", ..count.., "<br>",
"Hurricanes:",
hurricane_names_by_wind_speed$Hurricanes[match(as.character(cut(..x.., breaks = wind_speed_bins, include.lowest = TRUE)),
as.character(hurricane_names_by_wind_speed$Wind_Speed_Bin))]))) +
labs(title = "Distribution of Maximum Wind Speeds (MPH)",
x = "Maximum Wind Speed (MPH)",
y = "Count") +
theme_minimal()
## Warning in geom_histogram(binwidth = 10, fill = "darkorange", color = "white",
## : Ignoring unknown aesthetics: text
ggplotly(interactive_wind_speed_hist, tooltip = "text")
## Warning: The dot-dot notation (`..x..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(x)` instead.
## ℹ The deprecated feature was likely used in the base package.
## Please report the issue to the authors.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
duration_bins <- seq(0, ceiling(max(New_Hurricane_stats$Duration_h, na.rm = TRUE) / 24) * 24, by = 24)
hurricane_names_by_duration <- New_Hurricane_stats %>%
mutate(Duration_Bin = cut(Duration_h, breaks = duration_bins, include.lowest = TRUE)) %>%
group_by(Duration_Bin) %>%
summarize(Hurricanes = paste(Name, collapse = ", "))
interactive_duration_hist <- ggplot(New_Hurricane_stats, aes(x = Duration_h)) +
geom_histogram(binwidth = 24, fill = "forestgreen", color = "white",
aes(text = paste("Duration (Hours): [", floor(..x..), ", ", ceiling(..x..), ")<br>",
"Count:", ..count.., "<br>",
"Hurricanes:",
hurricane_names_by_duration$Hurricanes[match(as.character(cut(..x.., breaks = duration_bins, include.lowest = TRUE)),
as.character(hurricane_names_by_duration$Duration_Bin))]))) +
labs(title = "Distribution of Hurricane Duration",
x = "Duration (Hours)",
y = "Count") +
theme_minimal()
## Warning in geom_histogram(binwidth = 24, fill = "forestgreen", color = "white",
## : Ignoring unknown aesthetics: text
ggplotly(interactive_duration_hist, tooltip = "text")
total_duration_per_year <- New_Hurricane_stats %>%
group_by(Year) %>%
summarize(Total_Duration_h = sum(Duration_h, na.rm = TRUE))
ggplot(total_duration_per_year, aes(x = Year, y = Total_Duration_h)) +
geom_line(color = "purple") +
geom_point(color = "purple") +
labs(title = "Total Duration of US-Affected Hurricanes per Year",
x = "Year",
y = "Total Duration (Hours)") +
theme_minimal()

hurricane_data <- hurricane_data %>%
mutate(Damage = ifelse(Damage == "None", NA, as.numeric(str_replace_all(Damage, ",", ""))))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Damage = ifelse(...)`.
## Caused by warning in `ifelse()`:
## ! NAs introduced by coercion
ggplot(hurricane_data, aes(x = `Max_Wind_Speed_(mph)`, y = Damage)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(title = "Hurricane Damage vs. Maximum Wind Speed",
x = "Maximum Wind Speed (mph)",
y = "Damage (USD)") +
theme_minimal() +
scale_y_continuous(labels = scales::comma, na.value = NA)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 14 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 14 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(hurricane_data, aes(x = Year, y = Deaths)) +
geom_point() +
geom_smooth(method = "loess", se = FALSE, color = "blue") +
labs(title = "Hurricane-Related Deaths Over Time",
x = "Year",
y = "Number of Deaths") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

ggplot(hurricane_data, aes(x = `Max_Wind_Speed_(mph)`, y = Duration_h)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE, color = "purple") +
labs(title = "Hurricane Duration vs. Maximum Wind Speed",
x = "Maximum Wind Speed (mph)",
y = "Duration (Hours)") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

selected_year <- 2020
hurricanes_in_year <- New_Hurricane_last %>%
filter(Year == selected_year)
print(hurricanes_in_year %>% select(Name, Deaths))
## # A tibble: 9 × 2
## Name Deaths
## <chr> <dbl>
## 1 Delta 4
## 2 Douglas 0
## 3 Eta 165
## 4 Hanna 9
## 5 Isaias 17
## 6 Laura 41
## 7 Marco 0
## 8 Sally 4
## 9 Zeta 9
all_affected_states <- hurricane_data %>%
pull(Areas_affected) %>%
str_split(",|AND") %>%
unlist() %>%
str_trim() %>%
str_to_upper() %>%
unique()
message("All States Affected by Hurricanes from 2004 to 2024:")
## All States Affected by Hurricanes from 2004 to 2024:
print(all_affected_states)
## [1] "NORTH CAROLINA" "FLORIDA" "GEORGIA"
## [4] "SOUTH CAROLINA" "ARKANSAS" "LOUISIANA"
## [7] "MISSISSIPPI" "MISSOURI" "TEXAS"
## [10] "HAWAII" "DELAWARE" "MARYLAND"
## [13] "MASSACHUSETTS" "RHODE ISLAND" "VIRGINIA"
## [16] "ALABAMA" "MAINE" "NEW YORK"
## [19] "PENNSYLVANIA" "WEST VIRGINIA" "TENNESSEE"
## [22] "OHIO" "NEW MEXICO" "ARIZONA"
## [25] "" "CONNETICUT" "NEW JERSEY"
## [28] "KENTUCKY" "PUERTO RICO" "ILLINOIS"
## [31] "INDIANA" "MICHIGAN" "CONNECTICUT"
## [34] "VERMONT" "NEW HAMPSHIRE" "DISTRICT OF COLUMBIA"
state_counts <- hurricane_data %>%
pull(Areas_affected) %>%
str_split(",|AND") %>%
unlist() %>%
str_trim() %>%
str_to_upper() %>%
table() %>%
as.data.frame() %>%
setNames(c("State", "Count")) %>%
arrange(desc(Count))
message("\nFrequency of States Affected:")
##
## Frequency of States Affected:
print(state_counts)
## State Count
## 1 FLORIDA 34
## 2 NORTH CAROLINA 25
## 3 GEORGIA 24
## 4 SOUTH CAROLINA 23
## 5 LOUISIANA 19
## 6 ALABAMA 18
## 7 MISSISSIPPI 16
## 8 VIRGINIA 16
## 9 NEW YORK 15
## 10 HAWAII 14
## 11 MARYLAND 14
## 12 DELAWARE 12
## 13 TENNESSEE 12
## 14 TEXAS 12
## 15 MASSACHUSETTS 11
## 16 KENTUCKY 10
## 17 NEW JERSEY 10
## 18 PENNSYLVANIA 9
## 19 ARKANSAS 8
## 20 MAINE 8
## 21 RHODE ISLAND 8
## 22 WEST VIRGINIA 8
## 23 MISSOURI 6
## 24 OHIO 6
## 25 ILLINOIS 5
## 26 INDIANA 5
## 27 CONNETICUT 4
## 28 NEW HAMPSHIRE 4
## 29 VERMONT 4
## 30 CONNECTICUT 3
## 31 DISTRICT OF COLUMBIA 2
## 32 MICHIGAN 2
## 33 PUERTO RICO 2
## 34 1
## 35 ARIZONA 1
## 36 NEW MEXICO 1
hurricane_data_expanded <- hurricane_data %>%
mutate(Num_Affected_States = str_count(Areas_affected, ",|AND") + 1)
most_affected_hurricane <- hurricane_data_expanded %>%
top_n(1, Num_Affected_States)
message("\nHurricane Affecting the Most States:")
##
## Hurricane Affecting the Most States:
print(most_affected_hurricane %>% select(Name, Year, Num_Affected_States, Areas_affected))
## # A tibble: 1 × 4
## Name Year Num_Affected_States Areas_affected
## <chr> <int> <dbl> <chr>
## 1 Ivan 2004 24 Alabama, Florida, Louisiana, Mississippi, Tex…
find_hurricanes_by_state <- function(state_name, data) {
state_name <- str_to_upper(state_name)
hurricanes <- data %>%
filter(str_detect(Areas_affected, state_name)) %>%
select(Name, Year, Areas_affected)
if(nrow(hurricanes) == 0){
return(paste("No hurricanes found that affected",state_name))
}
return(hurricanes)
}
texas_hurricanes <- find_hurricanes_by_state("Texas", hurricane_data)
message("\nHurricanes that affected Texas:")
##
## Hurricanes that affected Texas:
print(texas_hurricanes)
## [1] "No hurricanes found that affected TEXAS"
calculate_average_affected_by_year <- function(year, data) {
hurricane_year_data <- data %>%
filter(Year == year)
if (nrow(hurricane_year_data) == 0) {
return(paste("No hurricanes found for the year", year))
}
average_states_affected_year <- hurricane_year_data %>%
mutate(Num_Affected_States = str_count(Areas_affected, ",|AND") + 1) %>%
summarize(mean_states_affected = mean(Num_Affected_States)) %>%
pull(mean_states_affected)
return(round(average_states_affected_year, 2))
}
user_year <- as.integer(readline(prompt = "Enter the year to calculate the average number of affected states: "))
## Enter the year to calculate the average number of affected states:
average_affected_for_year <- calculate_average_affected_by_year(user_year, hurricane_data)
message(paste("\nAverage Number of States Affected by Hurricanes in", user_year, ":", average_affected_for_year))
##
## Average Number of States Affected by Hurricanes in NA : No hurricanes found for the year NA
average_states_affected <- hurricane_data_expanded %>%
summarize(mean_states_affected = mean(Num_Affected_States)) %>%
pull(mean_states_affected)
message("\nAverage Number of States Affected by a Hurricane:")
##
## Average Number of States Affected by a Hurricane:
print(round(average_states_affected, 2))
## [1] 5.72
visualize_hurricane_track <- function(storm_id, data) {
hurricane <- data %>% filter(toupper(STORM_ID) == toupper(storm_id))
if (nrow(hurricane) > 0) {
map_hurricane <- mapview()
for (i in seq_len(nrow(hurricane))) {
if (!is.null(hurricane$LinesSF[[i]]) && inherits(hurricane$LinesSF[[i]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(hurricane$LinesSF[[i]], color = "blue",
layer.name = paste(hurricane$Name[i], "Track"))
} else {
message(paste("No valid line spatial data for", hurricane$Name[i],
"(", hurricane$STORM_ID[i], ")"))
}
if (!is.null(hurricane$PointsSF[[i]]) && inherits(hurricane$PointsSF[[i]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(hurricane$PointsSF[[i]], col.region = "red",
layer.name = paste(hurricane$Name[i], "Points"))
} else {
message(paste("No valid point spatial data for", hurricane$Name[i],
"(", hurricane$STORM_ID[i], ")"))
}
if (!is.null(hurricane$WindwathSF[[i]]) && inherits(hurricane$WindwathSF[[i]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(hurricane$WindwathSF[[i]], col.region = "yellow",
layer.name = paste(hurricane$Name[i], "Windswath"))
} else {
message(paste("No valid Windwath spatial data for", hurricane$Name[i],
"(", hurricane$STORM_ID[i], ")"))
}
}
print(map_hurricane)
} else {
message(paste("No data found for hurricane with STORM_ID:", storm_id))
}
}
visualize_hurricane_track("Nate2017", New_Hurricane_last)
#visualize_hurricane_track("Nate2017", New_Hurricane_base)
visualize_hurricane_track_by_name_and_year <- function(hurricane_name, data) {
hurricanes <- data %>%
filter(str_to_lower(Name) == str_to_lower(hurricane_name))
if (nrow(hurricanes) > 0) {
if (nrow(hurricanes) > 1) {
cat("Found multiple hurricanes with the name '", hurricane_name, "'. Please select the year:\n")
for (i in seq_len(nrow(hurricanes))) {
cat(i, ": ", hurricanes$Year[i], "\n", sep = "")
}
year_choice <- as.integer(readline(prompt = "Enter the number corresponding to the year: "))
if (year_choice %in% seq_len(nrow(hurricanes))) {
selected_hurricane <- hurricanes[year_choice, ]
message(paste("Visualizing track for", selected_hurricane$Name, "(", selected_hurricane$Year, ")"))
map_hurricane <- mapview()
if (!is.null(selected_hurricane$LinesSF[[1]]) && inherits(selected_hurricane$LinesSF[[1]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(selected_hurricane$LinesSF[[1]], color = "blue",
layer.name = paste(selected_hurricane$Name, selected_hurricane$Year, "Track"))
} else {
message(paste("No valid line spatial data for", selected_hurricane$Name,
"(", selected_hurricane$STORM_ID, ")"))
}
if (!is.null(selected_hurricane$PointsSF[[1]]) && inherits(selected_hurricane$PointsSF[[1]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(selected_hurricane$PointsSF[[1]], col.region = "red",
layer.name = paste(selected_hurricane$Name, selected_hurricane$Year, "Points"))
} else {
message(paste("No valid point spatial data for", selected_hurricane$Name,
"(", selected_hurricane$STORM_ID, ")"))
}
if (!is.null(selected_hurricane$WindwathSF[[1]]) && inherits(selected_hurricane$WindwathSF[[1]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(selected_hurricane$WindwathSF[[1]], col.region = "yellow",
layer.name = paste(selected_hurricane$Name, selected_hurricane$Year, "Windwath"))
} else {
message(paste("No valid Windwath spatial data for", selected_hurricane$Name,
"(", selected_hurricane$STORM_ID, ")"))
}
print(map_hurricane)
} else {
message("Invalid year choice.")
}
} else {
selected_hurricane <- hurricanes[1,]
message(paste("Visualizing track for", selected_hurricane$Name, "(", selected_hurricane$Year, ")"))
map_hurricane <- mapview()
if (!is.null(selected_hurricane$LinesSF[[1]]) && inherits(selected_hurricane$LinesSF[[1]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(selected_hurricane$LinesSF[[1]], color = "blue",
layer.name = paste(selected_hurricane$Name, selected_hurricane$Year, "Track"))
} else {
message(paste("No valid line spatial data for", selected_hurricane$Name,
"(", selected_hurricane$STORM_ID, ")"))
}
if (!is.null(selected_hurricane$PointsSF[[1]]) && inherits(selected_hurricane$PointsSF[[1]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(selected_hurricane$PointsSF[[1]], col.region = "red",
layer.name = paste(selected_hurricane$Name, selected_hurricane$Year, "Points"))
} else {
message(paste("No valid point spatial data for", selected_hurricane$Name,
"(", selected_hurricane$STORM_ID, ")"))
}
if (!is.null(selected_hurricane$WindwathSF[[1]]) && inherits(selected_hurricane$WindwathSF[[1]], 'sf')) {
map_hurricane <- map_hurricane +
mapview(selected_hurricane$WindwathSF[[1]], col.region = "yellow",
layer.name = paste(selected_hurricane$Name, selected_hurricane$Year, "Windwath"))
} else {
message(paste("No valid Windwath spatial data for", selected_hurricane$Name,
"(", selected_hurricane$STORM_ID, ")"))
}
print(map_hurricane)
}
} else {
message(paste("No hurricanes found with the name '", hurricane_name, "'."))
}
}
user_input <- readline(prompt = "Enter the name of a hurricane to visualize: ")
## Enter the name of a hurricane to visualize:
if (str_trim(user_input) != "") {
visualize_hurricane_track_by_name_and_year(user_input, New_Hurricane_last)
} else {
message("No hurricane name entered.")
}
## No hurricane name entered.