Skip to content

Commit

Permalink
Add as_SpatRaster() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardlavender committed Oct 2, 2024
1 parent 86ce0e1 commit 4e88799
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/spatial.R
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)

}

0 comments on commit 4e88799

Please sign in to comment.