Python or Rust? The same functions and arguments exist in Python (
m.verb(...)for R’sverb(m, ...)) and Rust.
basemapper renders MapLibre GL basemaps headlessly with a Rust core: no browser, display server, GPU or GDAL. Vector styles are drawn by a software port of MapLibre GL JS’s renderer, so maps look as they do in MapLibre. Everything on this page was run when the page was built; the maps are the output of the code above them.
Installation
# Any one of these installs the v0.1.1 release; each compiles the Rust core.
pak::pak("ar-puuk/basemapper/r-basemapper@v0.1.1")
remotes::install_github("ar-puuk/basemapper", subdir = "r-basemapper", ref = "v0.1.1")
devtools::install_github("ar-puuk/basemapper", subdir = "r-basemapper", ref = "v0.1.1")Choose a style
Every function takes a style. The simplest is a named
basemap, a leaflet-providers
name; list_providers() lists them, with the API key some
need:
library(tmap) # (for the tmap example below) before basemapper,
#> Registered S3 method overwritten by 'stars':
#> method from
#> st_interpolate_aw.stars sf
library(basemapper) # whose tm_basemap() replaces tmap's
#>
#> Attaching package: 'basemapper'
#> The following object is masked from 'package:tmap':
#>
#> tm_basemap
p <- list_providers()
head(p[, c("name", "vector", "raster", "api_key_env")], 12)
#> name vector raster api_key_env
#> 1 OpenStreetMap.Mapnik FALSE TRUE <NA>
#> 2 OpenStreetMap.DE FALSE TRUE <NA>
#> 3 OpenStreetMap.France FALSE TRUE <NA>
#> 4 OpenStreetMap.HOT FALSE TRUE <NA>
#> 5 OpenTopoMap FALSE TRUE <NA>
#> 6 CyclOSM FALSE TRUE <NA>
#> 7 OpenFreeMap.Liberty TRUE FALSE <NA>
#> 8 OpenFreeMap.Bright TRUE FALSE <NA>
#> 9 OpenFreeMap.Positron TRUE FALSE <NA>
#> 10 OpenFreeMap.Dark TRUE FALSE <NA>
#> 11 OpenFreeMap.Fiord TRUE FALSE <NA>
#> 12 CartoDB.Positron TRUE TRUE CARTO_API_KEYA MapLibre style URL or JSON works too, and so does tile data: a tile URL, TileJSON, PMTiles archive or ArcGIS service (see Your own tiles and services).
A map on its own
basemap() creates a view: a centre and MapLibre zoom (or
bounds to fit), and a size in CSS pixels. Printed in a document, a view
shows the map:

print() describes it, and the get_*()
functions read it:
print(m)
#> <basemap> 1 style, 720 x 405 @ 1x (720 x 405 px), center (-111.88870, 40.75560), zoom 12.400
get_bounds(m, lonlat = TRUE)
#> xmin ymin xmax ymax
#> -111.93553 40.73564 -111.84187 40.77555
get_attribution(m)
#> [1] "OpenFreeMap © OpenMapTiles Data from OpenStreetMap"set_view() and set_bounds() change the
view. Here it fits North Carolina’s counties, from {sf}:
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
m <- set_bounds(basemap("OpenFreeMap.Liberty", 720, 300), nc)
m
Raster and vector, PNG and SVG
Basemaps come as raster tiles (images, like OpenStreetMap’s) or
vector tiles (geometry styled on the fly, like OpenFreeMap’s). Either
renders to a PNG with render_png() or an SVG with
render_svg(). In an SVG, vector layers are paths and the
labels editable text with the style’s fonts embedded; raster tiles are
embedded as images.
slc <- c(-111.8910, 40.7608)
raster <- basemap("OpenStreetMap.Mapnik", 480, 300, center = slc, zoom = 11.5)
vector <- basemap("OpenFreeMap.Bright", 480, 300, center = slc, zoom = 11.5)
out <- file.path(tempdir(), c("raster.png", "raster.svg", "vector.png", "vector.svg"))
render_png(raster, path = out[1])
render_svg(raster, path = out[2])
render_png(vector, path = out[3])
render_svg(vector, path = out[4])
data.frame(file = basename(out), kb = round(file.size(out) / 1024))
#> file kb
#> 1 raster.png 229
#> 2 raster.svg 306
#> 3 vector.png 82
#> 4 vector.svg 430| PNG | SVG | |
|---|---|---|
|
Raster OpenStreetMap |
|
|
|
Vector OpenFreeMap Bright |
|
|
render_raster() returns a georeferenced {terra} raster,
and render_array() the pixels:
render_raster(vector)
#> class : SpatRaster
#> size : 300, 480, 4 (nrow, ncol, nlyr)
#> resolution : 27.02457, 27.02457 (x, y)
#> extent : -1.246214e+07, -1.244916e+07, 4973070, 4981177 (xmin, xmax, ymin, ymax)
#> coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
#> source(s) : memory
#> colors rgb : 1, 2, 3
#> names : red, green, blue, alpha
#> min values : 6, 6, 6, 255
#> max values : 255, 255, 255, 255
dim(render_array(vector))
#> [1] 300 480 4pixel_ratio makes images larger with the same view and
label layout: pixel_ratio = 2 for a sharp 2x image,
dpi / 96 for print.
In a plot
geom_basemap() adds a basemap to a {ggplot2} plot. It
renders when the plot is drawn, for the panel’s extent and at the
device’s resolution, and credits the provider in the corner:
library(ggplot2)
ggplot(nc) +
geom_basemap("OpenFreeMap.Positron") +
geom_sf(aes(fill = BIR74 / 1000), colour = "white", linewidth = 0.2, alpha = 0.75) +
scale_fill_viridis_c("Births, 1974\n(thousands)") +
theme_void()
tm_basemap() does the same in {tmap} (attach tmap first:
it has a tm_basemap() of its own):
tm_shape(nc) +
tm_basemap("Esri.WorldGrayCanvas") +
tm_polygons(fill = "SID74", fill_alpha = 0.7, fill.legend = tm_legend("SIDS, 1974"))
Plots with ggplot2 and tmap has more.
The tile cache
Tiles are cached on disk, so drawing an area again does not download its tiles again. They are kept for as long as each server allows (a day when it says nothing), and the cache looks after itself: expired tiles are deleted, and past 250 MB the least recently used tiles go first.
tile_cache() # where the tiles are kept
#> [1] "/home/runner/.cache/R/basemapper"
tile_cache(max_size = 500) # a 500 MB limit
tile_cache("D:/basemap-tiles") # another folder, for this session
tile_cache(FALSE) # no cache, for this session
tile_cache(clear = TRUE) # delete the cached tilesTo keep a folder or limit for good, set them in your
.Renviron (usethis::edit_r_environ() opens it)
and restart R:
BASEMAPPER_CACHE_DIR=D:/basemap-tiles
BASEMAPPER_CACHE_MAX_SIZE=500
Python reads the same variables, so one folder can serve both
languages. Without them, R keeps its tiles in
tools::R_user_dir("basemapper", "cache").
Next
- Basemap gallery: every basemap that needs no API key.
- Plots with ggplot2 and tmap: projected data, opacity, credits and print resolution.
- Stacking, labels and features: several basemaps in one, and parts of them.
- Your own tiles and services: tile URLs, PMTiles, ArcGIS and OGC services, and styles for your own data.