0

My aim was to get the dominant land cover class for a given area using the terra package.

In principle, this can be done with the function extract as for example

library(terra)
library(rworldmap) 
r <- rast(nrows=10, ncols=10)
values(r) <- sample(3, ncell(r), replace=TRUE) 
worldMap <- getMap()
terra::extract(x, map,fun=max)

The problem with this approch, the landcover with the highest numeric value will be selected, and not the dominant class. How to sort this?

1 Answers1

1

The function to use in the extract command is "modal".

Unfortunately, the manual does not mention this option and describes fun of the function extract as a function to summarize the data by geometry. If weights=TRUE or exact=TRUE only mean, sum, min and max are accepted.

library(terra)
library(rworldmap)
set.seed(0)

r <- rast(nrows=10, ncols=10)
values(r) <- sample(3, ncell(r), replace=TRUE)

worldMap <- getMap()
map <- vect(worldMap)
terra::extract(x, map, fun=modal)


Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63