-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapbox vector tiles #346
Comments
Hi @dcooley, I was wondering if there was a timeline for this feature showing up in a build? I'd love to kick the tires on it with some gigantic polygon datasets! |
The problem I am having with this is coming up with a clean way to pass in the colours / line widths / etc into the function. All the other layers that have a So this kind of fell of my radar because I couldn't come up with anything. I don't suppose you have any ideas? |
@dcooley I've been playing around with this, and it seems that the MVT layer is intended to use the external tile server specified in The only way for this to all work without externally hosted tiles would be to use something like https://github.com/felt/tippecanoe to generate local tiles, then spin up a local server, and insert the URL from that. But that's never going to be viable here. Short of that, the MVT layer is just a way of getting data from an external tile server and using that to constuct a UpdateImplementation in |
You can define your own library(mapdeck)
set_token(secret::get_secret("MAPBOX"))
js <- "function(el, x) {
const layerId = 'mvt-layer';
// reference: https://deck.gl/docs/api-reference/geo-layers/mvt-layer
const mvtLayer = new deck.MVTLayer({
map_id: el.id,
id: layerId,
data: [
'https://tiles-a.basemaps.cartocdn.com/vectortiles/carto.streets/v1/{z}/{x}/{y}.mvt'
],
minZoom: 0,
maxZoom: 14,
getFillColor: f => {
switch (f.properties.layerName) {
case 'poi':
return [255, 0, 0];
case 'water':
return [120, 150, 180];
case 'building':
return [218, 218, 218];
default:
return [240, 240, 240];
}
},
getLineWidth: f => {
switch (f.properties.class) {
case 'street':
return 6;
case 'motorway':
return 10;
default:
return 1;
}
},
getLineColor: [192, 192, 192],
getPointRadius: 2,
pointRadiusUnits: 'pixels',
stroked: false,
picking: true
});
md_update_layer( el.id, layerId, mvtLayer );
}
"
mapdeck(style = mapdeck_style("light"), repeat_view = TRUE) %>%
htmlwidgets::onRender(js) |
Here's a basic shiny example
./www/addMvt.jsDefine javascript functions to 'invoke' from R function clear_mvt(map_id, layer_id) {
md_clear_layer(map_id, layer_id); // internal mapdeck function for clearing layers by `layer_id`
}
function add_mvt(map_id, mvt_url, layer_id) {
const layerId = layer_id;
// reference: https://deck.gl/docs/api-reference/geo-layers/mvt-layer
const mvtLayer = new deck.MVTLayer({
map_id: map_id,
id: layerId,
data: [ mvt_url ],
minZoom: 0,
maxZoom: 14,
getFillColor: f => {
switch (f.properties.layerName) {
case 'poi':
return [255, 0, 0];
case 'water':
return [120, 150, 180];
case 'building':
return [218, 218, 218];
default:
return [240, 240, 240];
}
},
getLineWidth: f => {
switch (f.properties.class) {
case 'street':
return 6;
case 'motorway':
return 10;
default:
return 1;
}
},
getLineColor: [192, 192, 192],
getPointRadius: 2,
pointRadiusUnits: 'pixels',
stroked: false,
picking: true
});
// this is a mapdeck javascript function for adding layers to a `deckgl` object
md_update_layer( map_id, layerId, mvtLayer );
} mvt.RCreate the ## Define the javascript dependency. This gets added to the shiny server
mvt_dep <- list(
mapdeck:::createHtmlDependency(
name = "add_mvt"
, version = "1.0.0"
, src = "./www/"
, script = "addMvt.js"
, all_files = FALSE
)
)
## the `R` function to call in the shiny for adding an MVT layer
add_mvt <- function(map, url, layer_id) {
invoke_method(map, "add_mvt", url, layer_id) ## the `add_mvt` here is the js function defined in the .js file
}
## The `R` function to callin the shiny for clearing the MVT layer
clear_mvt <- function(map, layer_id) {
invoke_method(map, "clear_mvt", layer_id) ## the `clear_mvt` is the js function defined in the .js file
}
ui.Rlibrary(shiny)
library(shinydashboard)
library(mapdeck)
ui <- shinydashboard::dashboardPage(
header = shinydashboard::dashboardHeader()
, sidebar = shinydashboard::dashboardSidebar(
actionButton(
inputId = "load"
, label = "Load MVT"
),
actionButton(
inputId = "clear"
, label = "Clear MVT"
)
)
, body = shinydashboard::dashboardBody(
mapdeck::mapdeckOutput(
outputId = "map"
)
)
) server.Rlibrary(shiny)
library(mapdeck)
set_token(secret::get_secret("MAPBOX"))
source("./mvt.R")
server <- function(input, output, session) {
output$map <- mapdeck::renderMapdeck({
mapdeck(style = mapdeck_style("light")) %>%
mapdeck:::addDependency(mvt_dep)
})
## The `add_mvt` and `clear_mvt` are the `R` functions defined in the mvt.R file
observeEvent(input$load, {
mapdeck::mapdeck_update(map_id = "map") %>%
add_mvt(
url = 'https://tiles-a.basemaps.cartocdn.com/vectortiles/carto.streets/v1/{z}/{x}/{y}.mvt'
, layer_id = 'mvt-layer'
)
})
observeEvent(input$clear, {
mapdeck::mapdeck_update(map_id = "map") %>%
clear_mvt(layer_id = 'mvt-layer')
})
} |
Good to see things happening here Dave! And FYI, a couple of week ago, open street map started using MVT instead of raster, so you can also use the tileservers listed there ( |
Yeah it's been kinda slow progress on this one... :/ Thanks though @mpadge , I was not aware of OSMs migration to vector tiles. |
https://deck.gl/docs/api-reference/geo-layers/mvt-layer
The text was updated successfully, but these errors were encountered: