basemaps is a lightweight R package to download and cache spatial basemaps from open sources such as OpenStreetMap, Carto and Mapbox. Retrieved basemaps are translated into and returned as classes of choice, such as raster, stars, ggplot, mapview or magick. The package aims to ease the use of basemaps in different contexts by providing a function interface as minimalistic as possible.

Installation

basemaps is not on CRAN yet. The latest version can be installed from GitHub:

devtools::install_github("16EAGLE/basemaps")

Get started

All available map services and map types can be printed using get_maptypes(). The basemap() function and its class-specific aliases facilitate (down)loading a basemap and returning it as a class of choice. Map preferences that should be used during a session can be set as defaults using set_defaults().

library(basemaps)
data(ext)
# or use draw_ext() to interactively draw an extent yourself

# view all available maps
get_maptypes()

# set defaults for the basemap
set_defaults(map_service = "mapbox", map_type = "satellite",
             map_token = "YOUR_MAPTOKEN_IF_NEEDED")
# token only needed for mapbox maps, register for free at mapbox.com to get a token

# load and return basemap map as many different classes:
basemap_plot(ext)
#> Loading basemap 'satellite' from map service 'mapbox'...

basemap_mapview(ext)
#> Loading basemap 'satellite' from map service 'mapbox'...

basemap_ggplot(ext)
#> Loading basemap 'satellite' from map service 'mapbox'...

basemap_magick(ext)
#> Loading basemap 'satellite' from map service 'mapbox'...

basemap_raster(ext)
#> Loading basemap 'satellite' from map service 'mapbox'...
#> class      : RasterBrick 
#> dimensions : 582, 623, 362586, 3  (nrow, ncol, ncell, nlayers)
#> resolution : 19.10926, 19.10926  (x, y)
#> extent     : 1225763, 1237668, 6034379, 6045500  (xmin, xmax, ymin, ymax)
#> crs        : +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +no_defs 
#> source     : /tmp/RtmpjCVUpd/basemaps/basemap_20200528212540.tif 
#> names      : val1, val2, val3 
#> min values :   23,   16,   14 
#> max values :  255,  255,  255

basemap_stars(ext)
#> Loading basemap 'satellite' from map service 'mapbox'...
#> stars object with 3 dimensions and 1 attribute
#> attribute(s), summary of first 1e+05 cells:
#>      val1        
#>  Min.   : 23.00  
#>  1st Qu.: 64.00  
#>  Median : 77.00  
#>  Mean   : 84.12  
#>  3rd Qu.:104.00  
#>  Max.   :255.00  
#> dimension(s):
#>      from  to  offset    delta                       refsys point        values
#> x       1 623 1225763  19.1093 +proj=merc +a=6378137 +b=...    NA          NULL
#> y       1 582 6045500 -19.1093 +proj=merc +a=6378137 +b=...    NA          NULL
#> band    1   3      NA       NA                           NA    NA val1,...,val3
#>         
#> x    [x]
#> y    [y]
#> band

basemap_png(ext)
#> Loading basemap 'satellite' from map service 'mapbox'...
#> [1] "/tmp/RtmpjCVUpd/basemaps//mapbox_satellite_2020-05-28_21-25-47.png"

Map examples

basemaps supports a variety of map services and types (which can be printed using get_maptypes()). A selection of available map types is shown below:

basemap_magick(ext, map_service = "osm", map_type = "topographic")
#> Loading basemap 'topographic' from map service 'osm'...

basemap_magick(ext, map_service = "osm", map_type = "toner")
#> Loading basemap 'toner' from map service 'osm'...

basemap_magick(ext, map_service = "osm", map_type = "hike")
#> Loading basemap 'hike' from map service 'osm'...

basemap_magick(ext, map_service = "carto", map_type = "dark")
#> Loading basemap 'dark' from map service 'carto'...

basemap_magick(ext, map_service = "mapbox", map_type = "hybrid")
#> Loading basemap 'hybrid' from map service 'mapbox'...

basemap_magick(ext, map_service = "mapbox", map_type = "streets")
#> Loading basemap 'streets' from map service 'mapbox'...

basemap_magick(ext, map_service = "mapbox", map_type = "hike")
#> Loading basemap 'hike' from map service 'mapbox'...

basemap_magick(ext, map_service = "mapbox", map_type = "terrain")
#> Loading basemap 'terrain' from map service 'mapbox'...

basemap_magick(ext, map_service = "mapbox", map_type = "comic")
#> Loading basemap 'comic' from map service 'mapbox'...

basemap_magick(ext, map_service = "mapbox", map_type = "pirates")
#> Loading basemap 'pirates' from map service 'mapbox'...

Created on 2020-05-28 by the reprex package (v0.3.0)

Available functions