6.5 Rasterization
Vector —> Raster : terra::rasterize()
We are using a data set from spData
:
# the easy part
= spData::cycle_hire_osm
cycle_hire_osm = st_transform(cycle_hire_osm, "EPSG:27700")
cycle_hire_osm_projected # The new part: building a raster template
= terra::rast(ext(cycle_hire_osm_projected), resolution = 1000,
raster_template crs = st_crs(cycle_hire_osm_projected)$wkt)
- First Presence/absence:
= rasterize(cycle_hire_osm_projected, raster_template)
ch_raster1 plot(ch_raster1, col = "yellow")
- Second: count with
length
:
= rasterize(cycle_hire_osm_projected, raster_template,
ch_raster2 fun = "length")
plot(ch_raster2)
- Third: sum of a variable
= terra::rasterize(cycle_hire_osm_projected, raster_template,
ch_raster3 field = "capacity", fun = sum, na.rm = TRUE)
plot(ch_raster3)
rasterize()
has an argument touches
that is useful when dealing with lines or polygon border.