-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added fasterizeDT() function, a wrapper for fasterize::fasterize()
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
1 parent
606bf9b
commit a2c5132
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.