Search code examples
rgridr-sf

Aggregate grid cells to larger resolution in sf


Consider a grid cell, where each cell is 10x10.

library(sf)
grid1 <- st_make_grid()

plot(grid1)

enter image description here

I would like to create another grid cell, based on grid1, where each cell is 30x30 and consists of 9 10x10 neighboring cells.

I have very few clues as to how to do it, but I'd rather prefer an sf solution. I've tried with st_buffer, but it creates a buffer around each cell. One could perhaps group neighboring cells and aggregate them, but I don't know how to do that either.


Solution

  • Using st_make_grid's cellsize parameter, one can make a grid from an existing one and choose the resolution. Using a cellsize of 30 will indeed group the 8 neighboring cells.

    grid2 <- st_make_grid(grid1, cellsize = 30)
    
    plot(grid2, lwd = 3)
    plot(grid1, add = TRUE, col = NA)
    

    enter image description here


    Note that this works because the grid is made of squares; st_make_grid might not recover the neighboring elements if they are not squares.