Is there a max number of polylines that can be added to leaflet?

I am having trouble rendering a relatively large number of polylines in leaflet. It's not that it takes an inordinate amount of time for it to render, but rather that not all of the elements show up. I have been trying to determine if there is some limit to the number of layers that is causing the render to fail.


   library(httr)
   library(tidyverse)
   library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
   library(leaflet)
   library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
   
   # retrieve the EIA Natural Gas Shape Files
   tmp <- tempfile(fileext = ".zip")
   GET("https://www.eia.gov/maps/map_data/NaturalGas_InterIntrastate_Pipelines_US_EIA.zip", write_disk(tmp))
#> Response [https://www.eia.gov/maps/map_data/NaturalGas_InterIntrastate_Pipelines_US_EIA.zip]
#>   Date: 2019-12-20 21:00
#>   Status: 200
#>   Content-Type: application/x-zip-compressed
#>   Size: 3.14 MB
#> <ON DISK>  C:\Users\gmccomas\AppData\Local\Temp\RtmpaQGkIS\file73fc1b5860c2.zip
   file_names <- unzip(tmp, list = TRUE)$Name
   unzip(tmp)
   
   # convert natural gas pipeline shapefile to sf data.frame
   ng_pipes <- st_read(str_subset(file_names, "\\.shp$"))
#> Reading layer `NaturalGas_Pipelines_US_201804_v3' from data source `C:\Users\gmccomas\AppData\Local\Temp\RtmpAbvuVN\reprex769c22a73f08\NaturalGas_Pipelines_US_201804_v3.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 33806 features and 3 fields (with 163 geometries empty)
#> geometry type:  MULTILINESTRING
#> dimension:      XY
#> bbox:           xmin: -151.5589 ymin: 25.38629 xmax: -67.43964 ymax: 60.49997
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
   
   unlink(unzip(tmp, list = TRUE)$Name, recursive = TRUE)
   unlink(tmp, recursive = TRUE)
   
   # filter down to just interstate pipes
   ng_interstate <- ng_pipes %>% 
       filter(TYPEPIPE == "Interstate") %>% 
       mutate(
           Operator = as.character(Operator),
           text = glue("Operator: {Operator}")
       )
   
   ng_interstate    
#> Simple feature collection with 18582 features and 4 fields (with 161 geometries empty)
#> geometry type:  MULTILINESTRING
#> dimension:      XY
#> bbox:           xmin: -123.4763 ymin: 25.38629 xmax: -67.43964 ymax: 49.00192
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#> First 10 features:
#>      TYPEPIPE                     Operator Shape_Leng
#> 1  Interstate Enbridge Pipelines (AlaTenn) 0.17176595
#> 2  Interstate Enbridge Pipelines (AlaTenn) 0.17418924
#> 3  Interstate Enbridge Pipelines (AlaTenn) 0.17547824
#> 4  Interstate Enbridge Pipelines (AlaTenn) 0.05759876
#> 5  Interstate Enbridge Pipelines (AlaTenn) 0.09999168
#> 6  Interstate Enbridge Pipelines (AlaTenn) 0.28506669
#> 7  Interstate Enbridge Pipelines (AlaTenn) 0.07489261
#> 8  Interstate Enbridge Pipelines (AlaTenn) 0.12004247
#> 9  Interstate Enbridge Pipelines (AlaTenn) 0.17320178
#> 10 Interstate Enbridge Pipelines (AlaTenn) 0.13611706
#>                          geometry                                   text
#> 1  MULTILINESTRING ((-86.74458... Operator: Enbridge Pipelines (AlaTenn)
#> 2  MULTILINESTRING ((-86.91853... Operator: Enbridge Pipelines (AlaTenn)
#> 3  MULTILINESTRING ((-86.91853... Operator: Enbridge Pipelines (AlaTenn)
#> 4  MULTILINESTRING ((-86.97606... Operator: Enbridge Pipelines (AlaTenn)
#> 5  MULTILINESTRING ((-87.07142... Operator: Enbridge Pipelines (AlaTenn)
#> 6  MULTILINESTRING ((-87.34918... Operator: Enbridge Pipelines (AlaTenn)
#> 7  MULTILINESTRING ((-87.30979... Operator: Enbridge Pipelines (AlaTenn)
#> 8  MULTILINESTRING ((-87.46598... Operator: Enbridge Pipelines (AlaTenn)
#> 9  MULTILINESTRING ((-87.63802... Operator: Enbridge Pipelines (AlaTenn)
#> 10 MULTILINESTRING ((-87.77408... Operator: Enbridge Pipelines (AlaTenn)
   
   # split into list of sf data.frames by 'Operator'
   ng_interstate <- ng_interstate %>% 
       split(.$Operator)
   
   # create pipeline palette
   pipe_pal <- colorFactor(
       c("darkred", "yellow2", "purple4"),
       domain = names(ng_interstate)
   )
   
   render_pipe_map <- function(pipe_list) {
       # create initial leaflet map
       pipe_map <- leaflet() %>%
           addProviderTiles(providers$Esri.WorldGrayCanvas)
       
       # add all the pipeline polyline layers 
       names(pipe_list) %>%
           walk(function(pipeline) {
               pipe_map <<- pipe_map %>%
                   addPolylines(
                       data = pipe_list[[pipeline]],
                       smoothFactor = 2,
                       color = ~pipe_pal(pipeline),
                       group = pipeline,
                       label = pipeline
                   )
           })
       
       # render leaflet
       pipe_map %>% 
           addLayersControl(
               overlayGroups = names(pipe_list) %>% sort,
               options = layersControlOptions(collapsed = FALSE)
           )
   }
   
   # does not like trying to render all the pipes
   render_pipe_map(ng_interstate)

   
   # is fine with 8 of the pipes
   render_pipe_map(ng_interstate[sample(seq_len(length(ng_interstate)), 8)])

Created on 2019-12-20 by the reprex package (v0.3.0)

2 Likes

Short answer to "Is there a max number of polylines that can be added to leaflet", probably but I didn't hit it here. The simple features data.frame I pulled down was missing geometries which was tripping up leaflet. To address this issue, I just filtered out the empty geometries and everything rendered appropriately.

    # convert natural gas pipeline shapefile to sf data.frame
    ng_pipes <- st_read(str_subset(file_names, "\\.shp$")) %>% 
    # filter out missing geometries
        filter(!is.na(st_dimension(geometry)))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.