fasterize {fasterize} | R Documentation |
Rasterize set of polygons
Fast sf-to-raster conversion
fasterize(sf, raster, field = NULL, fun = "last", background = NA_real_, by = NULL)
sf |
an |
raster |
A raster object. Used as a template for the raster output.
Can be created with |
field |
character. The name of a column in |
fun |
character. The name of a function by which to combine overlapping
polygons. Currently takes "sum", "first", "last", "min", "max", "count", or
"any". Future versions may include more functions or the ability to pass
custom R/C++ functions. If you need to summarize by a different function,
use |
background |
numeric. Value to put in the cells that are not covered by any of the features of x. Default is NA. |
by |
character. The name of a column in |
This is a high-performance replacement for raster::rasterize()
.
The algorithm is based on the method described in course materials provided by Wayne O. Cochran. The algorithm is originally attributed to Wylie et al. (1967).
A raster of the same size, extent, resolution and projection as the provided raster template.
Wylie, C., Romney, G., Evans, D., & Erdahl, A. (1967). Half-tone perspective drawings by computer. Proceedings of the November 14-16, 1967, Fall Joint Computer Conference. AFIPS '67 (Fall). https://dx.doi.org/10.1145/1465611.1465619
library(sf) library(fasterize) p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20)) hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20)) p1 <- list(p1, hole) p2 <- list(rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))) p3 <- list(rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))) pols <- st_sf(value = rep(1,3), geometry = st_sfc(lapply(list(p1, p2, p3), st_polygon))) r <- raster(pols, res = 1) r <- fasterize(pols, r, field = "value", fun="sum") plot(r)