Search code examples
rspatialshapefiler-sfrasterize

How define the cell size from a hexagonal grid?


I'm trying to create a hexagonal grid from a biome boundaries (cerrado, but it could be able to apply to any other political boundaries). In sf documentation I saw that it is possible to do it with the st_make_grid function, but does not clear how the size cell are defined. I would like to make a hexagonal grid with 25km2 size cell.

I've tried to use this function:

grid = sf::st_make_grid(x= shpCerrado, cellsize = 0.4, square = FALSE)

I used the size 0.4 because a friend compared in QGis and told that this size had a 25m side. But we dont have sure about the total area of the cell (which we would like that was 25km2).

I tried to find some package docs or another tutorial that explained how to define the cell area of a hexagonal grid, but I couldn't. Does anyone know how this calculation could be done? Or is there another way to make this grid in R?


Solution

  • You can derive the target cell size from the generic formula for area of hexagon; with a little algebra you will arrive at sqrt(2*cell_area/sqrt(3))

    Or consider this piece of code; it covers county Ashe from the well known nc.shp that ships with {sf} in a 25 km² grid.

    library(sf)
    library(dplyr)
    
    nc <- st_read(system.file("shape/nc.shp", package="sf")) %>% 
      slice(1) %>% # coiunty Ashe only
      st_geometry() %>% # no need for data anymore
      st_transform(3857) # transform to a metric CRS 
    
    cell_area <- units::as_units(25, "km^2") # target grid size
    
    grid_spacing <- sqrt(2*cell_area/sqrt(3)) # size of hexagon calculated from area
    
    grid <- nc %>% 
      st_make_grid(square = F, cellsize = grid_spacing)  # make the grid
      
    plot(nc, col = "red")
    plot(grid, add = T)
    

    enter image description here