add_rpic() adds a georeferenced pic drawing to a
mapgl map — mapgl::maplibre() or
mapgl::mapboxgl() — as mapgl's own add_*() verbs do, map first, so it
joins a pipe: maplibre() |> add_rpic(pic(src)). It adds a GeoJSON
source "pic"
holding the engine's FeatureCollection, the six layers of
rpic_maplibre_layers() (added with mapgl::add_layer()) and, when
fit, a mapgl::fit_bounds() on the drawing (48 px of padding, zoom 19
at most — the fit of the engine's JavaScript attach()).
Usage
add_rpic(
map,
x,
...,
before = NULL,
text_scale = 1,
font = "style",
fit = TRUE
)
rpic_mapgl(
map,
x,
...,
before = NULL,
text_scale = 1,
font = "style",
fit = TRUE
)Arguments
- map
a map from
mapgl::maplibre()ormapgl::mapboxgl()(Shiny proxies are not supported).- x
the drawing: the result of
rpic_geojson()(or any GeoJSON FeatureCollection text: a string of class"json", or one whose first non-blank character is an opening brace or bracket — so pic source that opens with a brace group needs a statement or comment before it), ansfdata frame fromrpic_sf()(its"geojson"attribute, the engine's own text, is used — not its rows, so ansfthat has lost rows since is an error and edited columns are not seen), or pic source — apic()object or a string — which is georeferenced withrpic_geojson(x, ...).- ...
arguments passed to
rpic_geojson()whenxis pic source.- before
where the geometry layers go:
NULL(on top),"labels"(under the basemap's labels) or a layer id; see Details.- text_scale
multiplies every label's font size (
pic-textandpic-title): a finite number in (0, 16]. The default, 1, keeps the labels true to the ground; 1.5 draws them half as large again, so they stay legible further out. Strokes and dots keep their ground sizes. The scale is applied inside the engine's sizing (LayerOptions, the engine's DECISIONS G11), so the uppertext-sizezoom stop still keeps labels within MapLibre's 255 px limit — the same output asrpic --geojson --maplibre --text-scale.- font
the labels' font:
"style"(the style's own fonts),"spec"(the engine's), a character vector of font names (one stack for every label) or a named list of stacks,list(regular =, bold =, italic =, bold_italic =); see Details.- fit
zoom the map to the drawing's bounding box.
Details
before decides where the drawing goes among the style's layers, by the
rule of the engine's JavaScript attach(map, fc, { before }): only the
geometry layers (pic-fill, pic-stroke, pic-stroke-dashed,
pic-dot) are inserted before the chosen layer; the drawing's own
labels (pic-text, pic-title) are always added on top, so a basemap
label never covers them.
NULL(the default): all six layers on top of the style."labels": under the basemap's labels — before the style's layer with the idlabelsif it has one, else at the start of its trailing label block: before the first layer that follows the last non-symbollayer (fill, line, ...), among the style's layers and those already added tomap(thepic-*layers aside). Not simply the firstsymbollayer: CARTO's positron putswaterway_labelbefore its roads, buildings and boundaries, and the drawing would go under all of them. When a geometry layer is the last one, there is no label block and the layers go on top. The style is read frommap$x$style: a list is inspected as is (layersas a list of layers or, fromjsonlite::fromJSON(), a data frame); a URL (mapgl::carto_style("positron"), ...) is fetched once per session (needsjsonlite; 10 s timeout). When the style cannot be read — offline, amapbox://URL,mapgl::mapboxgl()'s default style, a list withoutlayers, nojsonlite— and no layer added tomapanswers, a warning says so and the layers go on top (the added layers alone answer when they end insymbollayers).any other string: the id of a layer to insert before — of the style or one already added to
map. When the style can be read and has no such layer, a warning says so and the layers go on top (MapLibre would otherwise drop them). When it cannot be read, the id is used as given, with a warning that it could not be checked.
The source is the engine's GeoJSON text, handed to the widget verbatim.
(mapgl::add_source() takes only an sf object, which it re-serialises
with the sf column names — geopic_kind instead of the geopic:kind
the layers read — or a URL; so add_rpic() registers the source in the
widget itself, the way add_source() does.) A map holds one pic drawing:
the source id and the layer ids are fixed by the engine, so a map that
already has a source "pic" or one of the six layer ids is an error.
font sets the text-font of the drawing's labels (pic-text,
pic-title). The font decides where a label sits: MapLibre places the
text box by the glyphs' vertical metrics, and a glyph server's
"Noto Sans Regular" (CARTO's, for one) is not the one the engine's
anchors were measured with, so a label meant to sit above a line can
land on it. With "style" (the default) the labels use the style's own
font: the most common text-font stack among its symbol layers,
upright Regular/Medium stacks preferred over bold and italic ones
(CARTO's positron: Montserrat Medium, Open Sans Regular, ...). The
style is read as for before (a URL is fetched once per session); when
it cannot be read or has no such stack, the engine's font is kept,
without a warning of its own. "spec" keeps the engine's Noto Sans
placeholders; any other character vector is the font stack itself, for
every label (names the style's glyph server must serve).
A label written bold or italic in the pic source keeps its face on
the map: the features carry geopic:font_weight and geopic:font_style,
and the layers' text-font picks a stack from them. With "style" the
bold, italic and bold italic stacks are looked for among the stacks the
style's own symbol layers use — the same family as the body font,
Bold before SemiBold/DemiBold before heavier faces, an italic of
plain weight — because those are the faces its glyph server is known to
serve; a face the style does not use falls back to the body font (bold
italic: to the bold, then the italic). A named list
list(regular =, bold =, italic =, bold_italic =) of stacks names them
outright (regular required, the same fallbacks). mono and
font "..." labels are drawn in the body family: the family travels as
geopic:font_family, but basemap glyph servers rarely have more.
rpic_mapgl() is the same function under its first name (an alias kept
for code written against the development version).
See also
rpic_maplibre_layers(); vignette("maps"); the article
Interactive maps with mapgl.
Examples
if (requireNamespace("mapgl", quietly = TRUE)) {
src <- pic('geo_lon = -34.8711; geo_lat = -8.0631; geo_scale = 1000
circle rad 2 at (0, 0) fill 0.9
"Recife" at (0, 0)')
m <- mapgl::maplibre(style = mapgl::carto_style("positron")) |>
add_rpic(src)
vapply(m$x$layers, function(l) l$id, "")
# shapes under the basemap's labels, larger labels; a style given as a
# list is read without a download
style <- list(version = 8, sources = list(), layers = list(
list(id = "water", type = "fill"),
list(id = "place-names", type = "symbol")
))
m <- mapgl::maplibre(style = style) |>
add_rpic(src, before = "labels", text_scale = 1.5)
vapply(m$x$layers, function(l) if (is.null(l$before_id)) NA_character_ else l$before_id, "")
}
#> [1] "place-names" "place-names" "place-names" "place-names" NA
#> [6] NA