Skip to content

Instantly share code, notes, and snippets.

@tim-salabim
Created February 25, 2023 13:29
Show Gist options
  • Save tim-salabim/8fd85c739b68d5bf2c4bba4ac05be2c2 to your computer and use it in GitHub Desktop.
Save tim-salabim/8fd85c739b68d5bf2c4bba4ac05be2c2 to your computer and use it in GitHub Desktop.
layer selector
library(mapview)
library(leafem)
library(leaflet)
library(sf)
library(geojsonsf)
franc_ext <- unname(as.vector(st_bbox(franconia)))
francgj = geojsonsf::sf_geojson(franconia)
leaflet() |>
addTiles() |>
addGeoJSON(francgj, layerId = "franc", group = "franc", weight = 1) |>
fitBounds(franc_ext[1], franc_ext[2], franc_ext[3], franc_ext[4]) |>
leafem:::addLayerSelector(colnames(franconia), "franc")
@tim-salabim
Copy link
Author

tim-salabim commented Feb 28, 2023

New version

library(mapview)
library(leafem)
library(leaflet)
library(sf)
library(geojsonsf)
library(terra)

franc_ext <- unname(as.vector(st_bbox(franconia)))
francgj = geojsonsf::sf_geojson(franconia)

f <- system.file("ex/lux.shp", package="terra")
lux <- vect(f)
luxgj = geojsonsf::sf_geojson(st_as_sf(lux))

leaflet() |>
  addTiles() |>
  addGeoJSON(francgj, layerId = "franc", group = "franc", weight = 1) |>
  addGeoJSON(luxgj, layerId = "lux", weight = 1) |>
  leafem:::addGeoJSONLayerSelector(colnames(franconia), "franc") |>
  leafem:::addGeoJSONLayerSelector(names(lux), "lux") |>
  fitBounds(franc_ext[1], franc_ext[2], franc_ext[3], franc_ext[4])

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