Skip to content

Commit

Permalink
adding Stadia Maps support
Browse files Browse the repository at this point in the history
  • Loading branch information
16EAGLE committed Mar 3, 2024
1 parent 9804754 commit 9880888
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 35 deletions.
5 changes: 3 additions & 2 deletions R/basemap.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' @param ext extent to be covered by the basemap as any spatial class supported by \code{st_bbox}.
#' @param map_service character, a map service, either \code{"osm"}, \code{"carto"} or \code{"mapbox"}. Default is \code{"osm"}.
#' @param map_type character, a map type, e.g. \code{"streets"}. For a full list of available map types, see \code{\link{get_maptypes}}.
#' @param map_token character, authentication token for services that require registration, which are \code{"osm_stamen"}, \code{"osm_thunderforest"} and \code{"mapbox"}. Register at \url{https://stadiamaps.com/} for stamen, \url{https://www.thunderforest.com/} and/or \url{https://www.mapbox.com/} to get tokens. Ignored for all other map services.
#' @param map_token character, authentication token for services that require registration, which are \code{"osm_stamen"}, \code{"osm_stadia"}, \code{"osm_thunderforest"} and \code{"mapbox"}. Register at \url{https://stadiamaps.com/} (for stamen and stadia), \url{https://www.thunderforest.com/} and/or \url{https://www.mapbox.com/} to get tokens. Ignored for all other map services.
#' @param map_res numeric, resolution of base map in range from 0 to 1.
#' @param 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.
#' @param class character, output class, either either \code{plot} (default), \code{magick}, \code{png}, \code{geotif} or if suggested packages are installed, \code{terra}, \code{raster}, \code{stars}, \code{ggplot}, \code{gglayer} or \code{mapview}.
Expand Down Expand Up @@ -38,7 +38,7 @@
#'
#' # set defaults for the basemap
#' set_defaults(map_service = "osm", map_type = "terrain_bg")
#' # for osm_stamen, osm thunderforest and mapbox maps, you need a API token.
#' # for "osm_stamen", "osm_stadia", osm "thunderforest" and "mapbox" maps, you need a API token.
#' # Register for free at stadiamaps.com, thunderforest.com and mapbox.com to get tokens.
#'
#' \dontrun{
Expand Down Expand Up @@ -106,6 +106,7 @@ basemap <- function(ext = NULL, map_service = NULL, map_type = NULL, map_res = N
if(map_service == "mapbox" & is.na(map_token)) out("You need to define 'map_token' to use map_service 'mapbox'. Register at https://www.mapbox.com/ to obtain a token.", type = 3)
if(map_service == "osm_thunderforest" & is.na(map_token)) out("You need to define 'map_token' to use map_service 'osm_thunderforest'. Register at https://thunderforest.com to obtain a token.", type = 3)
if(map_service == "osm_stamen" & is.na(map_token)) out("You need to define 'map_token' to use map_service 'osm_stamen'. Register at https://stadiamaps.com to obtain a token.", type = 3)
if(map_service == "osm_stadia" & is.na(map_token)) out("You need to define 'map_token' to use map_service 'osm_stadia'. Register at https://stadiamaps.com to obtain a token.", type = 3)

extras <- list(...)
if(!is.null(extras$browse)) browse <- extras$browse else browse <- TRUE
Expand Down
4 changes: 3 additions & 1 deletion R/get_maptypes.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' This function returns every supported map type that can be used as input to the \code{map_type} argument of \code{\link{set_defaults}}, \code{\link{basemap}} or associated functions.
#'
#' @param map_service character, optional, either \code{"osm"}, \code{"osm_stamen"}, \code{"osm_thunderforest"}, \code{"carto"}, \code{"mapbox"} or \code{"esri"}. Otherwise, a list of map types for both services is returned.
#' @param map_service character, optional, either \code{"osm"}, \code{"osm_stamen"}, \code{"osm_stadia"}, \code{"osm_thunderforest"}, \code{"carto"}, \code{"mapbox"} or \code{"esri"}. Otherwise, a list of map types for both services is returned.
#' @return A character vector of supported map types
#'
#'
Expand All @@ -12,6 +12,8 @@
#'
#' \code{"osm_stamen"}: Stamen (\url{https://maps.stamen.com/}) via Stadia Maps (\url{https://stadiamaps.com/}), Open Street Map contributors (\url{https://www.openstreetmap.org/copyright})
#'
#' \code{"osm_stadia"}: Stadia Maps (\url{https://stadiamaps.com/}), Open Street Map contributors (\url{https://www.openstreetmap.org/copyright})
#'
#' \code{"osm_thunderforest"}: Thunderforest (\url{https://www.thunderforest.com/}), Open Street Map contributors (\url{https://www.openstreetmap.org/copyright})
#'
#' \code{"carto"}: Carto (\url{https://carto.com/})
Expand Down
23 changes: 19 additions & 4 deletions R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
if(any(map_service != "mapbox", all(map_service == "mapbox", map_type == "terrain"))) ".png", # file suffix or not
if(map_service == "mapbox") paste0("?access_token=", map_token), # token or not
if(map_service == "osm_thunderforest") paste0("?apikey=", map_token), # token or not
if(map_service == "osm_stamen") paste0("?api_key=", map_token) # token or not
if(map_service == "osm_stamen") paste0("?api_key=", map_token), # token or not
if(map_service == "osm_stadia") paste0("?api_key=", map_token) # token or not
)

