【问题标题】:How to display WMS legends based on layer groups in Leaflet for R?如何根据 Leaflet for R 中的图层组显示 WMS 图例?
【发布时间】:2017-10-15 19:23:05
【问题描述】:

我正在尝试根据 Leaflet 中的图层组和 Shiny 中 R 的 Leaflet extras 显示 WMS 图例。我正在使用here 描述的 input$map_groups 但它似乎不起作用,关于如何隐藏和切换 WMS 图例的任何想法?

谢谢,

胡安

library(shiny)
library(leaflet)
library(leaflet.extras)

# User Interface
ui <- bootstrapPage(
  tags$style(type="text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width="100%", height="100%")
)

##### Shiny function server side

  server = function(input, output, session) {

    output$map <- renderLeaflet({
        leaflet() %>% 
        addProviderTiles("CartoDB.DarkMatter", options = tileOptions(minZoom = 0))%>% 
        addTiles(urlTemplate ="http://dataportal-dev.aquacross.eu/geoserver/gwc/service/tms/1.0.0/general:g2015_simplified@EPSG:900913@png/{z}/{x}/{y}.png",
                options = tileOptions(noWrap = TRUE, tms = TRUE, opacity =0.9),group ="P1", layerId ="test")%>% 
        addTiles(urlTemplate ="http://dataportal-dev.aquacross.eu/geoserver/gwc/service/tms/1.0.0/general:country@EPSG:900913@png/{z}/{x}/{y}.png",
                  options = tileOptions(noWrap = TRUE, tms = TRUE, opacity =1),group ="P2", layerId ="test2")%>% 
      # addWMSLegend(position = "topright",uri='http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=50&HEIGHT=20&LAYER=g2015_simplified', layerId ="test")%>% 
      addLayersControl(
        baseGroups = c("P1", "P2"),
       options = layersControlOptions(collapsed =FALSE)
      )
      })

## This is an attempt to show WMS legend maps based in groups

      observeEvent(input$map_groups,{
        map <- leafletProxy("map") %>% clearControls()
        if (input$map_groups == 'P1')
        {
         map %>% addWMSLegend(position = "topright",uri='http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=50&LAYER=g2015_simplified', layerId ="test")
         }
      else if (input$map_groups == 'P2')
        {
        map %>% addWMSLegend(position = "topright",uri='http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=20&LAYER=topp:states', layerId ="test2")
        }
     })
 }

shinyApp(ui, server)

【问题讨论】:

  • 能否具体说明您的问题?
  • 这个想法是根据图层组隐藏和切换 WMS 图例,但我找不到这样做的方法..
  • P1 和 P2 按钮的预期效果是什么?
  • 我刚刚编辑了代码,想法是根据P1和P2显示不同的WMS图例。

标签: r shiny leaflet legend wms


【解决方案1】:

我玩过你的代码,似乎addWMSLegend 函数在if 语句中不起作用。但是,它可以在正常的管道成语中使用,但这不是您想要的。标准的addLegend 函数在if 语句中可以正常工作,如以下代码所示。我也对您的代码进行了一些清理。

library(shiny)
library(leaflet)
# devtools::install_github('bhaskarvk/leaflet.extras')
library(leaflet.extras)

link1 <- "http://dataportal-dev.aquacross.eu/geoserver/gwc/service/tms/1.0.0/general:g2015_simplified@EPSG:900913@png/{z}/{x}/{y}.png"
link2 <- "http://dataportal-dev.aquacross.eu/geoserver/gwc/service/tms/1.0.0/general:country@EPSG:900913@png/{z}/{x}/{y}.png"
link3 <- "http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=50&HEIGHT=20&LAYER=g2015_simplified"
link4 <- "http://dataportal-dev.aquacross.eu/geoserver/wms?REQUEST=GetLegendGraphic&VERSION=1.0.0&FORMAT=image/png&WIDTH=20&HEIGHT=50&LAYER=g2015_simplified"

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%")
)

server = function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>% 
      addProviderTiles("CartoDB.DarkMatter", options = tileOptions(minZoom = 0), group = "P0", layerId = "DM") %>%
      addTiles(urlTemplate = link1, options = tileOptions(noWrap = TRUE, tms = TRUE, opacity = 1), group = "P1", layerId = "test") %>%
      addTiles(urlTemplate = link2, options = tileOptions(noWrap = TRUE, tms = TRUE, opacity = 1), group = "P2", layerId = "test2") %>%
      addWMSLegend(uri = link3, position = "topleft", layerId = "legend") %>%
      addLayersControl(baseGroups = c("P0", "P1", "P2"), options = layersControlOptions(collapsed = FALSE))
  })

  observeEvent(input$map_groups, {
    map <- leafletProxy("map") %>% clearControls()
    if (input$map_groups == "P0") {
      map <- map %>% addLegend(
        layerId = "legend",
        title = "Legend",
        position = "topleft",
        values = c(1, 2),
        labels = c("Gray", "Black"),
        colors = c("gray", "black"))
    } else if (input$map_groups == "P1") {
      map <- map %>% addLegend(
        layerId  = "legend",
        title = "Legend",
        position = "topleft",
        values = c(1, 2),
        labels = c("Gray", "Lemonchiffon"),
        colors = c("gray", "lemonchiffon"))
      # map <- map %>% addWMSLegend(layerId = "legend", uri = link3, position = "topleft")
    } else if (input$map_groups == "P2") {
      map <- map %>% addLegend(
        layerId  = "legend", 
        title = "Legend", 
        position = "topleft", 
        values = c(1, 2), 
        labels = c("Gray", "Tan"), 
        colors = c("gray", "tan"))
    }
  })

}

shinyApp(ui, server)

【讨论】:

  • 谢谢!这很好用,但问题是在这种情况下,我基本上必须手动生成 WMS 图例,并且像我一样使用更复杂的图例可能会非常耗时。也许问题可能与“leafletProxy”功能有关,因为 addWMSLegend 来自leaflet.extras ..
  • 是的,leafletleaflet.extras 包之间似乎缺乏互操作性。
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 2016-07-19
相关资源
最近更新 更多