Skip to content

#521 codebase extended to support vector basemaps - #523

Closed
gmatosferreira wants to merge 1 commit into
r-spatial:masterfrom
gmatosferreira:master
Closed

gmatosferreira wants to merge 1 commit into
r-spatial:masterfrom
gmatosferreira:master

Conversation

@gmatosferreira

@gmatosferreira gmatosferreira commented Nov 6, 2026 •

Copy link
Copy Markdown

I have extended the codebase to support vector tiles. Since the upstream change was not welcome at leaflet-extras/leaflet-providers#705, I have implemented the support for MapLibre GL vector tile basemaps by extending basemap to a named vector that, in addition to leaflet::providers raster tiles, now supports custom vector tiles urls.

# Custom vector tile styles (named or unnamed) mixed with raster providers
mapviewOptions(
  basemaps = c(
    # vector tile style JSON URL (or local file path)
    "OpenFreeMap.Liberty" = "https://tiles.openfreemap.org/styles/liberty",
    # raster tile provider from leaflet::providers
    "OpenStreetMap"
  )
)

For this, I have adapted R/options.R to set OpenFreeMap.Positron and OpenFreeMap.Dark as default (left all the other raster layers) and implemented the vector support at R/leafletControls.R, via R/vectorTiles.R, which validates the vector tiles URLs, their syntax and attaches the necessary script dependencies to the map HTML generated, through htmlwidgets::onRender hook. Minor changes have been performed on other scripts, to assure compatibility. Below is an example script that I used to test and validate these changes.

devtools::load_all()
mapviewOptions()

bus_lanes = sf::st_read("~/Downloads/bus_lanes_osm_lisbon.gpkg", quiet = TRUE)
bus_lanes_1 = bus_lanes[c(0:200),]
bus_lanes_2 = bus_lanes[c(200:400),]
bus_lanes_3 = bus_lanes[c(400:600),]

# Using default basemaps (vector tile OpenFreeMap.Positron set default)
mapview(bus_lanes_1, color = "#3BC1A8")

# Testing another platform
options("mapviewPlatform" = "leafgl")
m_platform = mapview(bus_lanes_3, color = "#3BC1A8")
m_platform

# Changing to raster
options("mapviewBasemaps" = c("OpenStreetMap"), "mapviewPlatform" = "leaflet")
m_raster = mapview(bus_lanes_1, color = "#7850bc")
m_raster

# Back to vector (another one)
options("mapviewBasemaps" = c("OpenFreeMap.Fiord" = "https://tiles.openfreemap.org/styles/fiord"))
m_vector = mapview(bus_lanes_2, color = "#90d04a")
m_vector

# Now, testing operations
m_raster + m_vector

m_vector + m_raster

m_vector | m_raster

m_raster | m_vector

m_vector + m_raster + m_platform

m_platform + m_vector + m_raster 

m_raster + m_platform + m_vector

# Testing invalid layer
options("mapviewBasemaps" = c("UnknownLayer"))
mapview(bus_lanes_1, color = "#7850bc")

options("mapviewBasemaps" = c("OpenFreeMap.Invalid" = "https://tiles.openfreemap.org/styles/invalid"))
mapview(bus_lanes_1, color = "#7850bc")

@tim-salabim

Copy link
Copy Markdown
Member

Thanks for this PR! How would you feel about providing the proposed vector tile support as a stand-alone package? I am quite happy with using the Stadia.* basemaps as replacements for Carto*.

I do like the support for vector tiles though! However, attaching the JS dependencies on the fly from a CDN is not optimal, as I think quarto (and possibly markdown) will complain. Have you tested running this in a quarto document?

Ideally, we should ship the dependencies with the package. Also, have you tested with maplibre > v6? There might be problems as they've changed their release pattern.

@gmatosferreira

gmatosferreira commented Nov 13, 2026 •

Copy link
Copy Markdown
Author

Hey, @tim-salabim! No, I haven't performed such a thorough batch of test cases. I get your concerns and considering them, I agree that it makes sense go proceed with this vector tiles support in a new package (with a native support instead of this workarounds). However, I currently don't have the availability to proceed with this project. I'll close this PR for now and let's hope that eventually in the near future someone takes over this gap in the R maps. 🙂

@tim-salabim

Copy link
Copy Markdown
Member

Thanks for the feedback. I have a long-term plan to replace the leaflet based mapview with a modern, maplibre/deck.gl based version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants