r-spatial/leafem

Edit label in addFgb

Filhomn opened this issue · 3 comments

Is it possible to edit the label? So the tooltip not only displays only the value of the field, but also other information or more than one field.

I don't think it is atm. I'll look into it

This should work now much better. Here's a few options that should work now:

library(leaflet)
library(leafem)
library(mapview)
library(sf)

fl = tempfile(fileext = ".fgb")
sf::st_write(
  obj = franconia
  , dsn = fl
  , driver = "FlatGeobuf"
  , layer_options = c("SPATIAL_INDEX=NO")
)

# # 1. character vector same length as data
# lab = sprintf("test %s", seq(nrow(mapview::franconia)))

# # 2. single character scalar
# lab = "test"

# # 3. name of a column/field in data
# lab = "NAME_ASCI"

# 4. a custom html popup with multiple columns/fields of data
# NOTE: this only works if fgb file is written without spatial index (as above)!!
lab = lapply(seq(nrow(franconia)), function(i) {
  paste0(
    '<p>'
    , franconia[["NAME_ASCI"]][i]
    , '<p></p>'
    , franconia[["NUTS_ID"]][i]
    , '</p>' 
  ) 
})
lab = lapply(lab, htmltools::HTML)

# try map
leaflet() %>%
  addTiles() %>%
  leafem:::addFgb(
    file = fl
    , group = "counties"
    , label = lab
    , popup = TRUE
    , fill = TRUE
    , fillColor = "blue"
    , fillOpacity = 0.6
    , color = "black"
    , weight = 1
  ) %>%
  addLayersControl(overlayGroups = c("counties")) %>%
  addMouseCoordinates() %>%
  setView(lng = 10.69, lat = 49.72, zoom = 8)

One note of caution, if you pass a vector of labels (options 1 & 4), you need to make sure that the fgb file was written without a spatial index, as otherwise the order of the features and the labels will not match and labels will be wrong!

Does this resolve your issue?

Yes!! Amazing!! Thank you very much!!