Read in packages:

Code
library(sf)
Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
Code
library(terra)
terra 1.7.78
Code
library(spDataLarge)
library(tigris)
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.

Attaching package: 'tigris'
The following object is masked from 'package:terra':

    blocks
Code
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::extract() masks terra::extract()
✖ dplyr::filter()  masks stats::filter()
✖ dplyr::lag()     masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Changing resolution:

Code
r <- rast()
r
class       : SpatRaster 
dimensions  : 180, 360, 1  (nrow, ncol, nlyr)
resolution  : 1, 1  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
Code
values(r) <- 1:ncell(r)
plot(r)

Code
ra <- aggregate(r, 20)
ra
class       : SpatRaster 
dimensions  : 9, 18, 1  (nrow, ncol, nlyr)
resolution  : 20, 20  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
source(s)   : memory
name        :   lyr.1 
min value   :  3430.5 
max value   : 61370.5 
Code
# rarely used
rd <- disagg(r, 20)

|---------|---------|---------|---------|
=
                                          
Code
plot(rd)

Crop and Mask

Code
srtm = rast(system.file("raster/srtm.tif", package = "spDataLarge"))
zion = read_sf(system.file("vector/zion.gpkg", package = "spDataLarge"))
zion = st_transform(zion, crs(srtm))

crs(srtm) == crs(zion)
[1] TRUE
Code
srtm.crop <- crop(x=srtm, y=zion, snap="near")
Code
srtm.crop.msk <- crop(x=srtm, y=vect(zion), snap="near", mask=TRUE)
plot(srtm.crop.msk)

Code
srtm.msk <- mask(srtm.crop, vect(zion), updatevalue=-1000)
plot(srtm.msk)

Code
srtm.msk2 <- mask(srtm.crop, vect(zion), inverse=TRUE, updatevalue=-1000)

Extend

Code
zion.buff <-  zion %>% 
  st_buffer(., 10000)
srtm.ext <- extend(srtm, vect(zion.buff))
ext(srtm.ext)
SpatExtent : -113.343749879444, -112.74541654615, 37.0479167631968, 37.5979167631601 (xmin, xmax, ymin, ymax)
Code
plot(srtm.ext)
plot(st_geometry(zion.buff), add=TRUE)

Practice

Load data:

Code
id <- rast("/opt/data/data/rasterexample/Copy of CRPS_ID.tif")
or <- rast("/opt/data/data/rasterexample/Copy of CRPS_OR.tif")

Aggregate

Code
id_agg <- aggregate(id, fact = 30)

|---------|---------|---------|---------|
=========================================
                                          
Code
or_agg <- aggregate(or, fact = 30)

Check for alignment

Code
crs(id_agg) == crs(or_agg)
[1] TRUE
Code
origin(id_agg) == origin(or_agg)
[1] FALSE FALSE
Code
ext(id_agg) == ext(or_agg)
[1] FALSE

Align the origins

Both project and resample don’t give the intended results.

Code
id_proj <- project(id_agg, or_agg)

id_resamp <- resample(id_agg, or_agg)

par(mfrow = c(1,2))
plot(id_proj)
plot(id_resamp)

We need to extend, then resample. Resample is the faster choice because the CRS’s of the the two rasters already match.

Code
id_ext <- extend(id_agg, or_agg)
or_ext <- extend(or_agg, id_ext)

id_resamp <- resample(id_ext, or_ext)
Code
crs(id_resamp) == crs(or_ext)
[1] TRUE
Code
origin(id_resamp) == origin(or_ext)
[1] TRUE TRUE
Code
ext(id_resamp) == ext(or_ext)
[1] TRUE

Mosaic

Code
idor <- mosaic(id_resamp, or_ext)

Crops and Masks

Code
idor_counties <- counties(state = c("ID", "OR"))
Retrieving data for the year 2022

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |=======================                                               |  34%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |============================================                          |  64%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |======================================================================| 100%
Code
east_id <- idor_counties %>%
  filter(NAME %in% c("Teton", "Jefferson", "Madison"))
plot(st_geometry(east_id))

mask without crop leaves a lot of white space.

Code
east_id_proj <- st_transform(east_id, crs(idor))

