Skip to content

Commit

Permalink
Added fasterizeDT() function, a wrapper for fasterize::fasterize()
Browse files Browse the repository at this point in the history
As described in an Issue filed at the **fasterize** package's GitHub
repository (ecohealthalliance/fasterize#26),
`fasterize` returns rasters that match the class of their template
raster, not of the field that the user selected to be burned into
them. In addition, when passed a character or a factor field, it does
not -- as would often be desireable -- return a factor-valued raster.

`fasterizeDT()` addresses both of these issues, consistently returning
a raster whose class (and, if a factor, levels) matches that of the
user-selected field in the input polygon object.

In addition, `fasterizeDT()` accepts `sp::SpatialPolygonsDataFrame()`
objects, converting them to `sf::sf()` objects before passing them on
to `fasterize::fasterize()`.
  • Loading branch information
JoshOBrien committed Jun 17, 2019
1 parent 606bf9b commit a2c5132
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

export(cat_to_val)
export(crosstabDT)
export(fasterizeDT)
export(freqDT)
export(subsDT)
export(zonalDT)
exportMethods(freqDT)
import(data.table, except = shift)
import(methods)
import(raster)
importFrom(fasterize,fasterize)
importFrom(sf,st_as_sf)
importFrom(stats,complete.cases)
69 changes: 69 additions & 0 deletions R/fasterizeDT.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

##' A front end for
##' \code{\link[fasterize:fasterize]{fasterize::fasterize()}}, fixing
##' several of its infelicities.
##'
##' @title Fixed up fasterize Function
##' @param x An \code{sf::sf()} object with a geometry column of
##' \code{POLYGON} and/or \code{MULTIPOLYGON} objects or a
##' \code{sp::SpatialPolygonsDataFrame} object.
##' @param raster A raster object. Used as a template for the raster
##' output
##' @param field character. The name of a column in \code{x},
##' providing a value for each of the polygons rasterized. If NULL
##' (default), all polygons will be given a value of 1.
##' @param fun \code{?fasterize::fasterize}
##' @param background Value to put in the cells that are not covered
##' by any of the features of x. Default is NA.
##' @param by \code{?fasterize::fasterize}
##' @return A raster of the same size, extent, resolution and
##' projection as the supplied raster template. Unlike
##' \code{\link[fasterize:fasterize]{fasterize::fasterize()}},
##' \code{fasterizeDT} returns a raster of the same type as the
##' data in the column of \code{x} column selected by the
##' \code{field} argument.
##' @importFrom fasterize fasterize
##' @importFrom sf st_as_sf
##' @export
##' @author Joshua O'Brien
##' @examples
##' \dontrun{
##' }
fasterizeDT <- function (x,
raster,
field = NULL,
fun = "last",
background = NA_real_,
by = NULL) {
if (class(x) == "SpatialPolygonsDataFrame") {
x <- st_as_sf(x)
}
if (is.null(field)) {
field <- names(x)[1]
}
field_class <- class(x[[field]])
## Convert character field to factor
if (field_class == "character") {
val <- x[[field]]
val <- factor(val, levels = sort(unique(val)))
x[[field]] <- val
}
## Rasterize polygons
out_raster <-
fasterize(sf = x, raster = raster, field = field,
fun = fun, background = background, by = by)
## Ensure that any RAT attached to output raster is derived from
## input char or factor field
if(field_class %in% c("character", "factor")) {
lev <- levels(x[[field]])
RAT <- data.frame(ID = seq_along(lev), VALUE = lev)
## Silence warning emitted when overwriting a RAT from input
## raster
suppressWarnings(levels(out_raster) <- RAT)
} else {
out_raster@data@isfactor <- FALSE
out_raster@data@attributes <- list()
}
out_raster
}

48 changes: 48 additions & 0 deletions man/fasterizeDT.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a2c5132

Please sign in to comment.