Package 'basemapper'

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

Help Index


basemapper: Headless Spatial Basemap Renderer

Description

logo

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.

Author(s)

Maintainer: Pukar Bhandari [email protected]

Authors:

See Also

Useful links:


An area around a point

Description

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().

Usage

around(center, span, units = "m")

Arguments

center

c(longitude, latitude).

span

c(width, height) on the ground, or one number for a square.

units

The unit of span: "m" (default), "km", "mi" or "ft".

Value

A basemapper_around object: an area in longitude / latitude.

Examples

around(c(-111.9, 40.76), c(20, 15), units = "km")

Create a basemap

Description

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).

Usage

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()
)

Arguments

style

A style, or a list (or character vector) of styles stacked bottom to top: a catalogue name ("OpenFreeMap.Liberty"), a style URL or JSON, tile data, or a style().

...

Not used: the arguments after style must be named.

bounds

The area to show: c(west, south, east, north) in longitude / latitude (west > east across the date line), around() (a ground distance around a point), or a spatial layer (sf, sfc, bbox; its CRS is read).

center

c(longitude, latitude) of the camera; needs zoom.

zoom

The MapLibre zoom (0–22, fractional): which roads, places and labels the style draws.

width, height

The image size, in units.

units

"px" (default), "in", "cm" or "mm"; with a physical unit, dpi sets only the pixel count.

dpi

Image pixels per inch: the sharpness (default 96). In a plot the device sets it, and a basemap's dpi is an error.

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. zoom and symbol_scale each set the other there, so give one. To keep the look of 0.3's geom_basemap(dpi = d) saved at ggsave(dpi = D), use symbol_scale = d / D.

crs

The map's CRS (anything sf::st_crs() takes, or an object with a CRS). Default: the bounds layer's CRS when it is projected, else Web Mercator. In another CRS the map is drawn in Web Mercator and warped into it (sf needed).

clip

Draw only inside a layer's polygons (an sf or sfc object), or outside them with outside(). An inside clip also gives the default extent (and CRS, when projected).

fetch

How the data is downloaded: a fetch_options() object.

Details

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.

Value

A basemap object.

See Also

render(), set_view(), get_bounds().

Examples

## 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 and notes from basemapper

Description

