Skip to contents

geo_webmap() turns a map built with geo_map() (or a single sf or SpatRaster layer) into an interactive web map: pan and zoom, a choice of basemaps, layers that switch on and off, the attributes of any feature on click, a scale bar and a legend. Every layer keeps the symbols of the printed map: the same colours, classes, presets, line dashes and point shapes, and the same legend sections.

The result is one self-contained HTML file: features are embedded as GeoJSON and rasters as images, so the page can be emailed, put on a website or opened offline (only the basemap tiles and the Leaflet library are fetched from the web).

Usage

geo_webmap(
  x,
  file = NULL,
  basemap = c("topo", "imagery", "osm", "light"),
  title = NULL,
  popup = TRUE,
  max_pixels = 1600
)

Arguments

x

A map from geo_map(), an sf table, or a SpatRaster.

file

Optional path of the HTML file to write.

basemap

Basemap shown first: "topo" (Esri World Topographic), "imagery" (Esri World Imagery), "osm" (OpenStreetMap) or "light" (CARTO Positron). All four are offered in the layer menu.

title

Page title; defaults to the map's title.

popup

Show feature attributes on click?

max_pixels

Largest side of embedded raster images.

Value

An object of class tmr_webmap holding the HTML; it prints by opening the page in a browser (or the RStudio viewer) when interactive. With file, the path is returned invisibly.

See also

geo_map() for the printed map.

Examples

elev <- terra::rast(system.file("ex/elev.tif", package = "terra"))
cantons <- sf::st_read(system.file("ex/lux.shp", package = "terra"), quiet = TRUE)
m <- geo_map(title = "Luxembourg") |>
  map_raster(elev, name = "Elevation (m)", palette = "Terrain 2") |>
  map_polygons(cantons, fill = NA, preset = "admin") |>
  map_labels(cantons, text = NAME_2)
w <- geo_webmap(m)
f <- tempfile(fileext = ".html")
geo_webmap(m, file = f)