-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
86ce0e1
commit 4e88799
Showing
1 changed file
with
49 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# (TEMPORARY) as_SpatRaster() function | ||
|
||
# TO DO | ||
# * Rename to assemble_maps() | ||
# * If SpatVector supplied, implement as shown | ||
# * If SpatRaster supplied, create SpatVector (?) | ||
# * Add to patter | ||
|
||
as_SpatRaster <- function(.x, .simplify = NULL, .utm = NULL, | ||
.res, .field, .touches = TRUE, | ||
.plot = FALSE, ...) { | ||
|
||
# Translate sf objects to SpatVectors | ||
if (inherits(.x, "sf")) { | ||
.x <- terra::vect(.x) | ||
} | ||
|
||
# (optional) Simplify SpatVector | ||
# > This improves speed | ||
if (!is.null(.simplify)) { | ||
.x <- terra::simplifyGeom(.x, tolerance = .simplify) | ||
} | ||
|
||
# Translate to UTM | ||
if (!is.null(.utm)) { | ||
.x <- terra::project(.x, .utm) | ||
} | ||
|
||
# Define map_value | ||
.x$map_value <- .x[[.field]] | ||
|
||
# Define blank raster for rasterization | ||
r <- terra::rast(terra::ext(.x), | ||
crs = terra::crs(.x), | ||
res = .res) | ||
|
||
# Rasterise the SpatVector | ||
map <- terra::rasterize(.x, r, field = "map_value", touches = .touches, ...) | ||
|
||
# (optional) Plot | ||
if (.plot) { | ||
terra::plot(map) | ||
terra::lines(.x) | ||
} | ||
|
||
# Return list | ||
list(SpatVector = .x, SpatRaster = map) | ||
|
||
} |