An 'all-touched-capable' rasterizer that depends only on sf and raster.
Arguments
- x
An
sfobject (polygon, lines, or points).- res
Resolution (raster cell size) in units of
x'scrs. May be either a 2-element vector (for lon and lat, respectively) or a single value (same value used for each dimension).- value
The value assigned to cells matching
x.- bg_value
The value assigned to cells not matching
x.- all_touched
If
TRUE(default), raster will returnvaluefor every cell touched by polygon (andbg_valuefor all others); otherwise, raster will returnvaluefor all cells whose center points are within the polygon.- silent
If false (default), progress messages are not displayed.
Examples
# Example 1. lon lat WGS input
poly1 <- great_lakes_polygon
plot(sf::st_geometry(poly1))
rast1 <- jarasterize(poly1, res = c(0.1, 0.05))
#> Rasterizing...
#> Done (2.1 secs)
if (FALSE) { # \dontrun{
# compare to polygon
x11(width = 12, height = 8)
raster::plot(rast1)
plot(sf::st_geometry(poly1), add = TRUE)
} # }
# Example 2. projected input; 5 km cell size
poly2 <- sf::st_transform(poly1, crs = 3175)
rast2 <- jarasterize(poly2, res = 5000)
#> Rasterizing...
#> Done (2.2 secs)
if (FALSE) { # \dontrun{
# compare to polygon
x11(width = 12, height = 8)
raster::plot(rast2)
plot(sf::st_geometry(poly2), add = TRUE)
} # }
# Example 3. projected input; 5 km cell size; all_touched = FALSE
rast3 <- jarasterize(poly2, res = 5000, all_touched = FALSE)
#> Rasterizing...
#> Done (0.2 secs)
if (FALSE) { # \dontrun{
# compare to polygon
x11(width = 12, height = 8)
raster::plot(rast3)
plot(sf::st_geometry(poly2), add = TRUE)
} # }
