library(tidyverse)
library(sf)
library(terra)
library(tmap)
library(tmaptools)
# Set directory for folder
<- here::here("data", "LC80340322016189-SC20170128091153")
pre_fire_dir
# Create a list of all images that have the extension .tif and contain the word band
<- list.files(pre_fire_dir,
pre_fire_bands pattern = glob2rx("*band*.tif$"),
full.names = TRUE)
# Create a raster stack
<- rast(pre_fire_bands)
pre_fire_rast
# Read mask raster
<- rast(here::here("data", "LC80340322016189-SC20170128091153", "LC80340322016189LGN00_cfmask_crop.tif")) pre_mask
# Set directory for folder
<- here::here("data", "LC80340322016205-SC20170127160728")
post_fire_dir
# Create a list of all images that have the extension .tif and contain the word band
<- list.files(post_fire_dir,
post_fire_bands pattern = glob2rx("*band*.tif$"),
full.names = TRUE)
# Create a raster stack
<- rast(post_fire_bands)
post_fire_rast
# Read mask raster
<- rast(here::here("data", "LC80340322016189-SC20170128091153", "LC80340322016205LGN00_cfmask_crop.tif")) post_mask
<- function(nir, swir2){
nbr_fun - swir2)/(nir + swir2)
(nir }
Rename Bands
<- c("Aerosol", "Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2")
bands names(pre_fire_rast) <- bands
names(post_fire_rast) <- bands
Mask Clouds and Shadows
# Set all cells with values greater than 0 to NA
> 0] <- NA
pre_mask[pre_mask
# Subset raster based on mask
<- mask(pre_fire_rast, mask = pre_mask)
pre_fire_rast
# View raster
plot(pre_fire_rast)
# Set all cells with values greater than 0 to NA
> 0] <- NA
post_mask[post_mask
# Subset raster based on mask
<- mask(post_fire_rast, mask = post_mask)
post_fire_rast
# View raster
plot(post_fire_rast)
Plot True Color Composite
plotRGB(pre_fire_rast, r = 4, g = 3, b = 2, stretch = "lin", colNA = "black")
plotRGB(post_fire_rast, r = 4, g = 3, b = 2, stretch = "lin", colNA = "black")
Plot False Color Composite
plotRGB(pre_fire_rast, r = 7, g = 5, b = 3, stretch = "lin", colNA = "black")
plotRGB(post_fire_rast, r = 7, g = 5, b = 3, stretch = "lin", colNA = "black")
Calculate NBR and dNBR
<- terra::lapp(pre_fire_rast[[c(5, 7)]], fun = nbr_fun)
pre_nbr_rast
plot(pre_nbr_rast, main = "Cold Springs Pre-Fire NBR", colNA = "black")
<- terra::lapp(post_fire_rast[[c(5, 7)]], fun = nbr_fun)
post_nbr_rast
plot(post_nbr_rast, main = "Cold Springs Post-Fire NBR", colNA = "black")
Calculate and Plot dNBR
<- pre_nbr_rast - post_nbr_rast
diff_nbr
tm_shape(diff_nbr) +
tm_raster(style = "equal", n = 6,
palette = get_brewer_pal("YlOrRd", n = 6, plot = FALSE),
title = "Difference NBR (dNBR)", colorNA = "black") +
tm_layout(legend.outside = TRUE)
Reclassification
# Set categories for severity levels
<- c("Enhanced Regrowth", "Unburned", "Low Severity", "Moderate Severity", "High Severity")
categories
# Create reclassification matrix
<- matrix(c(-Inf, -0.1, 1, # group 1 ranges for Enhanced Regrowth
rcl -0.1, 0.1, 2, # group 2 ranges for Unburned
0.1, 0.27, 3, # group 3 ranges for Low Severity
0.27, 0.66, 4, # group 4 ranges for Moderity Severity
0.66, Inf, 5), # group 5 ranges for High Severity
ncol = 3, byrow = TRUE)
# Use reclassification matrix to reclassify dNBR raster
<- classify(diff_nbr, rcl = rcl)
reclassified
is.nan(reclassified)] <- NA reclassified[
tm_shape(reclassified) +
tm_raster(style = "cat",
labels = c(categories, "Missing"),
palette = get_brewer_pal("YlOrRd", n = 5, plot = FALSE),
title = "Severity Level", colorNA = "black")+
tm_layout(legend.outside = TRUE)