
Interactive leaflet map for classified rasters
Source:R/dft_map_interactive.R
dft_map_interactive.RdBuild a toggleable leaflet map from classified SpatRasters or remote COG
URLs served via titiler. Includes layer control, legend, and fullscreen.
Arguments
- x
A named list of classified terra::SpatRasters (e.g. from
dft_rast_classify()) or a named character vector of COG URLs. A singleSpatRasteror URL string is auto-wrapped into a length-1 list/vector. Names become the layer toggle labels (years, seasons, etc.).- aoi
An
sfpolygon for the area of interest outline.NULL(default) omits the AOI layer.- class_table
A tibble with columns
code,class_name,color(hex). WhenNULL, loaded viadft_class_table()usingsource.- source
Character. Used to load a shipped class table when
class_tableisNULL. One of"io-lulc"or"esa-worldcover".- titiler_url
Base URL of a titiler instance (e.g.
"https://titiler.example.com"). Only used whenxcontains COG URLs. Defaults togetOption("drift.titiler_url"). IfNULLin COG mode, an error is raised prompting the user to set the option.- basemaps
Named character vector of provider tile IDs. The first element is the default basemap. Names become radio button labels.
- legend_position
Legend placement passed to
leaflet::addLegend(). Set toNULLto suppress the legend.- zoom
Initial zoom level.
Value
A leaflet::leaflet htmlwidget. The first layer in x is visible
by default; other layers are hidden but toggleable.
Examples
# Single classified raster — returns a leaflet widget
r <- terra::rast(system.file("extdata", "example_2020.tif", package = "drift"))
classified <- dft_rast_classify(r, source = "io-lulc")
map <- dft_map_interactive(classified)
class(map)
#> [1] "leaflet" "htmlwidget"
# Multiple years with AOI — toggle between time periods
aoi <- sf::st_read(
system.file("extdata", "example_aoi.gpkg", package = "drift"),
quiet = TRUE
)
files <- c("2017" = "example_2017.tif", "2020" = "example_2020.tif",
"2023" = "example_2023.tif")
rasters <- lapply(files, function(f) {
terra::rast(system.file("extdata", f, package = "drift"))
})
classified <- dft_rast_classify(rasters, source = "io-lulc")
map <- dft_map_interactive(classified, aoi = aoi)
if (interactive()) map
if (FALSE) { # \dontrun{
# Remote COGs via titiler (requires options(drift.titiler_url = "..."))
cogs <- c("2017" = "https://bucket.s3.amazonaws.com/lulc_2017.tif",
"2023" = "https://bucket.s3.amazonaws.com/lulc_2023.tif")
dft_map_interactive(cogs, source = "io-lulc")
} # }