I was working on a longer post about calculating flood risk using R, but with the hurricane Melissa approaching Jamaica, a shorter version available right now seems valuable. I´ve seen articles mentioning a 5 m surge caused by the hurricane. Jamaica is a mountainous country and we will use R to find out which areas might be flooded by this temporary sea level rise.
The script should be reusable for other parts of the world with minimum changes, so data download is included in the script.
# Set directory
setwd("C:/Eget/R")
# Load libraries
library(elevatr)
library(geodata)
library(sf)
library(terra)
# Parameters
FLOOD_THRESHOLD <- 5 # 3 m sea level change caused by hurricane
ZOOM_LEVEL <- 13 # zoom level/resolution of elevation data
# Get polygon for Jamaica
world_data <- geodata::world(resolution = 1, path = tempdir())
world_sf <- st_as_sf(world_data) # Convert to sf
jamaica_polygon <- world_sf[world_sf$NAME_0 == "Jamaica", ]
jamaica_polygon <- st_transform(jamaica_polygon, 4326)
# Download elevation data
dem <- get_elev_raster(locations = jamaica_polygon, z = ZOOM_LEVEL, clip = "locations") %>%
rast() %>%
crop(vect(jamaica_polygon)) %>%
mask(vect(jamaica_polygon))
# Compare elevation with flood threshold
flood_risk <- dem <= FLOOD_THRESHOLD
# plet(flood_risk, alpha = 0.5) # View result using interactive map, use 50% transparancy
# Create map
plot(flood_risk, col = c(NA, "red"), axes = TRUE,
main = paste0("Jamaica: Flood risk areas (≤", FLOOD_THRESHOLD, "m)"))
plot(st_geometry(jamaica_polygon), add = TRUE, border = "black", lwd = 2)
# Save to file
png("jamaica_flood_risk.png", width = 3000, height = 2400, res = 300)
plot(flood_risk, col = c(NA, "red"), axes = TRUE,
main = paste0("Jamaica: Flood risk areas (≤", FLOOD_THRESHOLD, "m)"))
plot(st_geometry(jamaica_polygon), add = TRUE, border = "black", lwd = 2)
dev.off()
Downloading the elevation data using ZOOM_LEVEL <- 13 will take a few minutes. Use a lower value for lower resolution, if it takes too long. I used “plet(flood_risk, alpha = 0.5)” to view the result in an interactive map:

The yellow parts are ≤5 m above sea level and at risk in the current scenario. We will use parths of this script in the next post about flood risk mapping.
Make sure you steal the script and run it for your own neighborhood.