Skip to contents

This function uses the Mapbox Static Maps API to return a pointer to an "magick-image" class image or a httr::response object from the static map image URL.

Usage

static_mapbox(
  location = NULL,
  buffer_dist = 1000,
  units = "m",
  style_id,
  username,
  style_url = NULL,
  overlay_sf = NULL,
  overlay_style = NULL,
  overlay_markers = NULL,
  longitude = NULL,
  latitude = NULL,
  zoom = NULL,
  width = NULL,
  height = NULL,
  bearing = NULL,
  pitch = NULL,
  scale = 0.5,
  scaling_factor = c("1x", "2x"),
  attribution = TRUE,
  logo = TRUE,
  before_layer = NULL,
  access_token = NULL,
  image = TRUE,
  strip = TRUE
)

Arguments

location

An input location for which you would like to request tiles. Can be a length-4 vector representing a bounding box, or an sf object. If an input sf object is supplied, use the buffer_dist argument to control how much area you want to capture around the layer. While the input sf object can be in an arbitrary coordinate reference system, if a length-4 bounding box vector is supplied instead it must represent WGS84 longitude/latitude coordinates and be in the order c(xmin, ymin, xmax, ymax).

buffer_dist

The distance to buffer around an input sf object for determining static map, specified in units. If location is a POINT object of 2 rows or less and buffer_dist is 0 or NULL, a 1 unit buffer is applied to try to ensure the creation of a valid bounding box for the map area.

units

Units of buffer_dist; defaults to "m" (meters). If buffer_dist is a units class object, the units argument is ignored.

style_id

A style ID (required if style_url is NULL).

username

A Mapbox username (required if style_url = NULL).

style_url

A Mapbox style url; defaults to NULL.

overlay_sf

The overlay sf object (optional). The function will convert the sf object to GeoJSON then plot over the basemap style. Spatial data that are too large will trigger an error, and should be added to the style in Mapbox Studio instead.

overlay_style

A named list of vectors specifying how to style the sf overlay. Possible names are "stroke", "stroke-width" (or "stroke_width"), "stroke-opacity" (or "stroke_opacity"), "fill", and "fill-opacity" (or "fill_opacity"). The fill and stroke color values can be specified as six-digit hex codes or color names, and the opacity and width values should be supplied as floating-point numbers. If overlay_style is NULL, the style values can be pulled from columns with the same names in overlay_sf.

overlay_markers

The prepared overlay markers (optional). See the function prep_overlay_markers for more information on how to specify a marker overlay.

longitude, latitude

The longitude and latitude of the map center. If an overlay is supplied, the map will default to the extent of the overlay unless longitude, latitude, and zoom are all specified.

zoom

The map zoom. The map will infer this from the overlay unless longitude, latitude, and zoom are all specified.

width, height

The map width and height; defaults to NULL

pitch, bearing

The map pitch and bearing; defaults to NULL. pitch can range from 0 to 60, and bearing from -360 to 360.

scale

ratio to scale the output image; scale = 1 will return the largest possible image. defaults to 0.5

scaling_factor

The scaling factor of the tiles; either "1x" (the default) or "2x"

attribution

Controls whether there is attribution on the image. Defaults to TRUE. If FALSE, the watermarked attribution is removed from the image. You still have a legal responsibility to attribute maps that use OpenStreetMap data, which includes most maps from Mapbox. If you specify attribution = FALSE, you are legally required to include proper attribution elsewhere on the webpage or document.

Controls whether there is a Mapbox logo on the image. Defaults to TRUE.

before_layer

A character string that specifies where in the hierarchy of layer elements the overlay should be inserted. The overlay will be placed just above the specified layer in the given Mapbox styles. List layer ids for a map style with get_style(style_id = style_id, username = username, style_url = style_url, access_token = access_token)[["layers"]][["id"]]

access_token

A Mapbox access token; which can be set with mb_access_token.

image

If FALSE, return the a httr::response object from httr::GET using the static image URL; defaults to TRUE.

strip

If TRUE, drop image comments and metadata when image = TRUE; defaults to TRUE.

Value

A pointer to an image of class "magick-image" if image = TRUE. The resulting image can be manipulated further with functions from the magick package.

Examples

if (FALSE) { # \dontrun{

library(mapboxapi)

points_of_interest <- tibble::tibble(
  longitude = c(-73.99405, -74.00616, -73.99577, -74.00761),
  latitude = c(40.72033, 40.72182, 40.71590, 40.71428)
)

prepped_pois <- prep_overlay_markers(
  data = points_of_interest,
  marker_type = "pin-l",
  label = 1:4,
  color = "fff"
)

map <- static_mapbox(
  style_id = "streets-v11",
  username = "mapbox",
  overlay_markers = prepped_pois,
  width = 1200,
  height = 800
)

map
} # }