Last active
February 8, 2018 20:13
-
-
Save etiennebr/273313f008654dcf826c to your computer and use it in GitHub Desktop.
rasterize using the extent of an object and no raster
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
library("raster") | |
setMethod("rasterize", signature(x="data.frame", y="missing"), | |
function(x, field, res = 1, fun = 'last', ...) { | |
r = raster(res = res, xmn = min(x$x), xmx = max(x$x), ymn = min(x$y), ymx = max(x$y)) | |
rasterize(x, r, field = field, fun = fun, ...) | |
}) | |
setMethod("rasterize", signature(x="SpatialPoints", y="missing"), | |
function(x, field, res = 1, fun = 'last', ...) { | |
box = sp::bbox(x) | |
r = raster(res = res, | |
xmn = box[1, "min"], | |
xmx = box[1, "max"], | |
ymn = box[2, "min"], | |
ymx = box[2, "max"]) | |
rasterize(x, r, field = field, fun = fun, ...) | |
}) | |
setMethod("rasterize", signature(x="SpatialLines", y="missing"), | |
function(x, field, res = 1, fun = 'last', ...) { | |
box = sp::bbox(x) | |
r = raster(res = res, | |
xmn = box[1, "min"], | |
xmx = box[1, "max"], | |
ymn = box[2, "min"], | |
ymx = box[2, "max"]) | |
rasterize(x, r, field = field, fun = fun, ...) | |
}) | |
setMethod("rasterize", signature(x="SpatialPolygons", y="missing"), | |
function(x, field, res = 1, fun = 'last', ...) { | |
box = bbox(x) | |
r = raster(res = res, | |
xmn = box[1, "min"], | |
xmx = box[1, "max"], | |
ymn = box[2, "min"], | |
ymx = box[2, "max"]) | |
rasterize(x, r, field = field, fun = fun, ...) | |
}) | |
setMethod("rasterize", signature(x="sf", y="missing"), | |
function(x, field, res = 1, fun = 'last', ...) { | |
box = st_bbox(x) | |
r = raster(res = res, | |
xmn = box["xmin"], | |
xmx = box["xmax"], | |
ymn = box["ymin"], | |
ymx = box["ymax"]) | |
rasterize(x, r, field = field, fun = fun, ...) | |
}) | |
setMethod("rasterize", signature(x="sf", y="RasterLayer"), | |
function(x, y, field, res = 1, fun = 'last', ...) { | |
rasterize(as(x, "Spatial"), y, field = field, fun = fun, ...) | |
}) | |
setMethod("rasterize", signature(x="sf", y="RasterStack"), | |
function(x, y, field, res = 1, fun = 'last', ...) { | |
rasterize(as(x, "Spatial"), y, field = field, fun = fun, ...) | |
}) | |
setMethod("rasterize", signature(x="sf", y="RasterBrick"), | |
function(x, y, field, res = 1, fun = 'last', ...) { | |
rasterize(as(x, "Spatial"), y, field = field, fun = fun, ...) | |
}) | |
round_to <- function(x, to = 1, origin = 0, up = TRUE) { | |
if(up) ceiling((x + origin) / to) * to + origin | |
else floor((x - origin) / to) * to + origin | |
} | |
#' Utility function to round to a resolution and shift according to an origin | |
round_to <- function(x, to = 1, origin = 0, up = TRUE) { | |
if(up) ceiling((x + origin) / to) * to + origin | |
else floor((x - origin) / to) * to + origin | |
} | |
#' Rasterize on a rounded grid | |
#' | |
#' Rasterizing on a rounded grid with defined origin facilitates future merging and stacking of data | |
rasterize_round <- function(x, field, res = 1, fun = 'last', round = TRUE, origin = c(0, 0), ...) { | |
origin = rep(origin, 2)[1:2] | |
box = st_bbox(x) | |
r = raster(res = res, | |
xmn = round_to(box["xmin"], res, origin[1], up = FALSE), | |
xmx = round_to(box["xmax"], res, origin[1], up = TRUE), | |
ymn = round_to(box["ymin"], res, origin[2], up = FALSE), | |
ymx = round_to(box["ymax"], res, origin[2], up = TRUE)) | |
rasterize(x, r, field = field, fun = fun, ...) | |
} | |
if (FALSE) { | |
testthat::expect_equal(round_to(101.1, 10, 0), 110) | |
testthat::expect_equal(round_to(101.1, 10, 1), 111) | |
testthat::expect_equal(round_to( 99, 10, 1), 101) | |
testthat::expect_equal(round_to(101.1, 10, 0, up = FALSE), 100) | |
testthat::expect_equal(round_to(101.1, 10, 1, up = FALSE), 101) | |
testthat::expect_equal(round_to(99, 10, 1, up = FALSE), 91) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment