1
votes

I have a list of rasters. I would like to create polygons based on the extent of each raster and combine all the newly made polygons into a single object.

I tried the following:

library(tidyverse)
library(raster)
library(sf)

lst(
  raster(ext = extent(20, 21, 10, 11)),
  raster(ext = extent(25, 26, 15, 16))
) %>% 
  map(
    ~ .x %>% 
      pluck("extent") %>% 
      as("SpatialPolygons")
  ) %>% 
  st_union()

but it throws an error because st_union or st_combine don't accept lists.

How can I combine a list of polygons into a multi-polygon object?

1

1 Answers

3
votes

You can do

library(raster)

x <- list(
  raster(ext = extent(20, 21, 10, 11)),
  raster(ext = extent(25, 26, 15, 16))
) 

y <- lapply(x, function(i) as(extent(i), "SpatialPolygons"))
bind(y)