Errors carry classes, so code can handle a kind of failure (as Python's basemapper.ValidationError, StyleError and NetworkError):

Details

  • 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").

Examples

## Not run: 
tryCatch(
  render(basemap("https://example.com/missing.json")),
  basemapper_style_error = function(e) message("no style: ", conditionMessage(e))
)

## End(Not run)

How basemaps download their data

Description

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.

Usage

fetch_options(
  timeout = 10,
  max_tiles = 256L,
  concurrency = 16L,
  on_error = c("skip", "fail")
)

Arguments

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

"skip" draws what there is, each gap a warning of class basemapper_problem; "fail" stops with an error for a failed tile or any other problem.

Value

A basemapper_fetch_options object.

Examples

fetch_options(timeout = 30, on_error = "fail")

Basemaps in ggplot2

Description

Two ways to put a basemap in a ggplot, drawing the same layer:

Usage

geom_basemap(style, ..., alpha = 1, attribution = TRUE)

Arguments

style

A style, a list of styles, or a basemap().

...

basemap()'s arguments that apply in a plot: zoom or symbol_scale, clip, fetch.

alpha

The basemap's opacity (0–1).

attribution

The providers' credit line in the bottom-right corner of each panel: TRUE (their own, see get_attribution()), FALSE (none), or your text.

Details

  • 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().

Value

A ggplot2 layer.

Examples

## 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)

The credit line a basemap's providers ask for

Description

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().

Usage

get_attribution(x)

Arguments

x

A basemap(), a style, or a list of styles.

Value

A character string; "" when the styles give none.

Examples

get_attribution("Esri.WorldImagery")

Read a basemap's view

Description

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.

Usage

get_bounds(m)

get_center(m)

get_zoom(m)

get_size(m)

Arguments

m

A basemap() object.

Details

  • 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.

Value

A named numeric vector (one number for get_zoom()).


The last basemap a plot drew

Description

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().

Usage

last_rendering()

Value

A basemapper_rendering, or NULL before any plot drew a basemap in this session.

See Also

zoom_for() to choose a zoom before drawing.

Examples

## 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)

The layers a style draws

Description

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().

Usage

list_layers(style)

Arguments

style

A style (anything style() takes).

Details

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.

Value

A data frame with columns id, type, source_layer and feature.

Examples

## Not run: 
list_layers("OpenFreeMap.Positron")
list_layers(style("OpenFreeMap.Positron", features = "labels"))   # what would be drawn

## End(Not run)

The built-in basemaps

Description

The named basemaps: their names work as styles ("CartoDB.Positron"), and with provider() for a chosen form. Python: basemapper.list_providers().

Usage

list_providers(type = NULL)

Arguments

type

"vector" or "raster" to list only basemaps with that form; NULL lists all.

Value

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.

Examples

list_providers()
list_providers("vector")$name

The area outside a layer

Description

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().

Usage

outside(layer)

Arguments

layer

Polygons: an sf or sfc object.

Value

A basemapper_outside object.

Examples

## Not run: 
basemap("OpenFreeMap.Liberty", bounds = county, clip = outside(lakes))

## End(Not run)

Show a basemap

Description

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.

Usage

## S3 method for class 'basemap'
plot(x, ...)

## S3 method for class 'basemap'
knit_print(x, options = list(), ...)

Arguments

x

A basemap() object.

...

Unused.

options

knitr chunk options.

Value

x, invisibly (plot()); a knitr image (knit_print()).


Basemap names, for autocompletion

Description

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.

Usage

providers

Format

A named list of character strings.

Examples

providers$Esri.WorldImagery

Render a basemap

Description

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().

Usage

render(m, ..., dpi = NULL)

render_svg(
  m,
  path = NULL,
  ...,
  text = c("editable", "bitmap", "paths"),
  embed_fonts = c("subset", "full", "none"),
  font_dirs = NULL
)

Arguments

m

A basemap() object.

...

Not used: the arguments after m must be named.

dpi

Render at this dpi instead of the basemap's.

path

A file to write, or NULL to return the SVG.

text

"editable": labels as ⁠<text>⁠ with the style's fonts embedded (labels without a font file fall back to "bitmap"); "bitmap": labels from the style's glyph bitmaps (MapLibre's look, not editable); "paths": glyph outlines as paths.

embed_fonts

"subset" (only the glyphs used), "full" or "none" (fonts by name only).

font_dirs

Folders of .ttf / .otf files to use first.

Details

A rendering carries notes on what basemapper decided, adjusted or could not do, shown by their level (see basemapper-errors).

Value

render(): a basemapper_rendering. render_svg(): the SVG as a string (invisibly when written to path).

Examples

## 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)

Convert a rendering

Description

A rendering (render()) holds the image once; these convert it.

Usage

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(), ...)

Arguments

x

A rendering, from render().

path

The file to write.

...

Unused.

options

knitr chunk options.

Details

  • 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().

Value

write_png(): the PNG bytes, invisibly. as_array(): an integer array. as_raster(): a SpatRaster.

Examples

## 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)

Change a basemap

Description

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().

Usage

set_view(m, ..., center, zoom, width, height, units, dpi, symbol_scale)

set_bounds(m, bounds)

Arguments

m

A basemap() object.

...

Not used: the arguments after m must be named.

center, zoom, width, height, units, dpi, symbol_scale

As in basemap(); left out, they keep their value.

bounds

As in basemap().

Value

A new basemap.

Examples

## 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)

Sources with their own options

Description

Helpers for style()'s source, each with that source's own options. A catalogue name or tile data also works as a plain string.

Usage

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
)

Arguments

name

The basemap's name (case-insensitive).

form

