Title: | Accessing Spatial Basemaps in R |
---|---|
Description: | A lightweight package to access spatial basemaps from open sources such as 'OpenStreetMap', 'Carto', 'Mapbox' and others in R. |
Authors: | Jakob Schwalb-Willmann [aut, cre]
|
Maintainer: | Jakob Schwalb-Willmann <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0 |
Built: | 2025-01-21 05:58:19 UTC |
Source: | https://github.com/16eagle/basemaps |
These functions (down)load and cache a basemap of a defined extent ext
, map_service
and map_type
and return it as an object of the defined class. Alternatively to defining the following arguments, set_defaults
can be used to define basemap preferences once for the running session.
basemap( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, class = "plot", force = FALSE, ..., verbose = TRUE ) basemap_plot( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_magick( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_png( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_geotif( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_terra( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_raster( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_stars( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_ggplot( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_gglayer( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_mapview( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE )
basemap( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, class = "plot", force = FALSE, ..., verbose = TRUE ) basemap_plot( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_magick( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_png( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_geotif( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_terra( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_raster( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_stars( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_ggplot( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_gglayer( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE ) basemap_mapview( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL, force = NULL, ..., verbose = TRUE )
ext |
extent to be covered by the basemap as any spatial class supported by |
map_service |
character, a map service, either |
map_type |
character, a map type, e.g. |
map_res |
numeric, resolution of base map in range from 0 to 1. |
map_token |
character, authentication token for services that require registration, which are |
map_dir |
character, cache directory where downloaded basemap tiles will be stored. By default, a temporary directory is used, which is destroyed when the session is terminated. |
class |
character, output class, either either |
force |
logical, whether to force download over cached files or not. Default is |
... |
additional arguments, including
|
verbose |
logical, if |
A basemap of the defined class in Web/Pseudo Mercator Projection (EPSG: 3857)
See get_maptypes for available map services and their sources.
The use of the map services "osm_thunderforest"
and "mapbox"
require registration to obtain an API token/key which can be supplied to map_token
. Register at https://www.thunderforest.com/ and/or https://www.mapbox.com/ to get a token.
library(basemaps) # example extent data(ext) # view all available maps get_maptypes() # set defaults for the basemap set_defaults(map_service = "osm", map_type = "terrain_bg") # for "osm_stamen", "osm_stadia", osm "thunderforest", "maptiler" and "mapbox" maps, # you need a API token. Register for free at stadiamaps.com, thunderforest.com, # maptiler.com and mapbox.com to get tokens. ## Not run: # load and plot basemap (default) map <- basemap(ext) # or explicitely as different classes such as: basemap_magick(ext) basemap_raster(ext) basemap_stars(ext) # or as files: basemap_geotif(ext) basemap_png(ext) # or as plots: basemap_plot(ext) basemap_mapview(ext) # including ggplot2: basemap_ggplot(ext) # or as ggplot2 layer: library(ggplot2) ggplot() + basemap_gglayer(ext) + scale_fill_identity() + coord_sf() # or, when combined with an sf vector object, # make sure to use Web/Pseudo Mercator (EPSG 3857), as this is # the CRS in which all basemaps are returned (see "Value"): library(sf) ext <- st_transform(ext, crs = st_crs(3857)) ggplot() + basemap_gglayer(ext) + geom_sf(data = ext, color = "red", fill = "transparent") + coord_sf() + scale_fill_identity() ## End(Not run)
library(basemaps) # example extent data(ext) # view all available maps get_maptypes() # set defaults for the basemap set_defaults(map_service = "osm", map_type = "terrain_bg") # for "osm_stamen", "osm_stadia", osm "thunderforest", "maptiler" and "mapbox" maps, # you need a API token. Register for free at stadiamaps.com, thunderforest.com, # maptiler.com and mapbox.com to get tokens. ## Not run: # load and plot basemap (default) map <- basemap(ext) # or explicitely as different classes such as: basemap_magick(ext) basemap_raster(ext) basemap_stars(ext) # or as files: basemap_geotif(ext) basemap_png(ext) # or as plots: basemap_plot(ext) basemap_mapview(ext) # including ggplot2: basemap_ggplot(ext) # or as ggplot2 layer: library(ggplot2) ggplot() + basemap_gglayer(ext) + scale_fill_identity() + coord_sf() # or, when combined with an sf vector object, # make sure to use Web/Pseudo Mercator (EPSG 3857), as this is # the CRS in which all basemaps are returned (see "Value"): library(sf) ext <- st_transform(ext, crs = st_crs(3857)) ggplot() + basemap_gglayer(ext) + geom_sf(data = ext, color = "red", fill = "transparent") + coord_sf() + scale_fill_identity() ## End(Not run)
basemaps
cacheThis function flushes the basemaps
cache and thereby removes all previously queried and/or composited products from the map directories (temporary or user-defined using the argument map_dir
) used during the current session.
flush_cache()
flush_cache()
None.
library(basemaps) flush_cache()
library(basemaps) flush_cache()
The example datasets contain the sf
objects ext
and ext_eur
that can be used to call basemap
and the associated functions.
data(ext) data(ext_eur)
data(ext) data(ext_eur)
sf
object
An object of class sf
(inherits from data.frame
) with 1 rows and 3 columns.
basemaps
defaultsThese functions set, get or reset the defaults of all map arguments passed to basemap
and associated functions.
set_defaults( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL ) get_defaults() reset_defaults()
set_defaults( ext = NULL, map_service = NULL, map_type = NULL, map_res = NULL, map_token = NULL, map_dir = NULL ) get_defaults() reset_defaults()
ext |
extent to be covered by the basemap as any spatial class supported by |
map_service |
character, a map service, either |
map_type |
character, a map type, e.g. |
map_res |
numeric, resolution of base map in range from 0 to 1. |
map_token |
character, authentication token for services that require registration, which are |
map_dir |
character, cache directory where downloaded basemap tiles will be stored. By default, a temporary directory is used, which is destroyed when the session is terminated. |
For get_defaults
, a list of defaults, otherwise none.
library(basemaps) data(ext) # set defaults for the basemap set_defaults(ext = ext, map_service = "osm", map_type = "terrain_bg") # get defaults get_defaults() ## Not run: # load and return basemap map as raster (default) map <- basemap() ## End(Not run) # reset defaults reset_defaults()
library(basemaps) data(ext) # set defaults for the basemap set_defaults(ext = ext, map_service = "osm", map_type = "terrain_bg") # get defaults get_defaults() ## Not run: # load and return basemap map as raster (default) map <- basemap() ## End(Not run) # reset defaults reset_defaults()
This function lets you draw an extent on an interactive map. It is a simple wrapper around mapedit::drawFeatures()
written by Tim Appelhans et al.
draw_ext()
draw_ext()
An sf
object
## Not run: library(basemaps) # draw extent interactively ext <- draw_ext() # set defaults for the basemap set_defaults(ext = ext, map_service = "osm", map_type = "terrain_bg") # for mapbox maps, you need a map_token. Register for free at mapbox.com to get a token # load and return basemap map as raster (default) map <- basemap() ## End(Not run)
## Not run: library(basemaps) # draw extent interactively ext <- draw_ext() # set defaults for the basemap set_defaults(ext = ext, map_service = "osm", map_type = "terrain_bg") # for mapbox maps, you need a map_token. Register for free at mapbox.com to get a token # load and return basemap map as raster (default) map <- basemap() ## End(Not run)
These functions manage map types.
get_maptypes(map_service = NULL, as_df = FALSE, url_cols = FALSE) add_maptypes( map_service, map_type, url_endpoint, url_xy = "xy", url_file_format = ".png", url_map_token = as.character(NA), auth_error_code = 401, url_website = as.character(NA) ) save_maptypes(file) load_maptypes(file)
get_maptypes(map_service = NULL, as_df = FALSE, url_cols = FALSE) add_maptypes( map_service, map_type, url_endpoint, url_xy = "xy", url_file_format = ".png", url_map_token = as.character(NA), auth_error_code = 401, url_website = as.character(NA) ) save_maptypes(file) load_maptypes(file)
map_service |
character.
|
as_df |
logical, whether to return a data.frame instead of a list (defaults to |
url_cols |
logical, whether to include the endpoint URL and auxiliary URL columns (defults to |
map_type |
character, name of map type to add |
url_endpoint |
character, endpoint URL for map service and type to add |
url_xy |
character, either "xy" or "yx" defining the order the map service to add is expecting tile subscripts in (defaults to "xy") |
url_file_format |
character, file format the endpoint to add is using (defaults to ".png"). |
url_map_token |
character, optional, request query string used by the endpoint to add for transmitting authentification tokens if required (defaults to |
auth_error_code |
numeric, http error code the endpoint to add uses if authentification failed (defaults to 401) |
url_website |
character, optional, website URL for the service to add the user is directed to for registering (defaults to |
file |
character, file name ending on ".csv" that the map types table is saved to or loaded from. |
get_maptypes()
returns every supported map type that can be used as input to the map_type
argument of set_defaults
, basemap
or associated functions.
add_maptypes()
adds custom user-defined map types.
save_maptypes()
saves the current map types table (as printed by get_maptypes(as_df = T, url_cols = T)
) to a file.
load_maptypes()
loads a map types table from file.
A character vector of supported map types
"osm"
: Open Street Map contributors (https://www.openstreetmap.org/copyright), Open Topo Map (https://opentopomap.org/)
"osm_stamen"
: Stamen (https://maps.stamen.com/) via Stadia Maps (https://stadiamaps.com/), Open Street Map contributors (https://www.openstreetmap.org/copyright)
"osm_stadia"
: Stadia Maps (https://stadiamaps.com/), Open Street Map contributors (https://www.openstreetmap.org/copyright)
"osm_thunderforest"
: Thunderforest (https://www.thunderforest.com/), Open Street Map contributors (https://www.openstreetmap.org/copyright)
"carto"
: Carto (https://carto.com/)
"mapbox"
: Mapbox (https://www.mapbox.com)
"esri"
: Esri (https://www.esri.com/en-us/home)
"maptiler"
: Esri (https://www.maptiler.com)
# for all services, as list (default:) get_maptypes() # for osm only get_maptypes("osm") # or get_maptypes()$osm # for mapbox only get_maptypes("mapbox") # or get_maptypes()$mapbox # same for all other map services # or as data.frame: get_maptypes(as_df = TRUE) # or as data.frame including all url columns (endpoints): get_maptypes(as_df = TRUE, url_cols = TRUE) # add a custom map service and type yourself: add_maptypes( map_service = "someservice", map_type = "terrain", url_endpoint = "https://tile.someservice.org") # control further aspects of a custom map service and type: add_maptypes( map_service = "someservice", map_type = "terrain", url_endpoint = "https://tile.someservice.org", url_xy = "xy", # order in which this service expects tile x and y id url_file_format = ".png", url_map_token = "?authtoken=", # query params for auth token auth_error_code = 401, url_website = "https://someservice.org" )
# for all services, as list (default:) get_maptypes() # for osm only get_maptypes("osm") # or get_maptypes()$osm # for mapbox only get_maptypes("mapbox") # or get_maptypes()$mapbox # same for all other map services # or as data.frame: get_maptypes(as_df = TRUE) # or as data.frame including all url columns (endpoints): get_maptypes(as_df = TRUE, url_cols = TRUE) # add a custom map service and type yourself: add_maptypes( map_service = "someservice", map_type = "terrain", url_endpoint = "https://tile.someservice.org") # control further aspects of a custom map service and type: add_maptypes( map_service = "someservice", map_type = "terrain", url_endpoint = "https://tile.someservice.org", url_xy = "xy", # order in which this service expects tile x and y id url_file_format = ".png", url_map_token = "?authtoken=", # query params for auth token auth_error_code = 401, url_website = "https://someservice.org" )
ggplot
This function plots objects of class SpatRaster
, RasterLayer
, RasterBrick
or RasterStack
as ggplot2
. It is used internally by basemap*
functions that return ggplot
plots.
gg_raster(r, r_type = "RGB", gglayer = F, ...)
gg_raster(r, r_type = "RGB", gglayer = F, ...)
r |
raster of class |
r_type |
character, either |
gglayer |
logical, if |
... |
additional arguments, including
|
A ggplot2
object
library(basemaps) # example extent data(ext) ## Not run: # terra raster object map <- basemap_terra(ext) # plotting raster as ggplot using the with fill aesthetic gg_raster(map, r_type = "RGB") # or as gg layer using the with fill aesthetic ggplot() + gg_raster(map, r_type = "RGB", gglayer = T) + scale_fill_identity() ## End(Not run)
library(basemaps) # example extent data(ext) ## Not run: # terra raster object map <- basemap_terra(ext) # plotting raster as ggplot using the with fill aesthetic gg_raster(map, r_type = "RGB") # or as gg layer using the with fill aesthetic ggplot() + gg_raster(map, r_type = "RGB", gglayer = T) + scale_fill_identity() ## End(Not run)