Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Nov 3, 2023
1 parent ed7a23b commit 2ed1ad5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# fasterize 1.0.5

* Input geometries may now be of type "sfc_GEOMETRY" or other mixed types. Any non polygon
geometry is dropped with a message.

* 'field' may now be a numeric, integer, or factor vector or (as before) a name of a column in
the input data frame. This means we can `fasterize(vec, r, field = seq_along(vec))` or
`fasterize(df, r, field = seq_len(dim(df)[1L]))` or with any values we like.

* Now supporting any geometry or dataframe input supported by wk (wkb, wkt, geos, as well as sf).

* Namespaced documentation fixes thanks to CRAN.
Expand Down
25 changes: 20 additions & 5 deletions R/fasterize.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ make_sf <- function(x, attr = NULL) {
#' Can be created with [raster::raster()].
#' The fasterize package provides a method to create a raster object from
#' an polygon dataset.
#' @param field character. The name of a column in `sf`,
#' @param field character (or numeric vector). The name of a column in `sf`,
#' providing a value for each of the polygons rasterized. If NULL (default),
#' all polygons will be given a value of 1.
#' all polygons will be given a value of 1. If a numeric vector this value
#' will be used as the value given to the pixel. (No recyling is done).
#' @param fun character. The name of a function by which to combine overlapping
#' polygons. Currently takes "sum", "first", "last", "min", "max", "count", or
#' "any". Future versions may include more functions or the ability to pass
Expand Down Expand Up @@ -61,18 +62,32 @@ make_sf <- function(x, attr = NULL) {
#' @export
fasterize <- function(sf, raster, field = NULL, fun = "last", background = NA_real_, by = NULL) {

## check the types up here
types <- wk::wk_meta(sf)$geometry_type
bad <- ! types %in% c(3, 6)

## ok so we get geometry from anything wk can handle
geom <- wk::wk_handle(sf, wk::sfc_writer())

if (any(bad)) {
if (all(bad)) stop("no polygon geometries to fasterize")
geom <- geom[!bad, ]
message(sprintf("removing %i geometries that are not polygon or multipolygon equivalent", sum(bad)))
}


sf1 <- make_sf(geom)
if (inherits(sf, "data.frame") && !is.null(field)) {
sf1[[field]] <- sf[[field]]
if ( !is.null(field)) {
if (length(field) == 1L) {
sf1[[field]] <- sf[[field]]
} else if (length(field) == dim(sf1)[1L]) {
sf1[["field"]] <- field
field <- "field"
}
}
if (inherits(sf, "data.frame") && !is.null(by)) {
sf1[[by]] <- sf[[by]]
}

fasterize_cpp(sf1, raster, field, fun, background, by)
}

5 changes: 3 additions & 2 deletions man/fasterize.Rd

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

2 changes: 1 addition & 1 deletion src/check_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void check_inputs(Rcpp::DataFrame &sf,
polygons = sf[Rcpp::as<std::string>(sf.attr("sf_column"))];

if(!(Rf_inherits(polygons, "sfc_MULTIPOLYGON") |
Rf_inherits(polygons, "sfc_POLYGON"))) {
Rf_inherits(polygons, "sfc_POLYGON") | Rf_inherits(polygons, "sfc_GEOMETRY"))) {
err_msg << "sf geometry must be POLYGON or MULTIPOLYGON" << std::endl;
}

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-01-inputcheck.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ test_that("fasterize needs polygons", {
geometry = st_sfc(lapply(list(p1, p2, p3),
function(x) st_linestring(x[[1]]))))
expect_error(fasterize(lines, r1),
"sf geometry must be POLYGON or MULTIPOLYGON")
"no polygon geometries to fasterize")

lines_wkb <- data.frame(value = c(1,2,3),
geometry = wk::as_wkb(sf::st_cast(pols$geometry, "MULTILINESTRING")))

expect_error(fasterize(lines_wkb, r1),
"geometry must be POLYGON or MULTIPOLYGON")
"no polygon geometries to fasterize")



})

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-05-by.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ test_that("'by' argument works", {

test_that("'by' layers are equivalent to layers generated separately", {
rba <- fasterize(pols, r1, field="value", fun="sum", by ="value")
for(i in nrow(pols))
for(i in 1:nrow(pols)) {
expect_equal(as.raster(rba[[i]]),
as.raster(fasterize(pols[i,], r1, field="value", fun="sum")))
}
})

test_that("'by' can handle non-character fields", {
Expand Down

0 comments on commit 2ed1ad5

Please sign in to comment.