Skip to content

Instantly share code, notes, and snippets.

@marcosci
Created March 16, 2018 08:59
Show Gist options
  • Save marcosci/f866aae4cc4295ee9904a8ff98cd687b to your computer and use it in GitHub Desktop.
Save marcosci/f866aae4cc4295ee9904a8ff98cd687b to your computer and use it in GitHub Desktop.
velox problem
ncol <- 100
nrow <- 100
germs <- 50
# bounding box placing germs and clipping ----
bounding_box <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0),
c(ncol, 0),
c(ncol, nrow),
c(0, nrow),
c(0, 0)))))
# distribute the germs from which the polygons are build ----
rnd_points <- sf::st_union(sf::st_sample(bounding_box, germs))
# compute the voronoi tessellation ----
voronoi_tess <- sf::st_voronoi(rnd_points)
# clip and give random values ----
voronoi_tess <- sf::st_intersection(sf::st_cast(voronoi_tess), bounding_box)
voronoi_tess <- sf::st_sf(value = stats::runif(germs),
geometry = sf::st_sfc(voronoi_tess))
# (f)rasterize with lightning speed ----
# does not work:
vx <- velox::velox(matrix(0, 100, 100), extent=raster::extent(voronoi_tess), res = 1)
vx$rasterize(voronoi_tess, field = "value")
test <- vx$as.RasterLayer()
raster::plot(test)
# works:
r <- raster::raster(raster::extent(voronoi_tess), res = resolution)
r <- fasterize::fasterize(voronoi_tess, r, field = "value", fun = "sum")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment