get_static_tiles.Rd
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.
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
)
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)
.
The zoom level for which you'd like to return tiles.
A Mapbox style ID; retrieve yours from your Mapbox account.
A Mapbox username.
A Mapbox style URL.
The scaling factor to use; one of "1x"
or "2x"
.
The distance to buffer around an input sf
object for determining tile extent, specified in units. Defaults to 5000.
Units of buffer_dist
; defaults to "m" (meters). If buffer_dist
is a units class object, the units argument is ignored.
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.
A Mapbox access token. Supply yours here or set globally with the mb_access_token()
function.
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.
if (FALSE) {
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")
}