I have a categorical raster and a polygon vector layer. I want to get a table with the area of each class from the raster layer that is covered by polygons.

I tried the following:

extr <- terra::extract(
    raster, polygons, fun = table, exact = TRUE, weights = FALSE

areas <- terra::expanse(polygons, unit = "ha")

tble <- extr
for (c in seq(2, ncol(extr))) {
    tble[, c] <- extr[, c] * polygon_areas

sums <- colSums(tble, na.rm = TRUE)

However, the areas covered by polygons are larger than the areas per class when I run

terra::expanse(terra::as.polygons(raster), unit = "ha")

When I checked the polygons, they were non-overlapping.

Am I using extract in the wrong way here?

1 Answer 1


Nevermind, I figured terra::extract does not return the fraction of polygon area covered (though the number of rows in the output table equals the number of polygons within the vector layer), but the values are given as a fraction of a raster grid cell. Thus, I need to multiply with the raster cell area.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.