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

Possibility of using leaflet.extra::addSearchFeature to addGlPolygons #18

Open
BastienFR opened this issue Apr 11, 2019 · 1 comment
Open
Labels
upstream needs to be adressed in Leaflet.Glify

Comments

@BastienFR
Copy link

Sorry to bother you again, I have another question/feature request.

For the map I'm building, I need to add search field the user to quickly pin point the feature (postal code) it's looking for. The package leaflet.extra has this functionality which works really well:

library(sf)  
library(leaflet)
library(leafgl)
library(colourvalues)
library(leaflet.extras)

nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>% 
  st_transform(st_crs(4326)) %>% 
  st_cast('POLYGON')

cols = colour_values_rgb(nc$AREA, palette = "inferno", include_alpha = FALSE) / 255
colpal <- colorNumeric(palette = "inferno", domain=nc$AREA, n=10)


leaflet(data=nc) %>% 
addProviderTiles(provider = providers$OpenStreetMap) %>%
addPolygons(color  = ~colpal(AREA),
            popup = ~NAME,
            label = ~NAME,
            group = "name") %>% 
addSearchFeatures(targetGroups  = 'name',
                  options = searchFeaturesOptions(zoom=10, openPopup=TRUE))

This creates a map which I can search:

image

I want to reproduce this but by using addGlPolygons instead of addPolygons. The problem is that addSearchFeatures requires addPolygons's label parameter using a ~ or called with nc$NAME which isn't available in addGlPolygon:

leaflet() %>%
   addProviderTiles(provider = providers$OpenStreetMap) %>%
   addGlPolygons(nc, 
                 color = cols,
                 popup = "NAME",
                 label = ~NAME,
                 group = "name") %>% 
   fitBounds(lng1 = unname(st_bbox(nc)$xmin), lng2 = unname(st_bbox(nc)$xmax), lat1 = unname(st_bbox(nc)$ymin), lat2 = unname(st_bbox(nc)$ymax)) %>% 
   addSearchFeatures(targetGroups  = 'name',
                     options = searchFeaturesOptions(zoom=10, openPopup=TRUE))
Error in geojsonsf::sf_geojson(data, ...) : 
  unused argument (label = ~NAME) 

My actual problem requires me to plot around 50,000 polygons, so I have to use leafgl instead of basic leaflet. However, the search feature is really needed to sort through all the 50,000 polygons. I tried to go around the problem by using leafgl to plot the polygons and adding an invisible leaflet::addMarkers to allow the search. I was expecting leaflet to be able to handle 50,000 points which should be simpler than 50,000 polygons but it wasn't the case, it failed. The code I used:

suppressWarnings(nc_marker <- st_centroid(nc))
  
leaflet() %>%
  addProviderTiles(provider = providers$OpenStreetMap) %>%
  addMarkers(data = nc_marker,
                   popup = ~NAME,
                   label = ~NAME,
                   group = "name",
                   options = markerOptions(opacity = 0)) %>% 
  addSearchFeatures(targetGroups  = 'name',
                    options = searchFeaturesOptions(zoom=10, openPopup=TRUE)) %>%
  addGlPolygons(data = nc, 
                color = cols, 
                popup = "NAME") %>%
  fitBounds(lng1 = unname(st_bbox(nc)$xmin), lng2 = unname(st_bbox(nc)$xmax),
            lat1 = unname(st_bbox(nc)$ymin), lat2 = unname(st_bbox(nc)$ymax)) 

Which produce:

image

Do you think there a way to add the label argument in your addGl* functions so they are compatible with leaflet.extra?

@tim-salabim
Copy link
Member

Sorry for the late reply, I think this needs to be addressed in the upstream repo

@trafficonese trafficonese added the upstream needs to be adressed in Leaflet.Glify label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream needs to be adressed in Leaflet.Glify
Projects
None yet
Development

No branches or pull requests

3 participants