library(sf)
library(terra)
library(spDataLarge)
r <- rast(xmin=-110, xmax=-90, ymin=40, ymax=60, ncols=40, nrows=40)
values(r) <- 1:ncell(r)
plot(r)
HES 505 Fall 2023: Session 13
Image Source: USGS
By the end of today, you should be able to:
Align rasters for spatial processing
Adjust the resolution of raster data
Combine (or reduce) rasters to match the extent of your analysis
resample
Aligning data for later analysis
Remembering scale
Thinking about support
aggregate
, disaggregate
, resample
allow changes in cell size
aggregate
requires a function (e.g., mean()
or min()
) to determine what to do with the grouped values
resample
allows changes in cell size and shifting of cell centers (slower)
|---------|---------|---------|---------|
=======
class : SpatRaster
dimensions : 3600, 7200, 1 (nrow, ncol, nlyr)
resolution : 0.05, 0.05 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84
source : spat_ZJECjHVtroeYIlg_75934.tif
name : lyr.1
min value : 1
max value : 64800
Raster extents often larger than our analysis
Reducing memory and computational resources
Making attractive maps
terra::crop()
Coordinate Reference System must be the same for both objects
Crop is based on the (converted) SpatExtent
of the 2nd object
snap
describes how y
will be aligned to the raster
Returns all data within the extent
terra::crop()
mask()
Often want to get rid of all values outside of vector
Can set mask=TRUE
in crop()
(y
must be SpatVector
)
Or use mask()
mask()
Allows more control over what the mask does
Can set maskvalues
and updatevalues
to change the resulting raster
Can also use inverse
to mask out the vector
Vector slightly larger than raster
Especially when using buffered datasets
Can use extend
Not exact; depends on snap()
SpatExtent : -113.343749879444, -112.74791654615, 37.0479167631968, 37.5979167631601 (xmin, xmax, ymin, ymax)
SpatExtent : -113.343652923976, -112.747986193365, 37.0477357596604, 37.5977812137969 (xmin, xmax, ymin, ymax)