Skip to contents

Creates regular hexagonal or square grids over a study area. The function automatically handles coordinate-system transformations and ensures proper grid alignment.

Usage

create_grid(
  study_area,
  cell_size,
  type = c("hexagonal", "square"),
  projection_crs = NULL,
  id_column = NULL,
  return_crs = 4326,
  check_size = TRUE,
  max_cells = 1e+06
)

Arguments

study_area

sf object containing polygonal geometries defining the study area. May be in any CRS.

cell_size

Grid cell size (see Details for units).

type

`"hexagonal"` (default) or `"square"`.

projection_crs

CRS used for grid construction. Default `NULL` selects an appropriate UTM zone via [`get_utm_crs()`]. Pass an EPSG code or CRS string to override.

id_column

Optional column name in `study_area` for creating separate grids per unique value. If supplied, returns a named list of grids.

return_crs

CRS for the output grid (default WGS84). The grid is transformed to this CRS after creation.

check_size

Whether to warn when the grid is very large (default TRUE).

max_cells

Maximum number of cells allowed before stopping (default 1,000,000). Set to `NULL` to disable.

Value

sf object containing the grid (or named list of sf objects when `id_column` is given). Each grid cell carries `grid_id` and `grid_index`.

Details

**Cell size units depend on the projection CRS:** - Projected CRS (UTM, etc.): `cell_size` is in **metres**. - Geographic CRS (WGS84): `cell_size` is in **degrees**.

Use a projected CRS for real-world analysis. If `projection_crs` is `NULL` (the default), an appropriate UTM zone is chosen automatically using [`get_utm_crs()`].

Hexagons are pointy-topped; `cell_size` is the flat-to-flat distance (width between opposite edges).

Examples

if (FALSE) { # \dontrun{
library(sf)
study_area <- st_sf(geometry = st_sfc(
  st_polygon(list(matrix(c(-5, 35, 5, 35, 5, 45, -5, 45, -5, 35),
                         ncol = 2, byrow = TRUE))),
  crs = 4326
))
hex_grid <- create_grid(study_area, cell_size = 1000, type = "hexagonal")
} # }