-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from mdsumner/wk-handler
Wk handler
- Loading branch information
Showing
16 changed files
with
259 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: fasterize | ||
Title: Fast Polygon to Raster Conversion | ||
Version: 1.0.4.9002 | ||
Version: 1.0.5 | ||
Authors@R: c( | ||
person("Noam", "Ross", , "[email protected]", | ||
role = c("aut"), | ||
|
@@ -12,28 +12,29 @@ Authors@R: c( | |
person("EcoHealth Alliance", role="cph"), | ||
person("USAID PREDICT", role = "fnd")) | ||
Description: Provides a drop-in replacement for rasterize() from the 'raster' | ||
package that takes 'sf'-type objects, and is much faster. There is support | ||
for the main options provided by the rasterize() function, including | ||
setting the field used and background value, and options for | ||
package that takes polygon vector or dataframe objects, and is much faster. | ||
There is support for the main options provided by the rasterize() function, | ||
including setting the field used and background value, and options for | ||
aggregating multi-layer rasters. Uses the scan line algorithm attributed to | ||
Wylie et al. (1967) <doi:10.1145/1465611.1465619>. | ||
License: MIT + file LICENSE | ||
URL: https://github.com/ecohealthalliance/fasterize | ||
BugReports: https://github.com/ecohealthalliance/fasterize/issues | ||
RoxygenNote: 7.2.1 | ||
SystemRequirements: C++11 | ||
RoxygenNote: 7.2.3 | ||
Suggests: | ||
testthat, | ||
microbenchmark, | ||
knitr, | ||
rmarkdown, | ||
sf, | ||
spelling | ||
spelling, | ||
geos | ||
Depends: | ||
R (>= 3.3.0) | ||
Imports: | ||
Rcpp, | ||
raster (>= 2.8-3) | ||
raster (>= 2.8-3), | ||
wk | ||
LinkingTo: | ||
Rcpp, | ||
RcppArmadillo | ||
|
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 |
---|---|---|
@@ -1,58 +1,7 @@ | ||
# Generated by using Rcpp::compileAttributes() -> do not edit by hand | ||
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 | ||
|
||
#' Rasterize an sf object of polygons | ||
#' | ||
#' Rasterize set of polygons | ||
#' | ||
#' This is a high-performance replacement for [raster::rasterize()]. | ||
#' | ||
#' The algorithm is based on the method described in course materials provided | ||
#' by [Wayne O. Cochran](https://labs.wsu.edu/wayne-cochran/). The algorithm | ||
#' is originally attributed to | ||
#' Wylie et al. (1967) \doi{10.1145/1465611.1465619}. | ||
#' | ||
#' @param sf an [sf::sf()] object with a geometry column of POLYGON and/or | ||
#' MULTIPOLYGON objects. | ||
#' @param raster A raster object. Used as a template for the raster output. | ||
#' Can be created with [raster::raster()]. | ||
#' The fasterize package provides a method to create a raster object from | ||
#' an sf object. | ||
#' @param field character. 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. | ||
#' @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 | ||
#' custom R/C++ functions. If you need to summarize by a different function, | ||
#' use `by=` to get a RasterBrick and then [raster::stackApply()] or | ||
#' [raster::calc()] to summarize. | ||
#' @param background numeric. Value to put in the cells that are not covered by | ||
#' any of the features of x. Default is NA. | ||
#' @param by character. The name of a column in `sf` by which to aggregate | ||
#' layers. If set, fasterize will return a RasterBrick with as many layers | ||
#' as unique values of the `by` column. | ||
#' @return A raster of the same size, extent, resolution and projection as the | ||
#' provided raster template. | ||
#' @references Wylie, C., Romney, G., Evans, D., & Erdahl, A. (1967). | ||
#' Half-tone perspective drawings by computer. Proceedings of the November | ||
#' 14-16, 1967, Fall Joint Computer Conference. AFIPS '67 (Fall). | ||
#' \doi{10.1145/1465611.1465619} | ||
#' @examples | ||
#' library(sf) | ||
#' library(fasterize) | ||
#' p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20)) | ||
#' hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20)) | ||
#' p1 <- list(p1, hole) | ||
#' p2 <- list(rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))) | ||
#' p3 <- list(rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))) | ||
#' pols <- st_sf(value = rep(1,3), | ||
#' geometry = st_sfc(lapply(list(p1, p2, p3), st_polygon))) | ||
#' r <- raster(pols, res = 1) | ||
#' r <- fasterize(pols, r, field = "value", fun="sum") | ||
#' plot(r) | ||
#' @export | ||
fasterize <- function(sf, raster, field = NULL, fun = "last", background = NA_real_, by = NULL) { | ||
.Call('_fasterize_fasterize', PACKAGE = 'fasterize', sf, raster, field, fun, background, by) | ||
fasterize_cpp <- function(sf, raster, field = NULL, fun = "last", background = NA_real_, by = NULL) { | ||
.Call('_fasterize_fasterize_cpp', PACKAGE = 'fasterize', sf, raster, field, fun, background, by) | ||
} | ||
|
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,93 @@ | ||
make_sf <- function(x, attr = NULL) { | ||
structure(list(geom = x), names = c("geom"), row.names = seq_len(length(x)), | ||
sf_column = "geom", class = c("sf", "data.frame" )) | ||
} | ||
|
||
|
||
#' Rasterize a vector or dataframe object of polygons | ||
#' | ||
#' Rasterize set of polygons | ||
#' | ||
#' This is a high-performance replacement for [raster::rasterize()]. | ||
#' | ||
#' The algorithm is based on the method described in course materials provided | ||
#' by [Wayne O. Cochran](https://labs.wsu.edu/wayne-cochran/). The algorithm | ||
#' is originally attributed to | ||
#' Wylie et al. (1967) \doi{10.1145/1465611.1465619}. | ||
#' | ||
#' Note that original implementation worked only for sf dataframes of class "sf", but this | ||
#' now works for any polygon vector (sfc, wkt, wkb, geos) or dataframe with a polygon vector | ||
#' supported by the wk package handlers. | ||
#' | ||
#' @param sf a polygon vector or data frame object with a geometry column of POLYGON and/or | ||
#' MULTIPOLYGON (equivalent) objects. | ||
#' @param raster A raster object. Used as a template for the raster output. | ||
#' Can be created with [raster::raster()]. | ||
#' The fasterize package provides a method to create a raster object from | ||
#' an polygon dataset. | ||
#' @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. 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 | ||
#' custom R/C++ functions. If you need to summarize by a different function, | ||
#' use `by=` to get a RasterBrick and then [raster::stackApply()] or | ||
#' [raster::calc()] to summarize. | ||
#' @param background numeric. Value to put in the cells that are not covered by | ||
#' any of the features of x. Default is NA. | ||
#' @param by character. The name of a column in `sf` by which to aggregate | ||
#' layers. If set, fasterize will return a RasterBrick with as many layers | ||
#' as unique values of the `by` column. | ||
#' @return A raster of the same size, extent, resolution and projection as the | ||
#' provided raster template. | ||
#' @references Wylie, C., Romney, G., Evans, D., & Erdahl, A. (1967). | ||
#' Half-tone perspective drawings by computer. Proceedings of the November | ||
#' 14-16, 1967, Fall Joint Computer Conference. AFIPS '67 (Fall). | ||
#' \doi{10.1145/1465611.1465619} | ||
#' @examples | ||
#' library(sf) | ||
#' library(fasterize) | ||
#' p1 <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20)) | ||
#' hole <- rbind(c(-150,-20), c(-100,-10), c(-110,20), c(-150,-20)) | ||
#' p1 <- list(p1, hole) | ||
#' p2 <- list(rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))) | ||
#' p3 <- list(rbind(c(-125,0), c(0,60), c(40,5), c(15,-45), c(-125,0))) | ||
#' pols <- st_sf(value = rep(1,3), | ||
#' geometry = st_sfc(lapply(list(p1, p2, p3), st_polygon))) | ||
#' r <- raster(pols, res = 1) | ||
#' r <- fasterize(pols, r, field = "value", fun="sum") | ||
#' plot(r) | ||
#' @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 ( !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) | ||
} | ||
|
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 |
---|---|---|
@@ -1,12 +1,8 @@ | ||
## R CMD check results | ||
|
||
0 errors | 0 warnings | 0 notes | ||
# fasterize 1.0.5 | ||
|
||
* Submitted by new maintainer, Michael Sumner ([email protected]), change from Noam Ross ([email protected]) as advised by email. | ||
|
||
* Fixes CRAN warnings on bitwise operator and unused LazyData. | ||
|
||
* Fixes implicit type warnings found with clang. | ||
* Fixes for roxygen namespace documentation and remove specific C++11 requirement. | ||
|
||
|
||
|
||
Thanks! |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.