| Title: | Headless Spatial Basemap Renderer |
|---|---|
| Description: | Renders MapLibre GL styled basemaps (vector and raster tiles) headlessly with a Rust core: a software port of MapLibre GL JS's renderer, tested pixel by pixel against MapLibre GL JS. A basemap is an extent, a zoom (the detail) and a dpi (the sharpness), any three of extent, zoom, image size and dpi giving the fourth; it renders to PNG, arrays and rasters in any coordinate reference system, or to true vector SVG, and several styles can be stacked. In 'ggplot2' (ggplot() + m) and 'tmap' v4 (tm_basemap()) plots it is warped into the plot's coordinate reference system and rendered at the output device's resolution. |
| Authors: | Pukar Bhandari [aut, cre] |
| Maintainer: | Pukar Bhandari <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.4.0 |
| Built: | 2026-11-30 20:24:59 UTC |
| Source: | https://github.com/ar-puuk/basemapper |
Renders MapLibre GL styled basemaps (vector and raster tiles) headlessly with a Rust core: a software port of MapLibre GL JS's renderer, tested pixel by pixel against MapLibre GL JS. A basemap is an extent, a zoom (the detail) and a dpi (the sharpness), any three of extent, zoom, image size and dpi giving the fourth; it renders to PNG, arrays and rasters in any coordinate reference system, or to true vector SVG, and several styles can be stacked. In 'ggplot2' (ggplot() + m) and 'tmap' v4 (tm_basemap()) plots it is warped into the plot's coordinate reference system and rendered at the output device's resolution.
Maintainer: Pukar Bhandari [email protected]
Authors:
Pukar Bhandari [email protected]
Useful links:
Report bugs at https://github.com/ar-puuk/basemapper/issues
An area of a true ground distance around a point, for basemap()'s
bounds. It is computed on the sphere (half the span each way), so it
needs neither PROJ nor sf, and is accurate to well under 1% for spans up
to a few hundred kilometres. Python: basemapper.around().
around(center, span, units = "m")around(center, span, units = "m")
center |
|
span |
|
units |
The unit of |
A basemapper_around object: an area in longitude / latitude.
around(c(-111.9, 40.76), c(20, 15), units = "km")around(c(-111.9, 40.76), c(20, 15), units = "km")
A basemap is what to draw (one style, or several stacked bottom to top),
what is given of its view, its CRS, a clip and fetch options. It renders on
its own (render()) or goes into a plot (ggplot(nc) + m,
tm_basemap(m)), which supplies what it leaves out. The same object exists
in Python (basemapper.basemap()) and Rust (Basemap).
basemap( style, ..., bounds = NULL, center = NULL, zoom = NULL, width = NULL, height = NULL, units = "px", dpi = NULL, symbol_scale = NULL, crs = NULL, clip = NULL, fetch = fetch_options() )basemap( style, ..., bounds = NULL, center = NULL, zoom = NULL, width = NULL, height = NULL, units = "px", dpi = NULL, symbol_scale = NULL, crs = NULL, clip = NULL, fetch = fetch_options() )
style |
A style, or a list (or character vector) of styles stacked
bottom to top: a catalogue name ( |
... |
Not used: the arguments after |
bounds |
The area to show: |
center |
|
zoom |
The MapLibre zoom (0–22, fractional): which roads, places and labels the style draws. |
width, height
|
The image size, in |
units |
|
dpi |
Image pixels per inch: the sharpness (default 96). In a plot
the device sets it, and a basemap's |
symbol_scale |
The size of lines, labels and icons on the page, as a
multiple of the style's designed size. At 1 (the default) a CSS pixel is
1/96 inch, so a label the style sizes at 12 px prints at 9 pt; 0.84
prints it at 84% of that. It changes what is drawn, not the sharpness:
with the extent and printed size fixed (always so in a plot), a smaller
symbol scale leaves room for more detail (a higher zoom), a larger one
for less. |
crs |
The map's CRS (anything |
clip |
Draw only inside a layer's polygons (an |
fetch |
How the data is downloaded: a |
Each control has one meaning everywhere:
the extent (bounds, or center with zoom): where;
zoom: the detail (which roads, places and labels the style draws);
symbol_scale: the size of lines, labels and icons on the page;
dpi: the sharpness (image pixels per inch), and nothing else.
Any three of extent, zoom, printed size and symbol scale give the
fourth; the dpi turns the printed size into pixels. A width /
height in "in", "cm" or "mm" is the printed size; in pixels it is a
printed size through the dpi (px / dpi inches), so with the pixels fixed,
a higher dpi prints the map smaller. What nothing gives is defaulted, in
this order: dpi (96), symbol scale (1), image size (800 x 600 px at 96 dpi,
scaled with the dpi), extent (the world); the zoom is never a default.
Over-determined requests are errors that say what to drop. In a plot, the
plot gives the extent and printed size and the device the dpi: give a
zoom or a symbol_scale. Add it to a ggplot with +; use
geom_basemap() instead only to set the layer's alpha or
attribution.
A basemap object.
render(), set_view(), get_bounds().
## Not run: m <- basemap("OpenFreeMap.Liberty", center = c(-111.8887, 40.7556), zoom = 12.4, dpi = 192) img <- render(m) # 1600 x 1200 px, sharp write_png(img, "slc.png") render_svg(m, "slc.svg") # vector # This area at this detail: the size follows nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) basemap("OpenFreeMap.Positron", bounds = nc, zoom = 7) # 20 x 15 km around a point basemap("OpenFreeMap.Liberty", bounds = around(c(-111.9, 40.76), c(20, 15), units = "km")) ## End(Not run)## Not run: m <- basemap("OpenFreeMap.Liberty", center = c(-111.8887, 40.7556), zoom = 12.4, dpi = 192) img <- render(m) # 1600 x 1200 px, sharp write_png(img, "slc.png") render_svg(m, "slc.svg") # vector # This area at this detail: the size follows nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) basemap("OpenFreeMap.Positron", bounds = nc, zoom = 7) # 20 x 15 km around a point basemap("OpenFreeMap.Liberty", bounds = around(c(-111.9, 40.76), c(20, 15), units = "km")) ## End(Not run)
Errors carry classes, so code can handle a kind of failure (as Python's
basemapper.ValidationError, StyleError and NetworkError):
basemapper_validation_error: invalid input, found before anything is
downloaded (a view, style, filter, CRS or clip that cannot work).
basemapper_style_error: the style could not be downloaded or read.
basemapper_network_error: data could not be downloaded (a wrong or
missing API key, a server down).
basemapper_render_error: anything else.
Every one also has class basemapper_error.
Renders also carry notes on what basemapper decided, adjusted or could
not do (see render()). They are shown by their level:
decided (a value nothing gave, such as the automatic zoom): in
print(), and as messages with options(basemapper.verbose = TRUE);
adjusted (something asked for was changed, such as the zoom raised
to keep the view inside the world): a message of class
basemapper_adjustment, once per kind per session;
problem (the output is incomplete, such as tiles that failed): a
warning of class basemapper_problem, every time, or an error with
fetch_options(on_error = "fail").
## Not run: tryCatch( render(basemap("https://example.com/missing.json")), basemapper_style_error = function(e) message("no style: ", conditionMessage(e)) ) ## End(Not run)## Not run: tryCatch( render(basemap("https://example.com/missing.json")), basemapper_style_error = function(e) message("no style: ", conditionMessage(e)) ) ## End(Not run)
Settings for downloading a basemap's data, given to the fetch argument
of basemap(). The defaults suit most maps; change them for slow
servers, very large maps, or to stop on missing data. Python:
basemapper.FetchOptions; Rust: FetchOptions.
fetch_options( timeout = 10, max_tiles = 256L, concurrency = 16L, on_error = c("skip", "fail") )fetch_options( timeout = 10, max_tiles = 256L, concurrency = 16L, on_error = c("skip", "fail") )
timeout |
Seconds to wait for each download. |
max_tiles |
Most tiles fetched per style. A view needing more is an error for vector tiles; raster tiles are fetched one zoom level coarser instead (an adjusted note). |
concurrency |
Downloads at once. |
on_error |
|
A basemapper_fetch_options object.
fetch_options(timeout = 30, on_error = "fail")fetch_options(timeout = 30, on_error = "fail")
Two ways to put a basemap in a ggplot, drawing the same layer:
geom_basemap(style, ..., alpha = 1, attribution = TRUE)geom_basemap(style, ..., alpha = 1, attribution = TRUE)
style |
A style, a list of styles, or a |
... |
|
alpha |
The basemap's opacity (0–1). |
attribution |
The providers' credit line in the bottom-right corner
of each panel: |
ggplot(nc) + basemap(...): the usual way. basemap() describes the
map (its style, zoom or symbol_scale, clip, fetch); added to a
plot, it is drawn at full opacity with the providers' credit line in the
corner.
ggplot(nc) + geom_basemap(...): when you need a setting of the layer
itself, which a basemap does not have: alpha (fade it) or
attribution (turn the credit line off, or write your own). It takes
basemap()'s arguments, or a basemap you already made:
geom_basemap(m, alpha = 0.5).
So + basemap("OpenFreeMap.Liberty", zoom = 8) and
+ geom_basemap("OpenFreeMap.Liberty", zoom = 8) are the same plot; only
alpha and attribution need geom_basemap(). In tmap,
tm_basemap() takes all of these.
The plot gives the extent (the panel's limits), the CRS
(coord_sf()'s; the basemap is warped into it), the image size (the
panel) and the sharpness: on screen 96 dpi, in a raster export
(ggsave(), png(), ragg) the device's dpi, in PDF / SVG 300 dpi.
What the map shows is set in one place, in basemap() (or
geom_basemap()): zoom (the detail; the size of lines and labels follows) or
symbol_scale (the size of lines and labels; the zoom follows). Give one.
The plot fixes the extent, so a larger symbol scale also means a lower
zoom (fewer roads and labels) and a smaller one more detail. With
neither, the symbol scale is 1 and the map is the same at any dpi. A
basemap's dpi, extent, CRS or size is an error in a plot. A clip is a
mask, in the plot's CRS. last_rendering() reads what a drawn plot used;
zoom_for() chooses a zoom in advance. Python (plotnine):
ggplot(df) + m, geom_basemap().
A ggplot2 layer.
## Not run: library(ggplot2) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) # The usual way: a basemap added to the plot ggplot(nc) + basemap("OpenFreeMap.Positron") + geom_sf(fill = NA) ggplot(nc) + basemap("OpenFreeMap.Positron", zoom = 8) + geom_sf(fill = NA) # Smaller lines and labels, more detail; ggsave() sets the sharpness p <- ggplot(nc) + basemap("OpenFreeMap.Positron", symbol_scale = 0.8) + geom_sf(fill = NA) ggsave("nc.png", p, width = 8, height = 3, dpi = 300) # geom_basemap() for the layer's own settings: faded, credit in a caption m <- basemap("OpenFreeMap.Positron") ggplot(nc) + geom_basemap(m, alpha = 0.5, attribution = FALSE) + geom_sf(fill = NA) + labs(caption = get_attribution(m)) ## End(Not run)## Not run: library(ggplot2) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) # The usual way: a basemap added to the plot ggplot(nc) + basemap("OpenFreeMap.Positron") + geom_sf(fill = NA) ggplot(nc) + basemap("OpenFreeMap.Positron", zoom = 8) + geom_sf(fill = NA) # Smaller lines and labels, more detail; ggsave() sets the sharpness p <- ggplot(nc) + basemap("OpenFreeMap.Positron", symbol_scale = 0.8) + geom_sf(fill = NA) ggsave("nc.png", p, width = 8, height = 3, dpi = 300) # geom_basemap() for the layer's own settings: faded, credit in a caption m <- basemap("OpenFreeMap.Positron") ggplot(nc) + geom_basemap(m, alpha = 0.5, attribution = FALSE) + geom_sf(fill = NA) + labs(caption = get_attribution(m)) ## End(Not run)
Every source the styles draw from contributes its attribution (from the
style, its TileJSON or PMTiles metadata), as plain text without
duplicates, joined with " | ". The plot layers draw it by default; use
it for a caption, e.g. labs(caption = get_attribution(m)). Python:
basemapper.get_attribution().
get_attribution(x)get_attribution(x)
x |
A |
A character string; "" when the styles give none.
get_attribution("Esri.WorldImagery")get_attribution("Esri.WorldImagery")
The view's values, each given, derived or defaulted by the core's one
rule. The view never reaches past the poles. Python: the properties
m.bounds, m.center, m.zoom, m.size.
get_bounds(m) get_center(m) get_zoom(m) get_size(m)get_bounds(m) get_center(m) get_zoom(m) get_size(m)
m |
A |
get_bounds(): c(xmin, ymin, xmax, ymax) of the map in its CRS: at
least the extent given, grown to the image's shape.
get_center(): c(longitude, latitude) of the centre.
get_zoom(): the MapLibre zoom.
get_size(): c(width, height) of the image in pixels.
print(m) shows these with the dpi, CRS, scale and where each value came
from.
A named numeric vector (one number for get_zoom()).
A plot renders its basemap when it is drawn (printed, saved, knitted), for
the panel's size and the device's dpi. last_rendering() returns that
rendering, to read what it was drawn at: $zoom, $symbol_scale, $dpi,
$width, $height, $scale (the printed 1:N) and its notes. With several
panels or basemaps, it is the last one drawn. Python:
basemapper.last_rendering().
last_rendering()last_rendering()
A basemapper_rendering, or NULL before any plot drew a
basemap in this session.
zoom_for() to choose a zoom before drawing.
## Not run: library(ggplot2) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) p <- ggplot(nc) + geom_basemap("OpenFreeMap.Positron", symbol_scale = 0.84) + geom_sf(fill = NA) ggsave("nc.png", p, width = 6, height = 3, dpi = 200) r <- last_rendering() r$zoom r$symbol_scale ## End(Not run)## Not run: library(ggplot2) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) p <- ggplot(nc) + geom_basemap("OpenFreeMap.Positron", symbol_scale = 0.84) + geom_sf(fill = NA) ggsave("nc.png", p, width = 6, height = 3, dpi = 200) r <- last_rendering() r$zoom r$symbol_scale ## End(Not run)
A style's layers in drawing order, with its filters applied: their id
(what a style's layers matches), type (symbol layers are the
labels), source_layer (the vector tile layer drawn; NA for background
and raster layers) and feature (what features matches). A style with
filters lists the layers it keeps, so this previews a filter:
list_layers(style(s, features = "roads")). A catalogue stack lists every
part's layers, bottom to top. Python: basemapper.list_layers().
list_layers(style)list_layers(style)
style |
A style (anything |
A layer's feature comes from its source layer, with tables for the
OpenMapTiles, Protomaps and Shortbread schemas and keywords for others: a
convenience to check here, with layers for exact control.
A data frame with columns id, type, source_layer and
feature.
## Not run: list_layers("OpenFreeMap.Positron") list_layers(style("OpenFreeMap.Positron", features = "labels")) # what would be drawn ## End(Not run)## Not run: list_layers("OpenFreeMap.Positron") list_layers(style("OpenFreeMap.Positron", features = "labels")) # what would be drawn ## End(Not run)
The named basemaps: their names work as styles ("CartoDB.Positron"),
and with provider() for a chosen form. Python:
basemapper.list_providers().
list_providers(type = NULL)list_providers(type = NULL)
type |
|
A data frame with columns name; vector and raster (logical:
the forms it has); api_key_env (the environment variable its API key
is read from, NA when it needs none); and attribution.
list_providers() list_providers("vector")$namelist_providers() list_providers("vector")$name
The area outside a layer's polygons, for basemap()'s clip: the
basemap everywhere except inside them. outside() only marks the layer;
the polygon-only rule applies as for clip = layer. Unlike an inside
clip, it sets no default extent or CRS. Python: basemapper.outside().
outside(layer)outside(layer)
layer |
Polygons: an |
A basemapper_outside object.
## Not run: basemap("OpenFreeMap.Liberty", bounds = county, clip = outside(lakes)) ## End(Not run)## Not run: basemap("OpenFreeMap.Liberty", bounds = county, clip = outside(lakes)) ## End(Not run)
plot() renders the basemap and draws it on the current graphics device.
In R Markdown and Quarto documents, a basemap printed by a chunk renders
and shows as an image at its CSS size, sharp at any dpi; print(m)
describes it instead. The image's alt text is the chunk's fig.alt, or
else a description of the map: its styles, where it is centred, its zoom
and scale, and its CRS when not Web Mercator.
## S3 method for class 'basemap' plot(x, ...) ## S3 method for class 'basemap' knit_print(x, options = list(), ...)## S3 method for class 'basemap' plot(x, ...) ## S3 method for class 'basemap' knit_print(x, options = list(), ...)
x |
A |
... |
Unused. |
options |
knitr chunk options. |
x, invisibly (plot()); a knitr image (knit_print()).
A named list of every basemap name: providers$CartoDB.Positron is the
string "CartoDB.Positron", usable as a style or with provider(). The names match leaflet::providers where both
have a basemap. Python: basemapper.providers.CartoDB.Positron.
providersproviders
A named list of character strings.
providers$Esri.WorldImageryproviders$Esri.WorldImagery
render() renders the map: a basemapper_rendering with its pixels and
where they are, converted with write_png(), as_array(),
as_raster() or plot(). render(m, dpi = 300) is
render(set_view(m, dpi = 300)): the same map, sharper (when the image
size is not fixed in pixels). render_svg() draws an SVG document; SVG is
Web Mercator for now, so a map in another CRS is an error. Python:
m.render(), m.render_svg().
render(m, ..., dpi = NULL) render_svg( m, path = NULL, ..., text = c("editable", "bitmap", "paths"), embed_fonts = c("subset", "full", "none"), font_dirs = NULL )render(m, ..., dpi = NULL) render_svg( m, path = NULL, ..., text = c("editable", "bitmap", "paths"), embed_fonts = c("subset", "full", "none"), font_dirs = NULL )
m |
A |
... |
Not used: the arguments after |
dpi |
Render at this dpi instead of the basemap's. |
path |
A file to write, or |
text |
|
embed_fonts |
|
font_dirs |
Folders of .ttf / .otf files to use first. |
A rendering carries notes on what basemapper decided, adjusted or could not do, shown by their level (see basemapper-errors).
render(): a basemapper_rendering. render_svg(): the SVG as a
string (invisibly when written to path).
## Not run: m <- basemap("OpenFreeMap.Liberty", center = c(-111.89, 40.76), zoom = 12) img <- render(m) img write_png(img, "map.png") a <- as_array(img) # [height, width, 4], with bounds and CRS r <- as_raster(img) # terra SpatRaster write_png(render(m, dpi = 300), "map-300.png") ## End(Not run)## Not run: m <- basemap("OpenFreeMap.Liberty", center = c(-111.89, 40.76), zoom = 12) img <- render(m) img write_png(img, "map.png") a <- as_array(img) # [height, width, 4], with bounds and CRS r <- as_raster(img) # terra SpatRaster write_png(render(m, dpi = 300), "map-300.png") ## End(Not run)
A rendering (render()) holds the image once; these convert it.
write_png(x, path) as_array(x) as_raster(x) ## S3 method for class 'basemapper_rendering' plot(x, ...) ## S3 method for class 'basemapper_rendering' knit_print(x, options = list(), ...)write_png(x, path) as_array(x) as_raster(x) ## S3 method for class 'basemapper_rendering' plot(x, ...) ## S3 method for class 'basemapper_rendering' knit_print(x, options = list(), ...)
x |
A rendering, from |
path |
The file to write. |
... |
Unused. |
options |
knitr chunk options. |
write_png() writes a PNG file (and returns its bytes invisibly).
as_array() gives an integer array [height, width, 4] (RGBA, 0–255,
straight alpha) with the bounds and CRS as attributes.
as_raster() gives a terra::rast() in the rendering's CRS (layers red,
green, blue, alpha; terra needed).
plot() draws it on the current device; print() describes it,
including its notes.
A rendering's fields: width, height, bounds, crs, zoom,
tile_zoom, dpi, scale (1:N at the centre, the same at any dpi) and
notes (a data frame: level, kind, message, count, details).
Python: img.save(), img.to_numpy(), img.to_xarray().
write_png(): the PNG bytes, invisibly. as_array(): an integer
array. as_raster(): a SpatRaster.
## Not run: img <- render(basemap("OpenFreeMap.Liberty", center = c(-111.89, 40.76), zoom = 12)) write_png(img, "map.png") dim(as_array(img)) img$notes ## End(Not run)## Not run: img <- render(basemap("OpenFreeMap.Liberty", center = c(-111.89, 40.76), zoom = 12)) write_png(img, "map.png") dim(as_array(img)) img$notes ## End(Not run)
A basemap never changes: these return a new one (value semantics, as
everything in R). set_view() changes what is given of the view (NULL
unsets a value; a center replaces the bounds); set_bounds() shows an
area (the centre is unset). Python: m.set_view(), m.set_bounds().
set_view(m, ..., center, zoom, width, height, units, dpi, symbol_scale) set_bounds(m, bounds)set_view(m, ..., center, zoom, width, height, units, dpi, symbol_scale) set_bounds(m, bounds)
m |
A |
... |
Not used: the arguments after |
center, zoom, width, height, units, dpi, symbol_scale
|
As in |
bounds |
As in |
A new basemap.
## Not run: m <- basemap("OpenFreeMap.Liberty", center = c(-111.89, 40.76), zoom = 12) set_view(m, zoom = 10) set_view(m, dpi = 300, width = 6, height = 4, units = "in") ## End(Not run)## Not run: m <- basemap("OpenFreeMap.Liberty", center = c(-111.89, 40.76), zoom = 12) set_view(m, zoom = 10) set_view(m, dpi = 300, width = 6, height = 4, units = "in") ## End(Not run)
Helpers for style()'s source, each with that source's own options.
A catalogue name or tile data also works as a plain string.
provider(name, form = NULL, tiles = NULL) tiles( input, ..., type = NULL, tile_size = NULL, scheme = NULL, minzoom = NULL, maxzoom = NULL, attribution = NULL, source_layers = NULL, paint = NULL ) wms( url, layers, ..., styles = "", version = "1.3.0", format = "image/png", transparent = FALSE, extra_params = NULL ) wmts( url, layer, ..., tilematrixset = "GoogleMapsCompatible", format = "image/png", style = "default", version = "1.0.0", extra_params = NULL )provider(name, form = NULL, tiles = NULL) tiles( input, ..., type = NULL, tile_size = NULL, scheme = NULL, minzoom = NULL, maxzoom = NULL, attribution = NULL, source_layers = NULL, paint = NULL ) wms( url, layers, ..., styles = "", version = "1.3.0", format = "image/png", transparent = FALSE, extra_params = NULL ) wmts( url, layer, ..., tilematrixset = "GoogleMapsCompatible", format = "image/png", style = "default", version = "1.0.0", extra_params = NULL )
name |
The basemap's name (case-insensitive). |
form |
|
tiles |
For |
input |
For |
... |
Not used: the arguments after the first must be named. |
type |
|
tile_size |
Raster tile size in px (default 256). |
scheme |
|
minzoom, maxzoom
|
The tiles' zoom range (enlarged beyond |
attribution |
Attribution text for the source. |
source_layers |
Vector tiles: the source layers to draw (default all). |
paint |
Vector tiles: a named list of paint properties for the
generated |
url |
The service URL, without query parameters. |
layers |
The WMS |
styles |
The WMS |
version |
The service version (WMS 1.1.x sends |
format |
The image MIME type. |
transparent |
Request transparent WMS images. |
extra_params |
More query parameters, appended verbatim. |
layer |
The WMTS |
tilematrixset |
A Web Mercator WMTS tile matrix set. |
style |
The WMTS |
provider(): a catalogue basemap, in a chosen form, or drawing your own
tile data with its style.
tiles(): tile data, with a style built for it and overrides.
wms(), wmts(): OGC services, as 256 px tiles in Web Mercator. A
RESTful WMTS template ({TileMatrix}/{TileRow}/{TileCol}) needs no
helper: give it as the style.
Python: basemapper.provider(), tiles(), wms(), wmts().
A basemapper_source object, for style() or as a style itself.
style(provider("CartoDB.Positron", form = "raster"), key = "...") style(tiles("https://example.com/{z}/{x}/{y}.mvt", source_layers = "land", paint = list("fill-color" = "#e8e0d8"))) style(wms("https://example.com/wms", layers = "relief"))style(provider("CartoDB.Positron", form = "raster"), key = "...") style(tiles("https://example.com/{z}/{x}/{y}.mvt", source_layers = "land", paint = list("fill-color" = "#e8e0d8"))) style(wms("https://example.com/wms", layers = "relief"))
A style is a source and what to draw of it: its own filters and
credential. A character string works wherever a style is expected and
means style(string). Each style of a stack is filtered on its own; a
catalogue stack ("UGRC.Lite") is one style. The core checks a style
when it is made, so a bad filter, unknown catalogue name or missing key
is an error here. Python: basemapper.style().
style(source, ..., layers = NULL, features = NULL, key = NULL)style(source, ..., layers = NULL, features = NULL, key = NULL)
source |
A catalogue name ( |
... |
Not used: the arguments after |
layers |
Layer ids to keep, or ( |
features |
Feature classes to keep or ( |
key |
The credential: an API key for catalogue basemaps (default from their environment variable), else a token sent only to the style's own server. |
A basemapper_style object.
style("OpenFreeMap.Liberty", features = "-labels") ## Not run: imagery <- style("Esri.WorldImagery") labels <- style("OpenFreeMap.Liberty", features = "labels") basemap(list(imagery, labels), center = c(-111.89, 40.76), zoom = 13) ## End(Not run)style("OpenFreeMap.Liberty", features = "-labels") ## Not run: imagery <- style("Esri.WorldImagery") labels <- style("OpenFreeMap.Liberty", features = "labels") basemap(list(imagery, labels), center = c(-111.89, 40.76), zoom = 13) ## End(Not run)
Map tiles are cached on disk for as long as the server's caching headers
allow (a day when they say nothing), so repeated renders of an area do
not download them again. The cache starts in BASEMAPPER_CACHE_DIR when
that is set ("off" disables it), else in
tools::R_user_dir("basemapper", "cache"). Set BASEMAPPER_CACHE_DIR in
.Renviron (usethis::edit_r_environ() opens it) to keep a folder for good,
or to share one cache with Python.
tile_cache(dir = NULL, clear = FALSE, max_size = NULL)tile_cache(dir = NULL, clear = FALSE, max_size = NULL)
dir |
A directory to move the cache to, or |
clear |
Delete every cached tile. |
max_size |
The cache's size limit in megabytes ( |
The cache is kept small: expired tiles are deleted, and past its size
limit (250 MB, or BASEMAPPER_CACHE_MAX_SIZE in MB) the least recently
used tiles go first, in the background as tiles are stored. Python:
basemapper.tile_cache().
The cache directory, or NULL when the cache is off (invisibly
when dir or clear is given).
tile_cache()tile_cache()
tm_basemap(m) puts a basemap() under a tmap v4 map; tm_basemap(style, ...) builds the basemap from basemap()'s arguments (tmap only accepts
its own elements, so this is the way in). As in ggplot2, the map gives the
extent, the CRS (the basemap is warped into it), the image size and the
sharpness (the device's dpi); what the map shows is set by the basemap's
zoom or symbol_scale, and a basemap's dpi is an error. Put it before
tm_shape(), so it draws behind the layers.
tm_basemap(x, ..., alpha = 1, attribution = TRUE)tm_basemap(x, ..., alpha = 1, attribution = TRUE)
x |
|
... |
With a style: |
alpha |
The basemap's opacity (0–1). |
attribution |
The providers' credit line, as a |
A tmap element.
## Not run: library(tmap) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) tm_basemap("OpenFreeMap.Positron") + tm_shape(nc) + tm_polygons(fill = NA) ## End(Not run)## Not run: library(tmap) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) tm_basemap("OpenFreeMap.Positron") + tm_shape(nc) + tm_polygons(fill = NA) ## End(Not run)
zoom_for() is the MapLibre zoom at which bounds drawn width wide
has lines and labels at symbol_scale times their designed size, by the
same rules as basemap() (in the map's CRS: crs, else the layer's when
projected, else Web Mercator). In a plot, give it as the zoom: the same
zoom draws the same picture at any panel size, so a page map and its
thumbnail match, while symbol_scale keeps the label size instead.
Python: basemapper.zoom_for().
zoom_for(bounds, width, units = "in", symbol_scale = 1, crs = NULL)zoom_for(bounds, width, units = "in", symbol_scale = 1, crs = NULL)
bounds |
As in |
width |
The printed width of the map, in |
units |
|
symbol_scale |
The size of lines and labels at that width (see
|
crs |
The map's CRS, as in |
The zoom, a number.
## Not run: z <- zoom_for(tract, 3.6, symbol_scale = 0.84) ggplot(tract) + geom_basemap("UGRC.Lite", zoom = z) # the page map # The same zoom in a 2.27 in thumbnail: the same picture, smaller ## End(Not run)## Not run: z <- zoom_for(tract, 3.6, symbol_scale = 0.84) ggplot(tract) + geom_basemap("UGRC.Lite", zoom = z) # the page map # The same zoom in a 2.27 in thumbnail: the same picture, smaller ## End(Not run)