if(isTRUE(http_error(url))){
resp <- GET(url)
status <- resp$status_code
if(any(status == 401 & map_service == "mapbox", status == 401 & map_service == "osm_stamen")) out("Authentification failed. Is your map_token correct?", type = 3)
if(any(status == 401 & map_service == "mapbox", status == 401 & map_service == "osm_stamen", status == 401 & map_service == "osm_stadia")) out("Authentification failed. Is your map_token correct?", type = 3)
if(status == 403 & map_service == "osm_thunderforest") out("Authentification failed. Is your map_token correct?", type = 3)
}
if(!file.exists(file)){
Expand Down Expand Up @@ -399,7 +400,14 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
.md_maptypes_table <- function(maptypes){
x <- paste0(lapply(names(maptypes), function(service){
paste0(unlist(lapply(maptypes[[service]], function(x, s = service){
paste0("| `", s, "` | `", x, "` | ", if(any(grepl("mapbox", s), grepl("osm_thunderforest", s), grepl("osm_stamen", s))) "yes" else "no", " |")
token <- if(grepl("mapbox", s)){
"yes, register: https://mapbox.com"
} else if(grepl("osm_thunderforest", s)){
"yes, register: https://www.thunderforest.com/"
} else if(any(grepl("osm_stamen", s), grepl("osm_stadia", s))){
"yes, register: https://stadiamaps.com/"
} else "no"
paste0("| `", s, "` | `", x, "` | ", token, " |")
})), collapse = "\n")
}), collapse = "\n")
cat(paste0("| `map_service` | `map_type` | `map_token` required? |\n | ------ | ------ | ------ |\n", x))
Expand Down Expand Up @@ -457,6 +465,12 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
terrain_bg = "https://tiles.stadiamaps.com/tiles/stamen_terrain_background/",
watercolor = "https://tiles.stadiamaps.com/tiles/stamen_watercolor/"
),
osm_stadia = c(
alidade_smooth = "https://tiles.stadiamaps.com/tiles/alidade_smooth/",
alidade_smooth_dark = "https://tiles.stadiamaps.com/tiles/alidade_smooth_dark/",
outdoors = "https://tiles.stadiamaps.com/tiles/outdoors/",
osm_bright = "https://tiles.stadiamaps.com/tiles/osm_bright/"
),
osm_thunderforest = list(
cycle = "https://tile.thunderforest.com/cycle/",
transport = "https://tile.thunderforest.com/transport/",
Expand Down Expand Up @@ -517,7 +531,8 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
world_reference_overlay = "https://services.arcgisonline.com/arcgis/rest/services/Reference/World_Reference_Overlay/MapServer/tile/",
world_transportation = "https://services.arcgisonline.com/arcgis/rest/services/Reference/World_Transportation/MapServer/tile/",
delorme_world_base_map = "https://services.arcgisonline.com/arcgis/rest/services/Specialty/DeLorme_World_Base_Map/MapServer/tile/",
world_navigation_charts = "https://services.arcgisonline.com/arcgis/rest/services/Specialty/World_Navigation_Charts/MapServer/tile/")
world_navigation_charts = "https://services.arcgisonline.com/arcgis/rest/services/Specialty/World_Navigation_Charts/MapServer/tile/"
)
))
if(!dir.exists(getOption("basemaps.defaults")$map_dir)) dir.create(getOption("basemaps.defaults")$map_dir)

