hypertidy / vaster

grid logic, without any pesky data
https://hypertidy.github.io/vaster/
Other
7 stars 0 forks source link

fastest grid to polygons (spoiler: it's wk::rct) #6

Open mdsumner opened 2 years ago

mdsumner commented 2 years ago
poly_grid_sf <- function(dimension, extent = NULL, cell = NULL) {
  d <- poly_grid(dimension, extent, cell = cell)

 sfheaders::sf_polygon(d, x= "x", y = "y", linestring_id = "linestring_id", polygon_id = "linestring_id")

}

dm <- c(360, 180)
ex <- c(0, dm[1], 0, dm[2])
system.time({
xc <- vaster::x_corner(dm, ex)
yc <- rev(vaster::y_corner(dm, ex))
rc <- rct(rep(xc[-length(xc)], length(yc) - 1),
          rep(yc[-length(yc)], each = length(xc) - 1),
          rep(xc[-1], length(yc) - 1),
          rep(yc[-1], each = length(xc) - 1))
sf::st_as_sf(rc)
})

system.time(poly_grid(dm, ex))
plot(poly_grid_sf(dm, ex, cell = sample(prod(dm), 1000)))

st <- terra::rast(terra::ext(ex), ncols = dm[1], nrows = dm[2])
system.time(terra::as.polygons(st))