Skip to content

Commit

Permalink
support terra rasters
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerke committed Jul 14, 2024
1 parent c42015d commit 37c97b2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Imports:
geojsonsf,
sf,
rlang,
htmltools
htmltools,
grDevices,
terra
Suggests:
shiny,
mapboxapi,
Expand Down
50 changes: 48 additions & 2 deletions R/sources.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,57 @@ add_raster_dem_source <- function(map, id, url, tileSize = 512, maxzoom = NULL)
#' @param map A map object created by the `mapboxgl` or `maplibre` function.
#' @param id A unique ID for the source.
#' @param url A URL pointing to the image source.
#' @param coordinates A list of coordinates specifying the image corners in clockwise order: top left, top right, bottom right, bottom left.
#' @param data A `SpatRaster` object from the `terra` package or a `RasterLayer` object.
#' @param coordinates A list of coordinates specifying the image corners in clockwise order: top left, top right, bottom right, bottom left. For `SpatRaster` or `RasterLayer` objects, this will be extracted for you.
#' @param colors A vector of colors to use for the raster image.
#'
#' @return The modified map object with the new source added.
#' @export
add_image_source <- function(map, id, url, coordinates) {
add_image_source <- function(map, id, url = NULL, data = NULL, coordinates = NULL, colors = NULL) {

if (!is.null(data)) {
if (inherits(data, "RasterLayer")) {
data <- terra::rast(data)
}

# Prepare color mapping
if (is.null(colors)) {
colors <- colorRampPalette(c("#440154", "#3B528B", "#21908C", "#5DC863", "#FDE725"))(256)
} else if (length(colors) < 256) {
colors <- colorRampPalette(colors)(256)
}

data <- data / max(values(data), na.rm = TRUE) * 255
data <- round(data)
coltb <- data.frame(value = 0:255, col = colors)


# Create color table
coltab(data) <- coltb

# Handle NA values
data[is.na(data[])] <- 255

png_path <- tempfile(fileext = ".png")
terra::writeRaster(data, png_path, overwrite = TRUE, NAflag = 255, datatype = "INT1U")
url <- base64enc::dataURI(file = png_path, mime = "image/png")

# Compute coordinates if not provided
if (is.null(coordinates)) {
ext <- ext(data)
coordinates <- list(
unname(c(ext[1], ext[4])), # top-left
unname(c(ext[2], ext[4])), # top-right
unname(c(ext[2], ext[3])), # bottom-right
unname(c(ext[1], ext[3])) # bottom-left
)
}
}

if (is.null(url)) {
stop("Either 'url' or 'data' must be provided.")
}

source <- list(
id = id,
type = "image",
Expand Down
15 changes: 13 additions & 2 deletions man/add_image_source.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 37c97b2

Please sign in to comment.