osmdata包中的数据未出现addPolylines()问题

时间:2019-05-28 13:16:58

标签: r leaflet sf

我在使用leaflet::addPolylines对象和sf为R的Leaflet时遇到了问题。

下面是我用来提取(作为随机示例)伦敦铁路的代码。

library(osmdata)
library(leaflet)
library(sf)
library(ggplot2)

# Get Data
dlr <-
  opq("London, UK") %>% 
  add_osm_feature(key = "line", value = "DLR") %>% 
  osmdata_sf()

str(dlr$osm_lines)

# Classes ‘sf’ and 'data.frame':    213 obs. of  25 variables:
#   $ osm_id         : chr  "3636480" "3663203" "4005749" "4005750" ...
# $ name           : chr  "Docklands Light Railway" "Docklands Light Railway" "Docklands Light Railway" "Docklands Light Railway" ...
# $ bridge         : chr  "viaduct" "viaduct" NA NA ...
# $ covered        : chr  NA NA NA NA ...
# $ cutting        : chr  NA NA NA NA ...
# $ disused.railway: chr  NA NA NA NA ...
# $ electrified    : chr  "rail" "rail" "rail" "rail" ...
# $ fixme          : chr  NA NA NA NA ...
# $ frequency      : chr  "0" "0" "0" "0" ...
# $ gauge          : chr  "1435" "1435" "1435" "1435" ...
# $ layer          : chr  "1" "1" "-2" "-2" ...
# $ level          : chr  NA NA NA NA ...
# $ line           : chr  "DLR" "DLR" "DLR" "DLR" ...
# $ note           : chr  NA NA "Route guessed" "Route guessed" ...
# $ oneway         : chr  NA NA NA NA ...
# $ railway        : chr  "light_rail" "light_rail" "light_rail" "light_rail" ...
# $ service        : chr  NA NA NA NA ...
# $ short_name     : chr  NA NA NA NA ...
# $ source         : chr  NA NA NA NA ...
# $ source_ref     : chr  NA NA NA NA ...
# $ start_date     : chr  NA NA NA NA ...
# $ track_detail   : chr  NA NA NA NA ...
# $ tunnel         : chr  NA NA "yes" "yes" ...
# $ voltage        : chr  "750" "750" "750" "750" ...
# $ geometry       :sfc_LINESTRING of length 213; first list element:  'XY' num [1:4, 1:2] -0.0673 -0.0669 -0.0664 -0.0661 51.5111 ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr  "18019994" "1842525419" "1752475375" "18019985"
# .. ..$ : chr  "lon" "lat"
# - attr(*, "sf_column")= chr "geometry"
# - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
# ..- attr(*, "names")= chr  "osm_id" "name" "bridge" "covered" ...

然后,使用ggplot()geom_sf()进行绘制就可以了:

dlr$osm_lines %>% 
  ggplot() + geom_sf()

DLR map using ggplot

但不能使用Leaflet

dlr$osm_lines %>% 
  leaflet() %>% 
  addProviderTiles("Stamen.Watercolor") %>% 
  addPolylines()

DLR map! using Leaflet

对不必要的水彩画表示歉意-只是想清楚地表明线条不存在。

2 个答案:

答案 0 :(得分:1)

在最近更新之后,在线的几何中设置名称似乎是一个问题-请参阅此处的讨论https://github.com/r-spatial/sf/issues/880-建议删除它们。

这对我来说适合您的示例...

names(st_geometry(dlr$osm_lines)) = NULL

dlr$osm_lines %>% 
  leaflet() %>% 
  addProviderTiles("Stamen.Watercolor") %>% 
  addPolylines()

答案 1 :(得分:0)

希望可以通过传单更新来解决-参见https://github.com/rstudio/leaflet/issues/631