Create grid of equally spaced rectangles or hexagons. The distance between centre points in both x and y dimension is equal to cell_size. The function is effectively a wrapper around st_make_grid with a little bit of preprocessing including generation of grid only inside st_convex_hull.

create_grid_rectangular(
  geometry,
  cell_size,
  side_offset = 0,
  only_inside = FALSE
)

create_grid_hexagonal(
  geometry,
  cell_size,
  side_offset = 0,
  only_inside = FALSE
)

Arguments

geometry

sf data.frame containing geometry which should be cover by the grid.

cell_size

numeric specifying the distance for equally spaced centers of polygons (rectangular or hexagonal).

side_offset

numeric specifying the side offset, distance added to the convex hull of input geometry to generate grid for KDE. Good estimate is usually the same value as band width of KDE.

only_inside

logical specifying if the grid cells should be generated only inside of the geometry. Default value is FALSE.

Value

sf

data.frame.

Functions

  • create_grid_rectangular(): Create rectangular grid

  • create_grid_hexagonal(): Create hexagonal grid

Examples

library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE
nc <- st_read(system.file("shape/nc.shp", package = "sf")) %>% st_transform(32031)
#> Reading layer `nc' from data source 
#>   `/home/runner/work/_temp/Library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
grid <- create_grid_hexagonal(nc, cell_size = 100000)
grid <- create_grid_rectangular(nc, cell_size = 100000, only_inside = TRUE)