Skip to content

Commit

Permalink
address #705
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Aug 26, 2024
1 parent 123db33 commit 28f7937
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions R/rasterize.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ st_rasterize = function(sf, template = guess_raster(sf, ...) %||%
} else
sf[isn]
n_attr = length(which(isn))
if (length(dim(template)) == 2 && n_attr > 1)
template = merge(do.call(c, lapply(seq_len(n_attr), function(x) template)))
geoms = which(sapply(sf, inherits, "sfc"))
attrs = as.data.frame(sf)[-geoms]
if (length(dim(template)) == 2 && n_attr > 1)
template = st_set_dimensions(merge(do.call(c, lapply(seq_len(n_attr), function(x) template))),
3, names(attrs))
for (i in which(sapply(sf, inherits, "factor"))) # factors:
sf[[i]] = as.numeric(sf[[i]])
sf::gdal_rasterize(sf, template, st_geotransform(template), file, driver, options)
ret = read_stars(file, driver = driver, proxy = proxy)
if (!proxy) {
if (! proxy) {
if (length(dim(ret)) > 2)
ret = split(ret)
for (i in seq_along(ret)) {
Expand All @@ -96,7 +97,7 @@ st_rasterize = function(sf, template = guess_raster(sf, ...) %||%
ret[[i]] = structure(ret[[i]], class = class(attrs[[i]]), levels = levels(attrs[[i]]))
}
}
setNames(ret, names(attrs))
ret
}

guess_raster = function(x, ...) {
Expand Down

0 comments on commit 28f7937

Please sign in to comment.