HES 505 Fall 2024: Session 16
Image Source: USGS
By the end of today, you should be able to:
Convert between raster and vector datasets
Generate new rasters describing the spatial arrangement of vector data
Extract raster values as attributes of vector data
Using coercion (as
, rast
, vect
) can change class
, but not data model
Sometimes we need to actually change the data model
rasterize
A special kind of data aggregation
x
is your SpatVector
object
y
is a template raster with the appropriate CRS, resolution, and extent
fun
allows you to specify the value of the resulting raster
rasterize
field
specifies which value should be returned to non-empty cellsrasterize
The fun
argument specifies how we aggregate the data
Useful for counting occurrences (using length
)
rasterize
The fun
argument specifies how we aggregate the data
Can use a variety of functions
rasterize
or stars::st_rasterize
touches
argumentLess common, but can convert to vector data
as.points
, as.countour
, and polygonize
Sometimes we want a raster describing the spatial context of vector data
distance
is a simple method
We’ll use interpolation in the next few weeks
SpatRaster
1 2 3 4 5
1 0.000 209100.3 474603.9 5731.844 422252.6
2 209100.275 0.0 401284.9 204972.864 281571.2
3 474603.876 401284.9 0.0 469036.193 171252.0
4 5731.844 204972.9 469036.2 0.000 416568.2
5 422252.623 281571.2 171252.0 416568.171 0.0
SpatRaster
Sometimes we want to use rasters to create new attributes
fun
controls how the cells are aggregated
zonal
for one summary statistic for polygons user system elapsed
31.39 1.21 32.89
user system elapsed
31.50 1.05 32.73
system.time(wildfire.zones3 <- exactextractr::exact_extract(wildfire_haz, cejst, fun="mean", progress = FALSE))
user system elapsed
3.02 0.12 3.14
WHP_ID
1 3.053172
2 2997.795051
3 6.647930
4 85.971309
5 34.706535
6 17.306250
ID WHP_ID
1 1 3.053172
2 2 2997.795051
3 3 6.647930
4 4 85.971309
5 5 34.706535
6 6 17.306250
[1] 3.230088 2997.102783 6.464695 86.015327 34.672573 16.559727
How do Collaborative Forest Landscape Restoration projects compare to other National Forest lands with respect to social and wildfire risks?
Datasets - Forest Service Boundaries, CFLRP Boundaries, Wildfire Risk Raster, CEJST shapefile
Dependent Variable - CFLRP (T or F)
Independent Variables - Wildfire hazard, income, education, housing burden
code
folderdownload_unzip_read <- function(link){
tmp <- tempfile()
download.file(link, tmp)
tmp2 <- tempfile()
unzip(zipfile=tmp, exdir=tmp2)
shapefile.sf <- read_sf(tmp2)
}
### FS Boundaries
fs.url <- "https://data.fs.usda.gov/geodata/edw/edw_resources/shp/S_USA.AdministrativeForest.zip"
fs.bdry <- download_unzip_read(link = fs.url)
### CFLRP Data
cflrp.url <- "https://data.fs.usda.gov/geodata/edw/edw_resources/shp/S_USA.CFLR_HPRP_ProjectBoundary.zip"
cflrp.bdry <- download_unzip_read(link = cflrp.url)