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 2024: Session 12
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)
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.74541654615, 37.0479167631968, 37.5979167631601 (xmin, xmax, ymin, ymax)
SpatExtent : -113.343652923976, -112.745006809213, 37.0477357596604, 37.5977812137969 (xmin, xmax, ymin, ymax)
mosaic
is a funciton that combines adjacent rasters, but they need to have the same origin and resolution. Let’s practice preparing some rasters for a mosaic.
Load wildfire hazard data from the rasterexample
folder for OR and ID: Copy of CRPS_OR.tif
and Copy of CRPS_ID.tif
.
These rasters have a fine resolution that will make our calculations slow. Transform them to have a resolution of 900 m.
Do the rasters have the same CRS, origin, resolution, and extent? Check this with ==
.
Use new functions from this lecture to align the properties mentioned in #3 (plot often to check your work). Why not use project
?
mosaic
the two rasters together.