在闪亮的应用程序中缓存或预渲染传单地图 [英] Cache or pre render leaflet map in shiny app

查看:88
本文介绍了在闪亮的应用程序中缓存或预渲染传单地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用传单映射约8000个多边形,并遇到性能问题.当我在一个闪亮的应用程序中使用地图时,我想知道是否有可能以某种方式缓存或预渲染该地图.

请注意,就我而言,我在之后交换了不同层次的多边形这种方法.

一个小的MWE是这样的:

可以从此处

下载数据

 库(发光)图书馆(传单)图书馆##下载Shapefile文件<-"plz-gebiete.shp";如果(!file.exists(file)){网址<-" https://www.suche-postleitzahl.org/download_files/public/plz-gebiete.shp.zipzipfile<-paste0(文件," .zip")download.file(URL,zipfile)解压缩(zipfile)}df<-st_read(file,options ="ENCODING = UTF-8")#如果可能的话:在此处预渲染地图!图书馆(闪亮)ui<-fluidPage(leafletOutput("mymap",宽度="700px",高度="700px"))服务器<-功能(输入,输出,会话){output$mymap <- renderLeaflet({传单()%>%addTiles()%&%;%addPolygons(数据= df,权重= 1,颜色=黑色")})}ShinyApp(用户界面,服务器) 

在我的计算机上,用多边形渲染地图大约需要16秒钟.

如果可能的话,我想将地图预先渲染一次,将其另存为 .rds 文件,并按需加载.请注意,我知道应用内地图的宽度/高度(此处设置为700px).但是类似

  map<-renderLeaflet({leaflet()%>%...})saveRDS(地图,"renderedmap.rds")映射<-readRDS("renderedmap.rds")在server()中的#输出$ mymap<-地图 

不会导致任何性能提升.

或者,我尝试异步加载传单,以便可以渲染应用程序的其他部分/进行交互,但无济于事.

有什么想法可以解决或解决这个问题吗?

解决方案

以下2种方法不能完全回答您的问题,但是与 leaflet :: addPolygons 相比,它们绝对是性能更高的替代方案.

使用Flatgeobuf格式:

基于 leafem :: addFgb :

的描述

Flatgeobuf可以逐块传输数据,以便呈现地图或多或少是瞬时的.地图是响应式的,而数据是仍在加载,以便弹出查询,缩放和平移将起作用即使尚未渲染所有数据.

我认为数据集是线串,这就是为什么 fillColor 似乎被忽略的原因.

 库(传单)图书馆(leafem)图书馆(闪亮)#通过URL(大约13mb的数据)url ="https://raw.githubusercontent.com/bjornharrtell/flatgeobuf/3.0.1/test/data/UScounties.fgb"ui<-fluidPage(leafletOutput("mymap",宽度="700px",高度="700px"))服务器<-功能(输入,输出,会话){output $ mymap<-renderLeaflet({传单()%>%addTiles()%&%;%leafem ::: addFgb(url = url,组=县",标签=名称",弹出窗口= TRUE,fillColor =蓝色",fillOpacity = 0.6,颜色=黑色",重量= 1)%>%addLayersControl(overlayGroups = c("counties"))%&%setView(lng = -105.644,lat = 51.618,zoom = 3)})}ShinyApp(用户界面,服务器) 


使用 leafgl (WebGL-Renderer):

 库(sf)图书馆(闪亮)图书馆(传单)图书馆(leafgl)plz<-st_read("C:/用户/用户/下载/plz-gebiete.shp",layer=plz-gebiete")ui<-fluidPage(leafletOutput("mymap",宽度="700px",高度="700px"))服务器<-功能(输入,输出,会话){output $ mymap<-renderLeaflet({传单()%>%addTiles()%&%;%addGlPolygons(data = plz,color =〜plz,popup ="note",group ="plz")%>%addLayersControl(overlayGroups ="plz")})}ShinyApp(用户界面,服务器) 

I am trying to map ~8000 polygons using leaflet and run into performance issues. As I am using the map within a shiny app, I was wondering if its possible to somehow cache or pre-render the map.

Note that in my case, I have different layers of polygons that are swapped following this approach.

A small MWE would be this:

The data can be downloaded from here

library(shiny)
library(leaflet)
library(sf)

## Download Shapefile
file <- "plz-gebiete.shp"

if (!file.exists(file)) {
  url <- "https://www.suche-postleitzahl.org/download_files/public/plz-gebiete.shp.zip"
  zipfile <- paste0(file, ".zip")
  download.file(url, zipfile)
  unzip(zipfile)
}

df <- st_read(file, options = "ENCODING=UTF-8")

# If possible: pre-render the map here!

library(shiny)

ui <- fluidPage(
  leafletOutput("mymap", width = "700px", height = "700px")
)

server <- function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      addPolygons(data = df, weight = 1, color = "black")
  })
}

shinyApp(ui, server)

It takes around 16 seconds on my machine to render the map with the polygons.

If possible, I would like to pre-render the map once, save it as an .rds file, and load it on demand. Note that I know the width/height of the map within the app (here set to 700px). But something like

map <- renderLeaflet({leaflet() %>% ...})
saveRDS(map, "renderedmap.rds")

map <- readRDS("renderedmap.rds")

# within server()
output$mymap <- map

does not result in any performance gains.

Alternatively, I have tried to load the leaflet asynchronously so that other parts of the app can be rendered/interacted with but to no avail.

Any ideas how to solve or circumnavigate this problem?

解决方案

The 2 following approaches dont exactly answer your question, but they are definitly more performant alternatives compared to leaflet::addPolygons.

Using Flatgeobuf Format:

Based on the description from leafem::addFgb:

Flatgeobuf can stream the data chunk by chunk so that rendering of the map is more or less instantaneous. The map is responsive while data is still loading so that popup queries, zooming and panning will work even though not all data has been rendered yet.

I think the dataset are linestrings, that is why fillColor seems to be ignored.

library(leaflet)
library(leafem)
library(shiny)

# via URL (data around 13mb)
url = "https://raw.githubusercontent.com/bjornharrtell/flatgeobuf/3.0.1/test/data/UScounties.fgb"

ui <- fluidPage(
  leafletOutput("mymap", width = "700px", height = "700px")
)

server <- function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      leafem:::addFgb(
        url = url, group = "counties",
        label = "NAME", popup = TRUE,
        fillColor = "blue", fillOpacity = 0.6,
        color = "black", weight = 1) %>%
      addLayersControl(overlayGroups = c("counties")) %>%
      setView(lng = -105.644, lat = 51.618, zoom = 3)
  })
}

shinyApp(ui, server)


Using leafgl (WebGL-Renderer):

library(sf)
library(shiny)
library(leaflet)
library(leafgl)

plz <- st_read("C:/Users/user/Downloads/plz-gebiete.shp", layer = "plz-gebiete")

ui <- fluidPage(
  leafletOutput("mymap", width = "700px", height = "700px")
)

server <- function(input, output, session) {
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addGlPolygons(data = plz, color = ~plz, popup = "note", group = "plz") %>% 
      addLayersControl(overlayGroups = "plz")
  })
}

shinyApp(ui, server)

这篇关于在闪亮的应用程序中缓存或预渲染传单地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