rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
805 stars 509 forks source link

addPolygons/addMarkers only draws first row of SF data.frame, issue disappears when layerId is omitted #574

Closed jeroenclaes closed 6 years ago

jeroenclaes commented 6 years ago

I'm using the github versions of both leaflet and leaflet.extras

I have the following leaflet code:

leaflet(width = "100%", height = "100%") %>%
      setView(4.3517103,50.8503396,10) %>%
      addTiles(group = "OpenStreetMap", urlTemplate = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png") %>%

      addProviderTiles("Stamen.Toner", group = "High contrast") %>%

      addProviderTiles("Esri.WorldImagery", group = "Satellite") %>%

       addDrawToolbar(
        targetGroup='Selection',
         polylineOptions = FALSE,
          circleOptions = FALSE,
        markerOptions = FALSE,
        editOptions = editToolbarOptions()
        ) %>%

      addLayersControl(
        baseGroups = c("High contrast", "OpenStreetMap", "Satellite"),
        options = layersControlOptions(collapsed = TRUE, autoZIndex = TRUE)

Based on the area selected with the drawing of leaflet.draw, my code selects polygons /point geometries from different shapefiles (propietary data, cannot share), represented in R as SF data.frames. I can see that the code selects multiple rows (as it should), because the selections have 10+ rows, but only the first row of each data.frame is showing up on the map when I use a leaflet Proxy (and for-loop, for the polygons) to add them to the map:

layers <- c()

      prox <-  leafletProxy("map", session, deferUntilFlush=TRUE) 
      prox %>%
        clearMarkers() %>%
        clearShapes() %>%
       addMarkers( data=setToPlot()$antennas, layerId = "layer_Antennas", group="Antennas")  

      for (i in names(setToPlot()$polygons)) {

        write(paste(i,"/", nrow(setToPlot()$polygons[[i]])), stderr())
        if(nrow(setToPlot()$polygons[[i]]) > 0) {
          prox  %>%
            addPolygons(data=setToPlot()$polygons[[i]],
                             layerId = paste0("layer_",i), group=i, dashArray = NULL)
          layers <- c(layers, i)
        }

      }

      prox %>%
        removeLayersControl() %>%
        addLayersControl(
          baseGroups = c("High contrast", "OpenStreetMap", "Satellite"),
          overlayGroups = c("Selection", "Antennas", layers),
          options = layersControlOptions(collapsed = TRUE, autoZIndex = TRUE)
        )  

I've found a similar issue on stackoverflow (https://stackoverflow.com/questions/51275767/only-the-first-polygon-is-showing-up-in-leaflet-2-0-1-choropleth-plot), where the suggested solution is to add "dashArray = NULL" to the addPolygons call, but this does not solve the problem.

In my tests, the issue goes away when I omit the layerId argument. This is the case even for simpler leaflet maps, such as

schloerke commented 6 years ago

I believe the documentation is misleading in that we can not add more than one element with the same layerId. If you were to retrieve the leaflet.js layer afterwards, it would only contain the last group that was added.

I believe you're looking for just the group argument. If this already uniquely defines the polygon group, there is no need for the layerId argument.

Leaflet.R should have a warning when a multiple polygons are supplied with a single layer id (vectors are allowed).

The minimal example below displays the behavior you've experienced and also using only the group argument.

Please let me know if I've missed your original Issue intent.

- Barret

library(leaflet)
states <- geojsonio::geojson_read("https://rstudio.github.io/leaflet/json/us-states.geojson", what = "sp")
class(states)
#> [1] "SpatialPolygonsDataFrame"
#> attr(,"package")
#> [1] "sp"
# works
leaflet(states) %>%
  addTiles() %>%
  addPolygons()

# displays last polygon only
leaflet(states) %>%
  addTiles() %>%
  addPolygons(layerId = "myLayer")

# works
leaflet(states) %>%
  addTiles() %>%
  addPolygons(group = "myGroup")

Created on 2018-08-07 by the reprex package (v0.2.0).

jeroenclaes commented 6 years ago

Oh, now I see thanks for the clarification!