calculate_grid: Calculate grid from spatial polygons.

View source: R/calculate_grid.R

calculate_gridR Documentation

Calculate grid from spatial polygons.

Description

Given an input multipolgyon spatial data frame this function calculates a hexagonal or regular grid that strives to preserve the original geography.

Usage

calculate_grid(
  shape,
  learning_rate = 0.03,
  grid_type = c("hexagonal", "regular"),
  seed = NULL,
  verbose = FALSE
)

## S3 method for class 'SpatialPolygonsDataFrame'
calculate_grid(
  shape,
  learning_rate = 0.03,
  grid_type = c("hexagonal", "regular"),
  seed = NULL,
  verbose = FALSE
)

## S3 method for class 'sf'
calculate_grid(
  shape,
  learning_rate = 0.03,
  grid_type = c("hexagonal", "regular"),
  seed = NULL,
  verbose = FALSE
)

Arguments

shape

A 'SpatialPolygonsDataFrame' or an sf object representing the original spatial polygons.

learning_rate

The rate at which the gradient descent finds the optimum cellsize to ensure that your gridded points fit within the outer boundary of the input polygons.

grid_type

Either 'hexagonal' for a hexagonal grid (default) or 'regular' for a regular grid.

seed

An optional random seed integer to be used for the grid calculation algorithm.

verbose

A logical indicating whether messages should be printed as the algorithm iterates.

Examples

library(sf)
input_file <- system.file('extdata', 'london_LA.json', package = 'geogrid')
original_shapes <- st_read(input_file) %>% st_set_crs(27700)

# calculate grid
new_cells <- calculate_grid(shape = original_shapes,
  grid_type = 'hexagonal', seed = 1)
grid_shapes <- assign_polygons(original_shapes, new_cells)
plot(grid_shapes)

par(mfrow = c(1, 2))
plot(st_geometry(original_shapes))
plot(st_geometry(grid_shapes))

## Not run: 
# look at different grids using different seeds
par(mfrow=c(2, 3), mar = c(0, 0, 2, 0))
for (i in 1:6) {
  new_cells <- calculate_grid(shape = original_shapes,
    grid_type = 'hexagonal', seed = i)
  plot(new_cells, main = paste('Seed', i, sep=' '))
}

## End(Not run)

geogrid documentation built on Aug. 19, 2023, 9:07 a.m.