Raster Data: II

HES 505 Fall 2024: Session 13

Carolyn Koehn

Objectives

  • By the end of today, you should be able to:

    • Use moving windows as a means of smoothing raster data

    • Reclassify data using conditional statements and reclassification tables

    • Use raster math as a means of creating new data based on an existing dataset.

Moving Windows

Why use moving windows?

  • To create new data that reflects “neighborhood” data

  • To smooth out values

  • To detect (and fill) holes or edges

  • Change the thematic scale of your data (without changing resolution)

What is a moving window?

Implementing Moving Windows in R

  • Use the focal function in terra

focal(x, w=3, fun="sum", ..., na.policy="all", fillvalue=NA, expand=FALSE, silent=TRUE, filename="", overwrite=FALSE, wopt=list())

focal for Continuous Rasters

library(tidyverse)
library(terra)
library(spData)
srtm = rast(system.file("raster/srtm.tif", package = "spDataLarge"))
srtm3  <-  focal(x = srtm, w = 3)
srtm9  <-  focal(x = srtm, w = 9)
srtm21  <-  focal(x = srtm, w = 21)

focal for Continuous Rasters

focal for Continuous Rasters

srtmsum  <-  focal(x = srtm, w = 3, fun="sum")
srtmmax  <-  focal(x = srtm, w = 9, fun="mean")
srtmmin  <-  focal(x = srtm, w = 21, fun="min")

focal for Continuous Rasters

focal for Continous Rasters

  • can alter the size and shape of window by providing a weights matrix for w

  • Can create different custom functions for fun (see the help file)

  • na.policy for filling holes or avoiding them

Reclassification

Reclassification

  • Create new data based on the presence of a particular class(es) of interest

  • Combine classes in a categorical map

  • Useful as inputs for overlay analyses

Reclassifying rasters in R

  • Using [] and conditionals
srtm = rast(system.file("raster/srtm.tif", package = "spDataLarge"))
srtm.lowelev <- srtm
srtm.lowelev[srtm.lowelev > 2500] <- 1
plot(srtm.lowelev)

Reclassifying rasters in R

  • Using [] and conditionals
srtm = rast(system.file("raster/srtm.tif", package = "spDataLarge"))


srtm.na <- srtm
srtm.na[200:300, 200:300] <- NA
srtm.na[is.na(srtm.na)] <- 8000
plot(srtm.na)

Reclassifying Categorical Rasters

  • Need a classification matrix
  • Use classify
mintemp <- rast("ftp://ftp.hafro.is/pub/data/rasters/Iceland_minbtemp.tif")
cm <- matrix(c(
  -2, 2, 0,
  2, 4, 1,
  4, 10, 2), ncol = 3, byrow = TRUE)

# Create a raster with integers
temp_reclass <- classify(mintemp, cm)
tempcats <- c("cold", "mild", "warm")
levels(temp_reclass) <- tempcats

Reclassifying Categorical Rasters

Raster Math

  • Performs cell-wise calculations on 1 (or more) SpatRasters
  • Generally works the same as matrix operations
  • All layers must be aligned

Raster Math

r <- rast(ncol=5, nrow=5)
values(r) <- 1:ncell(r)
r2 <- r*2
r3 <- t(r)
r4 <- r + r2

Cell-wise operations

  • terra has a special set of apply functions

  • app, lapp, tapp

  • app applies a function to the values of each cell

  • lapp applies a function using the layer as the value

  • tapp applies the function to a subset of layers

Context-specific Functions

  • distance and relatives are based on relationships between cells

  • terrain allows calculation of slope, ruggedness, aspect using elevation rasters

  • shade calculates hillshade based on terrain

Practice

You are tasked with the following (admittedly silly) analysis by the “Wildfire for Insurance Agents” group. They would like a map of wildfire risk in Idaho with these categories: “No worries,” “A little bit risky,” “Moderate risk,” and “Very risky,” and “Don’t move here.” They don’t want any stakeholders to feel that their properties stand out unfairly, so they don’t want pixels that are different from the pixels around them. Choose a county to present this analysis to and make a map for that audience as well as the state map.

Objectives

  • You should be able to:

    • Use moving windows as a means of smoothing raster data

    • Reclassify data using conditional statements and reclassification tables

    • Use raster math as a means of creating new data based on an existing dataset.