fasterizeDT: Polygon Rasterization Using Numeric, Factor, or Character...

fasterizeDTR Documentation

Polygon Rasterization Using Numeric, Factor, or Character Fields


A front end for fasterize::fasterize(), fixing several of its infelicities.


  field = NULL,
  fun = "last",
  background = NA_real_,
  by = NULL



Either an sf::sf() object with a geometry column of POLYGON and/or MULTIPOLYGON objects or a sp::SpatialPolygonsDataFrame object.


A RasterLayer object to be used as a template for the raster output.


Character. The name of a column in x, providing a value for each of the polygons rasterized. If NULL (the default), all polygons will be given a value of 1.


Character. The name of a function by which to combine overlapping polygons. Currently takes "sum", "first", "last", "min", "max", "count", or "any". For more details, see ?fasterize::fasterize.


Value to put in the cells that are not covered by any of the features of x. Default is NA.


Character string giving the name of a column in x by which to aggregate layers. If set, fasterizeDT will return a RasterBrick with as many layers as unique values of the by column.


Unlike other functions in this package, fasterizeDT() does not use data.table to speed up its computations. Instead, it is a wrapper for fasterize::fasterize(), intended to address several of that function's limitations.

Most importantly, fasterizeDT() takes care to properly handle rasterization operations in which either the template RasterLayer or the selected polygon feature field is a factor. Specifically, it always returns a raster whose type (numeric or factor) and levels (if a factor) match that of the spatial polygon attribute indicated by its field argument. Second, when field specifies an attribute of class "character", fasterizeDT() automatically converts it to a factor and returns a factor raster. In this, it is unlike both fasterize::fasterize() and raster::rasterize(). Finally, unlike fasterize::fasterize(), fasterizeDT() accepts as inputs either sf::sf() objects or sp::SpatialPolygonsDataFrame objects.


A raster of the same size, extent, resolution and projection as the supplied raster template. Unlike fasterize::fasterize(), fasterizeDT returns a raster of the same type as the data in the column of x selected by the field argument.


Joshua O'Brien


## Load example polygons and prepare a template raster
if (require(raster)) {
SPDF <- shapefile(system.file("external/lux.shp", package = "raster"))
llratio <- 1/cos(pi * mean(coordinates(SPDF)[, 2])/180)
rr <- raster(extent(SPDF),
             resolution = c(llratio * 0.01, 0.01),
             crs = proj4string(SPDF))

## An integer-valued field produces a numeric raster
rInt <- fasterizeDT(SPDF, rr, field = "ID_2")
plot(rInt, col = colorRampPalette(blues9)(12))

## A character-valued field returns a factor raster
rFac <- fasterizeDT(SPDF, rr, field = "NAME_2")
if (require(rasterVis)) {

rasterDT documentation built on Dec. 28, 2022, 1:57 a.m.