I would like to create a SpatialPolygons object (Hexagonal grid) that replace another SpatialPolygons.
my attempt using the sp, sf and rgeos packages does not work, I get the error: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘coordinates’ for signature ‘"sfc_MULTIPOLYGON"’
library(sf)
hti<- world %>%
filter(name_long=="Haiti")
plot(hti$geom )
require(sp)
data(hti)
hti.sr = SpatialPolygons(list(Polygons(list(Polygon(hti$geom)), "x")))
plot(hti.sr)
library(rgeos)
meuse.large = gBuffer(hti$geom, width = 2000)
HexPts <-spsample(hti$geom, type="hexagonal", cellsize=1000)
HexPols <- HexPoints2SpatialPolygons(HexPts)
plot(HexPols[meuse.sr,], add=TRUE)
Much thanks in advance for your help!
Using package sf:
library(sf)
hti<- world %>%
filter(name_long=="Haiti")
g = st_make_grid(hti$geom,cellsize = .1, square = FALSE)
plot(g)
plot(st_geometry(hti$geom), add = TRUE)
# g[hti$geom] selects cells that intersect with hti$geom:
plot(g[hti$geom], col = '#ff000088', add = FALSE)