Expand Down
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![Coverage](https://codecov.io/gh/16eagle/basemaps/branch/master/graph/badge.svg)](https://app.codecov.io/gh/16EAGLE/basemaps)
[![Package dependencies](https://tinyverse.netlify.com/badge/basemaps)](https://CRAN.R-project.org/package=basemaps)

`basemaps` is a lightweight `R` package to download and cache spatial basemaps from open sources such as *OpenStreetMap*, *Stamen/Stadia Maps*, *Thunderforest*, *Carto*, *Mapbox* and others. Retrieved basemaps are translated into and returned as classes of choice, such as `raster`, `stars`, `terra`, `ggplot`, `mapview`, `magick`, or as files, such as `png` or `geotif`. The package aims to ease the use of basemaps in different contexts by providing a function interface as minimalist as possible.
`basemaps` is a lightweight `R` package to download and cache spatial basemaps from open sources such as *OpenStreetMap*, *Stamen*, *Stadia*, *Thunderforest*, *Carto*, *Mapbox*, *Esri* and others. Retrieved basemaps are translated into and returned as classes of choice, such as `terra`, `stars`, `raster`, `ggplot`, `mapview`, `magick`, or as files, such as `png` or `geotif`. The package aims to ease the use of basemaps in different contexts by providing a function interface as minimalist as possible.


## Installation
Expand Down Expand Up @@ -203,21 +203,25 @@ This table lists all currently implemented map services and map types and indica
| `osm` | `streets` | no |
| `osm` | `streets_de` | no |
| `osm` | `topographic` | no |
| `osm_stamen` | `toner` | yes |
| `osm_stamen` | `toner_bg` | yes |
| `osm_stamen` | `terrain` | yes |
| `osm_stamen` | `terrain_bg` | yes |
| `osm_stamen` | `watercolor` | yes |
| `osm_thunderforest` | `cycle` | yes |
| `osm_thunderforest` | `transport` | yes |
| `osm_thunderforest` | `landscape` | yes |
| `osm_thunderforest` | `outdoors` | yes |
| `osm_thunderforest` | `transport_dark` | yes |
| `osm_thunderforest` | `spinal` | yes |
| `osm_thunderforest` | `pioneer` | yes |
| `osm_thunderforest` | `mobile_atlas` | yes |
| `osm_thunderforest` | `neighbourhood` | yes |
| `osm_thunderforest` | `atlas` | yes |
| `osm_stamen` | `toner` | yes, register: https://stadiamaps.com/ |
| `osm_stamen` | `toner_bg` | yes, register: https://stadiamaps.com/ |
| `osm_stamen` | `terrain` | yes, register: https://stadiamaps.com/ |
| `osm_stamen` | `terrain_bg` | yes, register: https://stadiamaps.com/ |
| `osm_stamen` | `watercolor` | yes, register: https://stadiamaps.com/ |
| `osm_stadia` | `alidade_smooth` | yes, register: https://stadiamaps.com/ |
| `osm_stadia` | `alidade_smooth_dark` | yes, register: https://stadiamaps.com/ |
| `osm_stadia` | `outdoors` | yes, register: https://stadiamaps.com/ |
| `osm_stadia` | `osm_bright` | yes, register: https://stadiamaps.com/ |
| `osm_thunderforest` | `cycle` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `transport` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `landscape` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `outdoors` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `transport_dark` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `spinal` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `pioneer` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `mobile_atlas` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `neighbourhood` | yes, register: https://www.thunderforest.com/ |
| `osm_thunderforest` | `atlas` | yes, register: https://www.thunderforest.com/ |
| `carto` | `light` | no |
| `carto` | `light_no_labels` | no |
| `carto` | `light_only_labels` | no |
Expand All @@ -228,13 +232,13 @@ This table lists all currently implemented map services and map types and indica
| `carto` | `voyager_no_labels` | no |
| `carto` | `voyager_only_labels` | no |
| `carto` | `voyager_labels_under` | no |
| `mapbox` | `streets` | yes |
| `mapbox` | `outdoors` | yes |
| `mapbox` | `light` | yes |
| `mapbox` | `dark` | yes |
| `mapbox` | `satellite` | yes |
| `mapbox` | `hybrid` | yes |
| `mapbox` | `terrain` | yes |
| `mapbox` | `streets` | yes, register: https://mapbox.com |
| `mapbox` | `outdoors` | yes, register: https://mapbox.com |
| `mapbox` | `light` | yes, register: https://mapbox.com |
| `mapbox` | `dark` | yes, register: https://mapbox.com |
| `mapbox` | `satellite` | yes, register: https://mapbox.com |
| `mapbox` | `hybrid` | yes, register: https://mapbox.com |
| `mapbox` | `terrain` | yes, register: https://mapbox.com |
| `esri` | `natgeo_world_map` | no |
| `esri` | `usa_topo_maps` | no |
| `esri` | `world_imagery` | no |
Expand Down
4 changes: 2 additions & 2 deletions man/basemap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/defaults.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/get_maptypes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9880888

Please sign in to comment.