library(tidyverse)
library(spData)
library(spDataLarge)
library(sf)
library(stars)
library(terra)
library(kableExtra)
Plot a Histogram and Boxplot of dem
<- terra::rast(system.file("raster/dem.tif", package = "spDataLarge"))
dem <- terra::rast(system.file("raster/landsat.tif", package = "spDataLarge"))
landsat <- terra::rast(system.file("raster/srtm.tif", package = "spDataLarge"))
srtm <- stars::read_stars(here::here("data", "week4-discussion", "PER_elv.tif")) peru
Plot a histogram and boxplot of dem
hist(dem,
main = "Digital Elevation Model Raster Value Distribution",
xlab = "Value")
boxplot(dem,
main = "Digital Elevation Model Raster Value Distribution",
ylab = "Value")
Reclassify Elevation and Find Mean
# define a reclassification matrix
<- matrix(c(-Inf, 300, 0, # values -Inf to 300 = 0
rcl 300, 500, 1, # values 300 to 500 = 1
500, Inf, 2), # values 500 to Inf = 2
ncol = 3, byrow = TRUE)
# apply the matrix to reclassify the raster, making all cells 0 or 1 or 2
<- terra::classify(dem, rcl = rcl)
dem_rcl
# assign labels to the numerical categories
levels(dem_rcl) <- tibble::tibble(id = 0:2,
cats = c("low", "medium", "high"))
# calculate mean elevation for each category using original DEM values
<- terra::zonal(dem, dem_rcl, fun = "mean")
elevation_mean elevation_mean
cats dem
1 low 274.3910
2 medium 392.0486
3 high 765.2197
Find Correlation Between NDWI and NDVI
Define functions for calculating NDWI and NDVI
<- function(green, nir){
ndwi_fun - nir)/(green + nir)
(green
}
<- function(nir, red){
ndvi_fun - red)/(nir + red)
(nir }
Apply the functions to the appropriate Landsat 8 bands. Landsat 8 bands 2-5 correspond to bands 1-4 for this raster. Bands are as follows:
Band | Color | |
---|---|---|
1 | blue | 30 meter |
2 | green | 30 meter |
3 | red | 30 meter |
4 | near-infrared | 30 meter |
<- terra::lapp(landsat[[c(2, 4)]],
ndwi_rast fun = ndwi_fun)
plot(ndwi_rast,
main = "Zion National Park NDWI")
<- terra::lapp(landsat[[c(4, 3)]],
ndvi_rast fun = ndvi_fun)
# stack rasters
<- c(ndvi_rast, ndwi_rast)
combine
plot(combine, main = c("NDVI", "NDWI"))
# calculate the correlation between raster layers
::layerCor(combine, fun = cor) terra
[,1] [,2]
[1,] 1.0000000 -0.9132838
[2,] -0.9132838 1.0000000
Find Distances Across All peru
Cells
# Aggregate by a factor of 20 to reduce resolution and create new raster
<- terra::aggregate(rast(peru), fact = 20)
peru_agg plot(peru_agg)
# Create mask of ocean (NA values)
<- is.na(peru_agg) # returns TRUE value for NA
water_mask # Set all FALSE values to NA
== 0] <- NA
water_mask[water_mask plot(water_mask)
# Find distance from each cell to ocean/coastline (default is unit = "m")
<- terra::distance(water_mask) distance_to_coast
|---------|---------|---------|---------|
=========================================
# Convert from meters to kilometers
<- distance_to_coast/1000
distance_to_coast_km
plot(distance_to_coast_km, main = "Distance to the coast (km)")
Change Resolution of srtm
plot(srtm)
<- terra::rast(terra::ext(srtm), res = 0.01)
rast_template
<- terra::resample(srtm, y = rast_template, method = "bilinear")
srtm_resampl1 <- terra::resample(srtm, y = rast_template, method = "near")
srtm_resampl2 <- terra::resample(srtm, y = rast_template, method = "cubic")
srtm_resampl3 <- terra::resample(srtm, y = rast_template, method = "cubicspline")
srtm_resampl4 <- terra::resample(srtm, y = rast_template, method = "lanczos")
srtm_resampl5
<- c(srtm_resampl1, srtm_resampl2, srtm_resampl3, srtm_resampl4, srtm_resampl5)
srtm_resampl_all <- c("Bilinear", "Near", "Cubic", "Cubic Spline", "Lanczos")
labs plot(srtm_resampl_all, main = labs)