<- zonal(r, vect(zones), fun = mean, na.rm = TRUE)
mean_vals
$mean <- mean_vals$elevation zones
Raster-Vector Operations
Two worlds of spatial data
- Till now, we have treated vector and raster data separately
- However, in many cases, you will need to combine both types of data
- For example, take the Zonal operation we discussed in the chapter Zonal: Typically, your “zones” will be vector polygons
Zonal operations with vector data
- The
zonal
function in{terra}
can handle vector data: however, it requiressf
objects to be converted toterra
’s own vector format, calledSpatVector
. - The functionvect()
can be used to convertsf
objects toSpatVector
objects:



Extracting raster values at vector points
- A another common operation is to extract raster values at specific points
- Let’s take the example of the city of Luxembourg (see Global Operation (2))
- The function
extract()
can be used to extract raster values at specific points extract
returns a data.frame with- one column per raster band (1 in our case)
- one row per point (also 1 in our case):
<- extract(r, luxembourg_city)
lux_elev lux_elev
ID elevation
1 1 293.9805
Vector to raster conversion
- Functions that combine raster and vector data usually convert vector to raster internally
- Sometimes, we might want to do this conversion explicitly. This can be done using the
rasterize()
function - This function takes three arguments:
x
: The vector data (either of classsf
orSpatVector
)y
: A raster object that defines the extent, resolution, and CRS of the resulting raster (i.e. a “template”)field
: The name of the column in the vector data that should be used to fill the raster cells
# we can create a template using the input vector. All we have to specify
# is the resolution of the output raster, which is evalutated in the units of
# the CRS of the input vector data (meters in our case).
<- rast(zones, resolution = 1000)
template
<- rasterize(zones, template, "zone") zones_raster


Note
Note that rasters don’t store character information. The above zones are coded as integers with a corresponding look-up table (see ?terra::levels
).
Raster to vector conversion
- The opposite operation, converting raster data to vector data, can be done using the
{terra}
functionsas.points
,as.lines
andas.polygons
: - The resulting object will be of class
SpatVector
. This can be converted to thesf
class usingst_as_sf()
<- as.polygons(zones_raster) |>
zones_poly st_as_sf()


🪕 Tasks
- Import
Arealstatistik.gpkg
from Spatial_Analysis_II using{sf}
- To rasterize the data, we need a template raster object. Create a template raster object with a resolution of 100m using the
rast()
function - Rasterize the arealstatistik data using the function
rasterize
, the tempalte from the previous step, and the columnAS_4
as the field to fill the raster cells
- The resulting raster contains the integer values 1 to 4. These values correspond to the following land use categories (see metadata):
- Settlement and urban areas
- Agricultural areas
- Wooded areas
- Unproductive areas
To make the raster more interpretable, assign the corresponding names to the levels of the raster using the levels()
function. The levels should be a data.frame with two columns: ID
and name
(see below)
<- data.frame(
levs ID = 1:4,
name = c(
"Settlement and urban areas",
"Agricultural areas",
"Wooded areas",
"Unproductive areas"
)
)
levels(areal_rast) <- levs
plot(areal_rast)