
Interactive leaflet map for classified rasters and transitions
Source:R/dft_map_interactive.R
dft_map_interactive.RdBuild a toggleable leaflet map from classified SpatRasters or remote COG
URLs served via titiler. Optionally overlay land cover transitions from
dft_rast_transition(). Includes layer control, legend, and fullscreen.
Usage
dft_map_interactive(
x = NULL,
aoi = NULL,
transition = NULL,
rgb = NULL,
rgb_rescale = NULL,
class_table = NULL,
source = "io-lulc",
titiler_url = getOption("drift.titiler_url"),
basemaps = c(Light = "CartoDB.Positron", `Esri Satellite` = "Esri.WorldImagery",
`Google Satellite` = "https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
OpenTopoMap = "OpenTopoMap"),
legend_position = "bottomright",
zoom = 14
)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.). May beNULLwhenrgbis supplied, for an imagery-only map.- aoi
An
sfpolygon for the area of interest outline.NULL(default) omits the AOI layer.- transition
Output of
dft_rast_transition()— a list with elementsraster(factorSpatRaster) andsummary(tibble). Each transition type becomes a toggleable overlay. Stable transitions (same from/to class) are excluded by default.NULL(default) omits transition overlays.- rgb
Dated reference imagery: a named list of three-band terra::SpatRasters in red, green, blue order (the output of
dft_stac_composite()with its defaultbands), or a named character vector of three-band COG URLs served throughtitiler_url. Names become the layer labels, e.g."2017 Jun–Jul". Local rasters are drawn withleafem::addRasterRGB(), which reads every cell into memory; for a floodplain-sized composite, publish the cached COG and pass its URL instead.NULL(default) adds no imagery.- rgb_rescale
Numeric length-2 reflectance range stretched to the full display range, the same for every band of every
rgblayer. WhenNULL, local rasters stretch each band from its 2nd to 98th percentile across all composites pooled, and COG URLs usec(0, 0.3), a range that suits vegetated land in surface reflectance.- 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 or tile URL templates (starting with
http). 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. Transition overlays
are visible by default when supplied. rgb layers are hidden when x is
supplied (toggle them on beneath it); without x the first is shown.
Details
When only x is supplied, classified layers appear as radio-toggle overlays
(one visible at a time). When transition is also supplied, each transition
type (e.g. Trees -> Rangeland) is added as a checkbox overlay that can be
shown simultaneously on top of any classified layer.
When rgb is supplied, each composite (e.g. from dft_stac_composite()) is
added as a switchable overlay beneath the classified and transition layers,
so dated imagery can be toggled under a land-cover label to check it. Every
composite gets the same stretch: each band's display range is set once,
from all the composites pooled, so a brightness or colour difference between
two years is in the data and not an artefact of stretching each image to its
own histogram.
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
# Combined: classified layers + transition overlays
trans <- dft_rast_transition(classified, from = "2017", to = "2023",
from_class = "Trees")
map <- dft_map_interactive(classified, aoi = aoi, transition = trans)
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")
# Dated true-colour composites beneath the land cover (network + gdalcubes)
tc <- dft_stac_composite(aoi, years = c(2017, 2023), months = 6:7)
dft_map_interactive(classified, aoi = aoi, rgb = tc)
} # }