Skip to content

Commit

Permalink
Add support for deck.gl
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Oct 4, 2024
1 parent be2251a commit e3d219b
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 5 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(Marker)
export(MarkerOptions)
export(add_call)
export(add_control)
export(add_deck_layers)
export(add_layer)
export(add_marker)
export(add_popup)
Expand Down
10 changes: 10 additions & 0 deletions R/deck-gl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' Add `deck.gl` layers to the map
#' @inherit add_control params return
#' @param layers A list of `deck.gl` layers.
#' @param tooltip Either a single mustache template string applied to all layers
#' or a dictionary where keys are layer ids and values are mustache template strings.
#' @example examples/deck-gl.R
#' @export
add_deck_layers <- function(.map, layers, tooltip = NULL) {
add_call(.map, "addDeckOverlay", layers, tooltip)
}
21 changes: 21 additions & 0 deletions R/html-dependencies.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
DECK_GL_VERSION <- "9.0.16"

dependency_deck_gl <- htmltools::htmlDependency(
name = "deck.gl",
version = DECK_GL_VERSION,
src = list(
href = glue::glue("https://unpkg.com/deck.gl@{DECK_GL_VERSION}")
),
script = "dist.min.js",
all_files = FALSE
)

dependency_deck_gl_json <- htmltools::htmlDependency(
name = "deck.gl.json",
version = DECK_GL_VERSION,
src = list(
href = glue::glue("https://unpkg.com/@deck.gl/json@{DECK_GL_VERSION}")
),
script = "dist.min.js",
all_files = FALSE
)
16 changes: 13 additions & 3 deletions R/maplibre.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
#'
#'
#' @param map_options [mapOptions()]
#' @param deck Whether to add `deck.gl` HTML dependency.
#' @param width,height The width and the height of the widget.
#' @param elementId The unique ID of the widgets's HTML element.
#' @param element_id The unique ID of the widgets's HTML element.
#' @param ... Further map options.
#' @import htmlwidgets
#'
#' @export
#' @example examples/basemap.R
#'
maplibre <- function(map_options = mapOptions(), width = "100%", height = NULL, elementId = NULL, ...) {
maplibre <- function(map_options = mapOptions(),
deck = FALSE,
width = "100%", height = NULL, element_id = NULL, ...) {
if (inherits(map_options$bounds, "bbox")) {
map_options$bounds <- unname(map_options$bounds)
}
Expand All @@ -22,14 +25,21 @@ maplibre <- function(map_options = mapOptions(), width = "100%", height = NULL,
calls = list()
)

# Dependencies
dependencies <- list()
if (isTRUE(deck)) {
dependencies <- c(dependencies, list(dependency_deck_gl, dependency_deck_gl_json))
}

# create widget
htmlwidgets::createWidget(
name = "maplibre",
x,
width = width,
height = height,
package = "maplibre",
elementId = elementId
dependencies = dependencies,
elementId = element_id
)
}

Expand Down
22 changes: 22 additions & 0 deletions examples/deck-gl.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
deck_grid_layer <- list(
"@@type" = "GridLayer",
id = "GridLayer",
data = "https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/sf-bike-parking.json",
extruded = TRUE,
getPosition = "@@=COORDINATES",
getColorWeight = "@@=SPACES",
getElevationWeight = "@@=SPACES",
elevationScale = 4,
cellSize = 200,
pickable = TRUE
)

setup <- mapOptions(
center = list(-122.4, 37.74),
zoom = 12,
pitch = 40,
hash = TRUE
)

maplibre(setup, deck = TRUE) |>
add_deck_layers(list(deck_grid_layer))
46 changes: 46 additions & 0 deletions man/add_deck_layers.Rd

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

7 changes: 5 additions & 2 deletions man/maplibre.Rd

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

43 changes: 43 additions & 0 deletions vignettes/articles/deck-gl.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: "deck.gl integration"
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

```{r setup}
library(maplibre)
```

```{r}
deck_grid_layer <- list(
"@@type" = "GridLayer",
id = "GridLayer",
data = "https://raw.githubusercontent.com/visgl/deck.gl-data/master/website/sf-bike-parking.json",
extruded = TRUE,
getPosition = "@@=COORDINATES",
getColorWeight = "@@=SPACES",
getElevationWeight = "@@=SPACES",
elevationScale = 4,
cellSize = 200,
pickable = TRUE
)
setup <- mapOptions(
center = list(-122.4, 37.74),
zoom = 12,
pitch = 40,
hash = TRUE
)
maplibre(setup, deck = TRUE) |>
add_control("NavigationControl") |>
add_deck_layers(
list(deck_grid_layer),
tooltip = "Number of points: {{ count }}"
)
```

0 comments on commit e3d219b

Please sign in to comment.