"vector" (a MapLibre style) or "raster" (tiles); by default the vector form when there is one. A form the basemap lacks is an error, never a silent fallback.

tiles

For provider(): your own tile data drawn with the basemap's style (a .pmtiles archive, tile URL or TileJSON), in the same schema as its tiles.

input

For tiles(): a tile URL template ({z}/{x}/{y}, {quadkey}, {bbox-epsg-3857}), a TileJSON, a PMTiles archive (URL or path), or an ArcGIS .../MapServer or .../VectorTileServer.

...

Not used: the arguments after the first must be named.

type

"raster" or "vector"; detected when NULL.

tile_size

Raster tile size in px (default 256).

scheme

"xyz" or "tms" (rows counted from the south).

minzoom, maxzoom

The tiles' zoom range (enlarged beyond maxzoom).

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 ⁠fill-*⁠, ⁠line-*⁠ and ⁠circle-*⁠ layers, e.g. list("fill-color" = "#cccccc"). A key that matches no layer is a problem note when rendering.

url

The service URL, without query parameters.

layers

The WMS LAYERS parameter.

styles

The WMS STYLES parameter.

version

The service version (WMS 1.1.x sends SRS, 1.3.0 CRS).

format

The image MIME type.

transparent

Request transparent WMS images.

extra_params

More query parameters, appended verbatim.

layer

The WMTS LAYER parameter.

tilematrixset

A Web Mercator WMTS tile matrix set.

style

The WMTS STYLE parameter.

Details

  • 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().

Value

A basemapper_source object, for style() or as a style itself.

Examples

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: what to draw

Description

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().

Usage

style(source, ..., layers = NULL, features = NULL, key = NULL)

Arguments

source

A catalogue name ("OpenFreeMap.Liberty"; see list_providers()), a MapLibre style URL, file or JSON, tile data (a tile URL, TileJSON, PMTiles archive, ArcGIS service), a source helper (provider(), tiles(), wms(), wmts()), or a style (to change its filters or key).

...

Not used: the arguments after source must be named.

layers

Layer ids to keep, or (--prefixed) to drop; * and ⁠?⁠ are wildcards ("-label_*"). See list_layers().

features

Feature classes to keep or (--prefixed) drop: background, water, landcover, parks, buildings, roads, rail, boundaries, places, poi, terrain, other, and "labels" ("labels" keeps only the labels, "-labels" drops them; c("roads", "labels") keeps the road labels).

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.

Value

A basemapper_style object.

Examples

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)

The tile cache

Description

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.

Usage

tile_cache(dir = NULL, clear = FALSE, max_size = NULL)

Arguments

dir

A directory to move the cache to, or FALSE to turn it off. NULL leaves it as it is.

clear

Delete every cached tile.

max_size

The cache's size limit in megabytes (NULL leaves it as it is).

Details

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().

Value

The cache directory, or NULL when the cache is off (invisibly when dir or clear is given).

Examples

tile_cache()

Basemaps in tmap

Description

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.

Usage

tm_basemap(x, ..., alpha = 1, attribution = TRUE)

Arguments

x

A basemap(), or a style (anything basemap() takes).

...

With a style: basemap()'s arguments that apply in a plot (zoom or symbol_scale, clip, fetch).

alpha

The basemap's opacity (0–1).

attribution

The providers' credit line, as a tmap::tm_credits() element in the bottom-right corner: TRUE (their own), FALSE, or your text.

Value

A tmap element.

Examples

## 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)

The zoom for a map of a given printed width

Description

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().

Usage

zoom_for(bounds, width, units = "in", symbol_scale = 1, crs = NULL)

Arguments

bounds

As in basemap(): c(west, south, east, north), around(), or a spatial layer.

width

The printed width of the map, in units.

units

"in" (default), "cm" or "mm".

symbol_scale

The size of lines and labels at that width (see basemap()).

crs

The map's CRS, as in basemap().

Value

The zoom, a number.

Examples

## 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)