east_id_fire <- mask(x = idor, mask = east_id_proj)

east_id_fire2 <- crop(idor, east_id_proj, mask=TRUE)

Read in packages:

Code
library(sf)
library(terra)
library(spDataLarge)
library(tigris)
library(tidyverse)

Changing resolution:

Code
r <- rast()
r
class       : SpatRaster 
dimensions  : 180, 360, 1  (nrow, ncol, nlyr)
resolution  : 1, 1  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
Code
values(r) <- 1:ncell(r)
plot(r)

Code
ra <- aggregate(r, 20)
ra
class       : SpatRaster 
dimensions  : 9, 18, 1  (nrow, ncol, nlyr)
resolution  : 20, 20  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84) 
source(s)   : memory
name        :   lyr.1 
min value   :  3430.5 
max value   : 61370.5 
Code
# rarely used
rd <- disagg(r, 20)

|---------|---------|---------|---------|
=
                                          
Code
plot(rd)

Crop and Mask

Code
srtm = rast(system.file("raster/srtm.tif", package = "spDataLarge"))
zion = read_sf(system.file("vector/zion.gpkg", package = "spDataLarge"))
zion = st_transform(zion, crs(srtm))

crs(srtm) == crs(zion)
[1] TRUE
Code
srtm.crop <- crop(x=srtm, y=zion, snap="near")
Code
srtm.crop.msk <- crop(x=srtm, y=vect(zion), snap="near", mask=TRUE)
plot(srtm.crop.msk)

Code
srtm.msk <- mask(srtm.crop, vect(zion), updatevalue=-1000)
plot(srtm.msk)

Code
srtm.msk2 <- mask(srtm.crop, vect(zion), inverse=TRUE, updatevalue=-1000)

Extend

Code
zion.buff <-  zion %>% 
  st_buffer(., 10000)
srtm.ext <- extend(srtm, vect(zion.buff))
ext(srtm.ext)
SpatExtent : -113.343749879444, -112.74541654615, 37.0479167631968, 37.5979167631601 (xmin, xmax, ymin, ymax)
Code
plot(srtm.ext)
plot(st_geometry(zion.buff), add=TRUE)

Practice

Load data:

Code
id <- rast("/opt/data/data/rasterexample/Copy of CRPS_ID.tif")
or <- rast("/opt/data/data/rasterexample/Copy of CRPS_OR.tif")

Aggregate

Code
id_agg <- aggregate(id, fact = 30)

|---------|---------|---------|---------|
=========================================
                                          
Code
or_agg <- aggregate(or, fact = 30)

Check for alignment

Code
crs(id_agg) == crs(or_agg)
[1] TRUE
Code
origin(id_agg) == origin(or_agg)
[1] FALSE FALSE
Code
ext(id_agg) == ext(or_agg)
[1] FALSE

Align the origins

Both project and resample don’t give the intended results.

Code
id_proj <- project(id_agg, or_agg)

id_resamp <- resample(id_agg, or_agg)

par(mfrow = c(1,2))
plot(id_proj)
plot(id_resamp)

We need to extend, then resample. Resample is the faster choice because the CRS’s of the the two rasters already match.

Code
id_ext <- extend(id_agg, or_agg)
or_ext <- extend(or_agg, id_ext)

id_resamp <- resample(id_ext, or_ext)
Code
crs(id_resamp) == crs(or_ext)
[1] TRUE
Code
origin(id_resamp) == origin(or_ext)
[1] TRUE TRUE
Code
ext(id_resamp) == ext(or_ext)
[1] TRUE

Mosaic

Code
idor <- mosaic(id_resamp, or_ext)

Crops and Masks

Code
idor_counties <- counties(state = c("ID", "OR"))
Retrieving data for the year 2022
Code
east_id <- idor_counties %>%
  filter(NAME %in% c("Teton", "Jefferson", "Madison"))
plot(st_geometry(east_id))

mask without crop leaves a lot of white space.

Code
east_id_proj <- st_transform(east_id, crs(idor))

east_id_fire <- mask(x = idor, mask = east_id_proj)

east_id_fire2 <- crop(idor, east_id_proj, mask=TRUE)