Skip to content

Commit

Permalink
feat: check tile providers in addProviderTiles() (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-davison authored Aug 7, 2024
1 parent 407d0f7 commit 9bf137b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

* Updated vignettes to replace `{sp}`/`{raster}` usage with `{sf}`/`{terra} and their corresponding examples. (@jack-davison, #928)

* `addProviderTiles()` will now error if the chosen `provider` does not match any currently loaded provider (by default, those in `providers`). This behaviour can be toggled off by setting the new `check` argument to `FALSE` (@jack-davison, #929)

# leaflet 2.2.2

* Fixed #893: Correctly call `terra::crs()` when checking the CRS of a `SpatVector` object in `pointData()` or `polygonData()` (thanks @mkoohafkan, #894).
Expand Down
21 changes: 17 additions & 4 deletions R/plugin-providers.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ leafletProviderDependencies <- function() {
#' <https://github.com/leaflet-extras/leaflet-providers>)
#' @param layerId the layer id to assign
#' @param group the name of the group the newly created layers should belong to
#' (for [clearGroup()] and [addLayersControl()] purposes).
#' Human-friendly group names are permitted--they need not be short,
#' identifier-style names.
#' (for [clearGroup()] and [addLayersControl()] purposes). Human-friendly
#' group names are permitted--they need not be short, identifier-style names.
#' @param options tile options
#' @param check Check that the specified `provider` matches the available
#' currently loaded leaflet providers? Defaults to `TRUE`, but can be toggled
#' to `FALSE` for advanced users.
#' @return modified map object
#'
#' @examples
Expand All @@ -36,8 +38,19 @@ addProviderTiles <- function(
provider,
layerId = NULL,
group = NULL,
options = providerTileOptions()
options = providerTileOptions(),
check = TRUE
) {
if (check) {
loaded_providers <- leaflet.providers::providers_loaded()
if (!provider %in% names(loaded_providers$providers)) {
stop(
"Unknown tile provider '",
provider,
"'; either use a known provider or pass `check = FALSE` to `addProviderTiles()`"
)
}
}
map$dependencies <- c(map$dependencies, leafletProviderDependencies())
invokeMethod(map, getMapData(map), "addProviderTiles",
provider, layerId, group, options)
Expand Down
12 changes: 8 additions & 4 deletions man/addProviderTiles.Rd

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

14 changes: 14 additions & 0 deletions tests/testthat/test-tiles.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

testthat::test_that("Checking of tile providers works correctly", {
expect_no_error(
leaflet() %>% addProviderTiles(providers[[1]])
)

expect_no_error(
leaflet() %>% addProviderTiles("FAKETILESET123", check = FALSE)
)

expect_error(
leaflet() %>% addProviderTiles("FAKETILESET123")
)
})

0 comments on commit 9bf137b

Please sign in to comment.