get_static_tiles: Get static tiles from a Mapbox style for use as a basemap

get_static_tilesR Documentation

Get static tiles from a Mapbox style for use as a basemap

Description

This function queries the Mapbox Static Tiles API and composites the tiles as a raster suitable for use as a basemap in tmap or ggplot2 (with the ggspatial::layer_spatial() function. It returns a raster layer that corresponds either to an input bounding box or a buffered area around an input shape.

Usage

get_static_tiles(
  location,
  zoom,
  style_id,
  username,
  style_url = NULL,
  scaling_factor = c("1x", "2x"),
  buffer_dist = 5000,
  units = "m",
  crop = TRUE,
  access_token = NULL
)

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

zoom

The zoom level for which you'd like to return tiles.

style_id

A Mapbox style ID; retrieve yours from your Mapbox account.

username

A Mapbox username.

style_url

A Mapbox style URL.

scaling_factor

The scaling factor to use; one of "1x" or "2x".

buffer_dist

The distance to buffer around an input sf object for determining tile extent, specified in units. Defaults to 5000.

units

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

crop

Whether or not to crop the result to the specified bounding box or buffer area. Defaults to TRUE; FALSE will return the extent of the overlapping tiles.

access_token

A Mapbox access token. Supply yours here or set globally with the mb_access_token() function.

Value

A raster layer of tiles from the requested Mapbox style representing the area around the input location. The raster layer is projected in the Web Mercator coordinate reference system.

Examples

## Not run: 

library(mapboxapi)
library(tigris)
library(tmap)
library(ggspatial)
library(ggplot2)

ny_tracts <- tracts("NY", "New York", cb = TRUE)

ny_tiles <- get_static_tiles(
  location = ny_tracts,
  zoom = 10,
  style_id = "light-v9",
  username = "mapbox"
)

# tmap usage:
tm_shape(ny_tiles) +
  tm_rgb() +
  tm_shape(ny_tracts) +
  tm_polygons(alpha = 0.5, col = "navy") +
  tm_credits("Basemap (c) Mapbox, (c) OpenStreetMap",
    position = c("RIGHT", "BOTTOM")
  )

# ggplot2 usage:
ggplot() +
  layer_spatial(ny_tiles) +
  geom_sf(data = ny_tracts, fill = "navy", alpha = 0.5) +
  theme_void() +
  labs(caption = "Basemap (c) Mapbox, (c) OpenStreetMap")

## End(Not run)


mapboxapi documentation built on Oct. 17, 2023, 1:10 a.m.