Last active
February 5, 2025 19:01
-
-
Save tim-salabim/20f330a442f4861a1620711bc04bf41d to your computer and use it in GitHub Desktop.
add deckgl polygonLayer to a maplibre map using mapgl. Utilizes the blazing fast data transfer via arrow IPC stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_deckgl_polygons = function( | |
map | |
, data | |
, layerId | |
, geom_column_name = attr(data, "sf_column") | |
) { | |
path_layer = tempfile() | |
dir.create(path_layer) | |
path_layer = paste0(path_layer, "/", layerId, "_layer.arrow") | |
geom_col_name <- attr(data, "sf_column") | |
geom_type <- geoarrow::infer_geoarrow_schema(data, coord_type = "INTERLEAVED") | |
data_schema <- nanoarrow::infer_nanoarrow_schema(data) | |
data_schema$children[[geom_col_name]] <- geom_type | |
data_out = nanoarrow::as_nanoarrow_array_stream( | |
data | |
, schema = data_schema | |
) | |
nanoarrow::write_nanoarrow(data_out, path_layer) | |
source("https://raw.githubusercontent.com/r-spatial/leafgl/refs/heads/geoarrow-deckgl/R/deckgl-helpers.R") | |
# source("/home/tim/tappelhans/privat/rpckgdv/leafgl/R/deckgl-helpers.R") | |
deckglMapboxDependency = function() { | |
list( | |
htmltools::htmlDependency( | |
"deck.gl", | |
'9.0.14', | |
src = c( | |
href = "https://cdn.jsdelivr.net/npm/@deck.gl/[email protected]/dist/" | |
) | |
, script = "resolve-layers.min.js" | |
) | |
) | |
} | |
map$dependencies = c( | |
map$dependencies | |
, arrowDependencies() | |
, geoarrowjsDependencies() | |
, deckglDependencies() | |
, geoarrowDeckglLayersDependencies() | |
, deckglDataAttachmentSrc(path_layer, layerId) | |
, deckglMapboxDependency() | |
) | |
map = htmlwidgets::onRender( | |
map | |
, htmlwidgets::JS( | |
sprintf( | |
"function(el, x, data) { | |
debugger; | |
map = this.getMap(); | |
addDeckGlPolygons(map, '%s', '%s'); | |
addGlobeControl(map); | |
}" | |
, geom_column_name | |
, layerId | |
) | |
) | |
) | |
return(map) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment