Skip to content

Commit

Permalink
feat: add support for apikey stored in env variable (for Stadia et Th…
Browse files Browse the repository at this point in the history
…underforest providers)
  • Loading branch information
rCarto committed Jan 11, 2024
1 parent 3a0e78c commit fc39867
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 26 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Imports:
png,
terra,
tools,
slippymath
slippymath,
utils
Suggests:
covr,
tinytest
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ importFrom(terra,project)
importFrom(terra,rast)
importFrom(terra,writeRaster)
importFrom(tools,file_path_sans_ext)
importFrom(utils,globalVariables)
28 changes: 17 additions & 11 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ get_param <- function(provider) {
param <- provider
} else {
stamen_provider <- c(
"Stamen.Toner", "Stamen.TonerBackground",
"Stamen.TonerHybrid", "Stamen.TonerLines",
"Stamen.TonerLabels", "Stamen.TonerLite",
"Stamen.Watercolor", "Stamen.Terrain",
"Stamen.TerrainBackground",
"Stamen.Toner", "Stamen.TonerBackground", "Stamen.TonerHybrid",
"Stamen.TonerLines", "Stamen.TonerLabels", "Stamen.TonerLite",
"Stamen.Watercolor", "Stamen.Terrain", "Stamen.TerrainBackground",
"Stamen.TerrainLabels"
)
if (provider %in% stamen_provider) {
Expand All @@ -114,11 +112,10 @@ get_param <- function(provider) {
call. = FALSE
)
}
param <- maptiles_providers[[provider]]
param <- .global_maptiles$providers[[provider]]
}
param$q <- gsub("XXXXXX", "{apikey}", param$q, perl = TRUE)
param$ext <- get_extension(param$q)
param
return(param)
}

# get zoom
Expand Down Expand Up @@ -174,13 +171,22 @@ get_cached_raster <- function(filename, forceDownload, verbose) {


# get the tiles according to the grid
download_tiles <- function(tile_grid, param, apikey, verbose,
cachedir, forceDownload) {
download_tiles <- function(tile_grid, param, apikey, verbose, cachedir,
forceDownload) {
images <- vector("list", length = nrow(tile_grid$tiles))
if (missing(apikey)) {apikey <- ""}
zoom <- tile_grid$zoom
ext <- param$ext
src <- param$src
if (missing(apikey)) {
apikey <- ""
if (startsWith(src, "Stadia") && Sys.getenv("STADIA_MAPS") != "") {
apikey <- Sys.getenv("STADIA_MAPS")
}
if (startsWith(src, "Thunderforest") &&
Sys.getenv("THUNDERFOREST_MAPS") != "") {
apikey <- Sys.getenv("THUNDERFOREST_MAPS")
}
}
cpt <- 0
for (i in seq_along(images)) {
x <- tile_grid$tiles[i, ]
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.re
<!-- badges: end -->

To create maps from tiles, `maptiles` downloads, composes and displays tiles
from a large number of providers (e.g. OpenStreetMap, Stamen, Esri, CARTO, or
from a large number of providers (e.g. OpenStreetMap, Stadia, Esri, CARTO, or
Thunderforest).

## Installation
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.re
<!-- badges: end -->

To create maps from tiles, `maptiles` downloads, composes and displays
tiles from a large number of providers (e.g. OpenStreetMap, Stamen,
tiles from a large number of providers (e.g. OpenStreetMap, Stadia,
Esri, CARTO, or Thunderforest).

## Installation
Expand Down Expand Up @@ -88,7 +88,7 @@ nc_osmpos <- get_tiles(
)
#> Zoom: 7
#> Source(s): © OpenStreetMap contributors © CARTO
#> Cache directory: /tmp/Rtmpcn8iFm/CARTO.POSITRON
#> Cache directory: /tmp/Rtmpjmeafm/CARTO.POSITRON
#> 8 tiles
# display map
plot_tiles(nc_osmpos)
Expand Down
Binary file modified man/figures/README-example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 13 additions & 11 deletions man/get_tiles.Rd

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

0 comments on commit fc39867

Please sign in to comment.