簡體   English   中英

如何在plot MULTILINESTRING leaflet addPolylines?

[英]How to plot MULTILINESTRING in leaflet addPolylines?

leaflet 文檔說:

線和多邊形數據可以來自多種來源:[...] MULTIPOLYGON、POLYGON、MULTILINESTRING 和 LINESTRING 對象(來自 sf 包)

然而,我如何在 leaflet 中使用這些對象?

這是一個 MULTILINESTRING 示例:

# attach packages---------------------------------------------------------------
library(dplyr)
library(sf)
library(leaflet)

# set up data ------------------------------------------------------------------
set.seed(5)
data <-  tibble(X = 1:5 * 2 + runif(5),
                Y = 1:5 - runif(5),
                GROUP = c("A", "A", "B", "B", "B"))
# A tibble: 5 x 3
#       X     Y GROUP
#   <dbl> <dbl> <chr>
# 1  2.27 0.798 A    
# 2  4.49 1.61  A    
# 3  6.32 2.11  B    
# 4  8.56 3.45  B    
# 5 10.3  4.16  B 

# create MULTILINESTRING -------------------------------------------------------
multiline <- data %>% 
  st_as_sf( coords = c("X", "Y")) %>% 
  group_by(GROUP) %>% 
  summarize() %>%
  st_cast("MULTILINESTRING") %>% 
  st_set_crs("+init=epsg:2154") %>% 
  st_transform(crs="+proj=longlat +datum=WGS84")

# plot with leaflet ------------------------------------------------------------
leaflet() %>% 
  addTiles() %>% 
  addPolylines(multiline)
# Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolylines") : 
# addPolylines must be called with both lng and lat, or with neither.

如果這是不可能的,除了分別調用 for 循環到 plot 這些行之外,還有其他解決方案嗎?

好的,這比我想象的要容易得多。 只需將MULTILINESTRING object 放入leaflet()

leaflet(multiline) %>% 
  addTiles() %>% 
  addPolylines()

@mnist,它比那更簡單。 您只需要在addPolylines中添加data參數,它允許您添加具有不同數據源的多行:

leaflet() %>%
  addTiles() %>%
  addPolylines(data=multiline, color='blue') %>%
  addPolylines(data=otherlines, color='orange')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM