Skip to content

Commit

Permalink
fixes #986)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Jan 27, 2023
1 parent f2f4007 commit 8ff8a2f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
32 changes: 30 additions & 2 deletions R/rasterize.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,23 @@ rasterize_points <- function(x, y, field, values, fun="last", background=NA, upd


setMethod("rasterize", signature(x="matrix", y="SpatRaster"),
function(x, y, values=1, fun, ..., background=NA, update=FALSE, filename="", overwrite=FALSE, wopt=list()) {
function(x, y, values=1, fun, ..., background=NA, update=FALSE, by=NULL, filename="", overwrite=FALSE, wopt=list()) {

if (!is.null(by)) {
by <- rep_len(by, nrow(x))
values <- rep_len(values, nrow(x))

x <- lapply(split(data.frame(x), by), as.matrix)
values <- split(values, by)

out <- rast(lapply(1:length(x), function(i) rasterize(x[[i]], y, values[[i]], fun, background=background, update=update)))
names(out) <- unique(by)
if (filename != "") {
out <- writeRaster(out, filename, overwrite=overwrite, wopt=wopt)
}
return(out)
}

lonlat <- .checkXYnames(colnames(x))

if (NCOL(values) <= 1) {
Expand All @@ -177,7 +193,19 @@ setMethod("rasterize", signature(x="matrix", y="SpatRaster"),


setMethod("rasterize", signature(x="SpatVector", y="SpatRaster"),
function(x, y, field="", fun, ..., background=NA, touches=FALSE, update=FALSE, sum=FALSE, cover=FALSE, filename="", overwrite=FALSE, wopt=list()) {
function(x, y, field="", fun, ..., background=NA, touches=FALSE, update=FALSE, sum=FALSE, cover=FALSE, by=NULL, filename="", overwrite=FALSE, wopt=list()) {


if (!is.null(by)) {
uby <- unlist(unique(x[[by]]))
x <- split(x, by)
out <- rast(lapply(x, function(i) rasterize(i, y, field=field, fun, background=background, touches=touches, update=update, sum=sum, cover=cover)))
names(out) <- uby
if (filename != "") {
out <- writeRaster(out, filename, overwrite=overwrite, wopt=wopt)
}
return(out)
}

values <- 1
if (!is.character(field)) {
Expand Down
6 changes: 4 additions & 2 deletions man/rasterize.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ Transfer values associated with the geometries of vector data to a raster

\usage{
\S4method{rasterize}{SpatVector,SpatRaster}(x, y, field="", fun, ..., background=NA, touches=FALSE,
update=FALSE, sum=FALSE, cover=FALSE, filename="", overwrite=FALSE, wopt=list())
update=FALSE, sum=FALSE, cover=FALSE, by=NULL, filename="", overwrite=FALSE, wopt=list())

\S4method{rasterize}{matrix,SpatRaster}(x, y, values=1, fun, ..., background=NA,
update=FALSE, filename="", overwrite=FALSE, wopt=list())
update=FALSE, by=NULL, filename="", overwrite=FALSE, wopt=list())
}

\arguments{
Expand All @@ -43,6 +43,8 @@ Transfer values associated with the geometries of vector data to a raster

\item{cover}{logical. If \code{TRUE} and the geometry of \code{x} is polygons, the fraction of a cell that is covered by the polygons is returned. This is estimated by determining presence/absence of the polygon in at least 100 sub-cells (more of there are very few cells)}

\item{by}{character or numeric value(s) to split \code{x} into multiple groups. There will be a separate layer for each group returned. If \code{x} is a SpatVector, \code{by} can be a column number or name. If \code{x} is a matrix, \code{by} should be a vector that identifies group membership for each row in \code{x}}

\item{filename}{character. Output filename}
\item{overwrite}{logical. If \code{TRUE}, \code{filename} is overwritten}
\item{wopt}{list with additional arguments for writing files as in \code{\link{writeRaster}}}
Expand Down

0 comments on commit 8ff8a2f

Please sign in to comment.