I am trying to display a simple feature collection of (disconnected) linestrings in R using sf
and leaflet
. The data is obtained from openstreetmap using osmdata
.
Using the base plot()-function, tmap
, or mapview
, I am able to directly display the linestrings on a static and interactive map. However, this does not work using leaflet
. Leaflet correctly centers the map, but does not display the linestrings. Fiddling with color, weight, etc. parameters in addPolyLines() does not seem to help.
Reprex
library(osmdata)
library(leaflet)
library(sf)
library(tmap)
osm <- opq(bbox = c(-0.27, 51.47, -0.20, 51.50)) %>%
add_osm_feature(key = 'name', value = "Thames", value_exact = FALSE) %>%
osmdata_sf()
osm$osm_lines
Simple feature collection with 2085 features and 178 fields
geometry type: LINESTRING
dimension: XY
bbox: xmin: -2.029917 ymin: 51.37841 xmax: 0.6778926 ymax: 51.79032
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 10 features:
...
plot(osm$osm_lines, max.plot = 1) # OK
qtm(osm$osm_lines) + tm_lines() # OK
mapview(osm$osm_lines) # OK
leaflet(osm$osm_lines) %>% # not OK (grey)
addPolylines()
Session-info
> library(sf)
Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
Warning message:
package ‘sf’ was built under R version 3.4.4
> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tmap_1.11-2 sf_0.6-3 leaflet_2.0.1 osmdata_0.0.7
loaded via a namespace (and not attached):
[1] httr_1.3.1 viridisLite_0.3.0 jsonlite_1.5 splines_3.4.3 geojsonlint_0.2.0 foreach_1.4.4 R.utils_2.6.0 gtools_3.5.0
[9] shiny_1.1.0 expm_0.999-2 sp_1.3-1 stats4_3.4.3 yaml_2.1.19 LearnBayes_2.15.1 lattice_0.20-35 digest_0.6.15
[17] RColorBrewer_1.1-2 promises_1.0.1 rvest_0.3.2 colorspace_1.3-2 plyr_1.8.4 htmltools_0.3.6 httpuv_1.4.3 Matrix_1.2-12
[25] R.oo_1.22.0 XML_3.98-1.11 rmapshaper_0.4.0 raster_2.6-7 gmodels_2.16.2 xtable_1.8-2 webshot_0.5.0 scales_0.5.0
[33] gdata_2.18.0 satellite_1.0.1 later_0.7.3 gdalUtils_2.0.1.14 mapview_2.4.0 magrittr_1.5 mime_0.5 deldir_0.1-15
[41] R.methodsS3_1.7.1 nlme_3.1-131 MASS_7.3-47 xml2_1.2.0 class_7.3-14 tools_3.4.3 geosphere_1.5-7 stringr_1.3.1
[49] V8_1.5 munsell_0.5.0 compiler_3.4.3 e1071_1.6-8 classInt_0.2-3 units_0.6-0 grid_3.4.3 tmaptools_1.2-4
[57] RCurl_1.95-4.10 dichromat_2.0-0 iterators_1.0.9 htmlwidgets_1.2 crosstalk_1.0.0 bitops_1.0-6 base64enc_0.1-3 boot_1.3-20
[65] codetools_0.2-15 DBI_1.0.0 jsonvalidate_1.0.0 curl_3.2 R6_2.2.2 lubridate_1.7.4 rgdal_1.3-2 rgeos_0.3-28
[73] spdep_0.7-7 KernSmooth_2.23-15 stringi_1.1.7 osmar_1.1-7 Rcpp_0.12.17 png_0.1-7 spData_0.2.8.3 coda_0.19-1
osmdata has named features, which leaflet cannot read. See https://github.com/ropensci/osmdata/issues/100
As a hack you can set names to NULL:
osm_lines <- osm$osm_lines
names(osm_lines$geometry) <- NULL
leaflet(osm_lines) %>%
addPolylines()