leafem icon indicating copy to clipboard operation
leafem copied to clipboard

FGB attach by group, highlightOptions, label as function

Open trafficonese opened this issue 1 year ago • 1 comments

  • This changes the FGB attachments and uses group instead of layerId. Although layerId cannot be a vector or formula with addFgb, I think we should treat the leaflet arguments as similar as possible. But maybe in this case we could ignore that, or what if we expose a new argument dataSrc for example and set it to to group if it is NULL?

  • Update Flatgeobuf to '3.31.1' from '3.21.3'

  • expose highlightOptions for mouseover-effect. https://github.com/r-spatial/mapview/issues/415

  • mouseEvent will look if layerId is a property of the feature, otherwise just return layerId as string. Fix #80

  • label can also be a JS() function. (see the shiny example below)

  • Some JS-cleanup, fix indentation in addFlatGeoBufFiltered, changed var to let/const, etc..

and sry for the git-commit-mess.. I rebased against master, but in the PR the individal commits are visible again.


Shiny-App

## pkgs ###############
library(leafem)
library(leaflet)
library(shiny)
library(sf)

## Transform Data to FGB ############
options("shiny.autoreload" = TRUE)

url = "https://flatgeobuf.org/test/data/UScounties.fgb"
# fgb <- sf::st_read(dsn = url)
# fgb <- st_cast(fgb, "POLYGON")
# uscounties <- "./uscounties_polyon.fgb"
# sf::st_write(obj = fgb, dsn = uscounties, driver = "FlatGeobuf", delete_dsn = TRUE)

## UI ##################
ui <- fluidPage(
  actionButton("show", "show"),
  actionButton("hide", "hide"),
  actionButton("clearGroup", "clearGroup"),
  leafletOutput("map", height = 700),
  verbatimTextOutput("printclick")
)

## SERVER ##################
server <- function(input, output){
  output$map <- renderLeaflet({
    leaflet("map") %>%
      setView(-104, 44, 7) %>%
      addTiles() %>%
      leafem::addMouseCoordinates() %>%
      addFgb(
        # file = uscounties
        url = url
        , fill = TRUE
        , group = "Lines_FGB"
        , layerId = "FIPS"
        , stroke = TRUE
        , color = "#41db5d"
        , fillOpacity = 0.3
        , popup = TRUE
        # , label = "NAME"
        , label = JS("function (layer) {
                        return(json2table(layer.feature.properties));
                       }")
        # , minZoom = 6
        , highlightOptions = highlightOptions(color = "#207f01", fillOpacity=0.8)
      ) %>%
      addLayersControl(position = "topright",
                       overlayGroups = c("Lines_FGB"),
                       options = layersControlOptions(collapsed = F, autoZIndex = TRUE)
      )
  })
  
  ## Observer ###############
  observeEvent(input$show, {
    leafletProxy("map") %>% showGroup("Lines_FGB")
  })
  observeEvent(input$hide, {
    leafletProxy("map") %>% hideGroup("Lines_FGB")
  })
  observeEvent(input$clearGroup, {
    leafletProxy("map") %>% clearGroup("Lines_FGB")
  })
  
  ## Ouputs ###############
  output$printclick <- renderPrint({
    click <- req(input$map_shape_click)
    req(click$group == "Lines_FGB")
    print(click)
  })
}

shinyApp(ui = ui, server = server)

trafficonese avatar Jun 13 '24 22:06 trafficonese

The commit 2300146 fixes the options. Currently TRUE options are always removed (in my case contextmenu = TRUE and interactive = TRUE)

trafficonese avatar Jul 08 '24 09:07 trafficonese

@tim-salabim How do you feel about this PR? Too many potential breaking changes?

trafficonese avatar Sep 09 '24 15:09 trafficonese

@trafficonese I am not sure about the breaking changes. In all honesty, I forgot about this PR... Let me see if mapview still works with it.

tim-salabim avatar Sep 10 '24 10:09 tim-salabim

Seems to not break anything as far as I can see. Is it safe to merge (wrt the layerviewcontrol PR)?

tim-salabim avatar Sep 10 '24 10:09 tim-salabim

no worries :) Yes it should be safe, and it would make FGB the most feature-complete alternative to leaflets-functions.

trafficonese avatar Sep 10 '24 11:09 trafficonese

Thanks!

tim-salabim avatar Sep 16 '24 15:09 tim-salabim