-
Notifications
You must be signed in to change notification settings - Fork 29
/
hexagons.R
46 lines (46 loc) · 1.63 KB
/
hexagons.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#' @title Hexagons
#' @description Create hexagon polygons
#'
#' @param x sf class object indicating extent
#' @param res Area of resulting hexagons
#'
#' @details
#' Based on extent of x, creates a hexagon mesh with size of hexagons defined by res argumnet
#'
#' @return sf POLYGONS object
#'
#' @examples
#' library(sf)
#' if(require(sp, quietly = TRUE)) {
#' data(meuse, package = "sp")
#' meuse <- st_as_sf(meuse, coords = c("x", "y"), crs = 28992,
#' agr = "constant")
#'
#' hex <- hexagons(meuse, res=300)
#' plot(st_geometry(hex))
#' plot(st_geometry(meuse),pch=20,add=TRUE)
#'
#' # subset hexagons to intersection with points
#' idx <- which(apply(st_intersects(hex, meuse, sparse=FALSE), 1, any))
#' hex.sub <- hex[idx,]
#' plot(st_geometry(hex.sub))
#' plot(st_geometry(meuse),pch=20,add=TRUE)
#'
#' } else {
#' cat("Please install sp package to run example", "\n")
#' }
#'
#' @export
hexagons <- function(x, res = 100) {
if(!inherits(x, "sf"))
stop(deparse(substitute(x)), " must be an sf object")
if(sf::st_is_longlat(x))
stop("Data appears to be in Latitude/Longitude
and needs to be projected")
e <- sf::st_as_sfc(sf::st_bbox(x))
res <- sf::st_as_sf(sf::st_make_grid(e, cellsize = res,
square = FALSE))
sf::st_geometry(res) <- "geometry"
sf::st_crs(res) <- sf::st_crs(x)
return(res)
}