Skip to content
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

FGB attach by group, highlightOptions, label as function #85

Merged
merged 18 commits into from
Sep 16, 2024

Conversation

trafficonese
Copy link
Contributor

@trafficonese trafficonese commented Jun 13, 2024

  • 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. enable highlighting for fgb mapview#415

  • mouseEvent will look if layerId is a property of the feature, otherwise just return layerId as string. Fix addFgb uses wrong label and layerId #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
Copy link
Contributor Author

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

@trafficonese
Copy link
Contributor Author

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

@tim-salabim
Copy link
Member

@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
Copy link
Member

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

@trafficonese
Copy link
Contributor Author

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

@tim-salabim tim-salabim merged commit 2b748e8 into r-spatial:master Sep 16, 2024
5 checks passed
@tim-salabim
Copy link
Member

Thanks!

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.

addFgb uses wrong label and layerId
2 participants