本文介绍了如何在传单addPolylines中绘制MULTILINESTRING?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

传单文档说:

但是我如何在传单中使用此类对象?

Yet how do I make use of such objects in leaflet?

这是一个MULTILINESTRING示例:

Here is a MULTILINESTRING example:

# 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循环分别绘制这些线之外,还有其他解决方案吗?

If this is not possible, is there a solution besides calling a for loop to plot those lines separately?

推荐答案

好的,这比我想象的要容易得多.只需将 MULTILINESTRING 对象放在 leaflet()中:

Okay, this has been a lot easier than I thought it would. Just put the MULTILINESTRING object inside of leaflet():

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

这篇关于如何在传单addPolylines中绘制MULTILINESTRING?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 20:45