使用传单裁剪开放式街道地图

时间:2019-05-07 15:15:29

标签: r leaflet

我的问题是我在大约两周前发布的this问题的扩展。假设我有一个Shiny应用程序,该应用程序允许用户在感兴趣的区域周围绘制多边形并裁剪出边界框之外的所有信息(代码由SeGa提供):

library(leaflet)
library(leaflet.extras)
library(sf)
library(raster)
library(maptools)

# Downloads some Worldclim data for cropping
r<-getData('worldclim', var='bio', res=10)
r<-r[[1]]

# Crop 'r' when action button is pressed
ui <- fluidPage(
  leafletOutput("map"),
  p("Your area of extent is:"),
  textOutput("poly"),

  # actionButton takes as input the result of observeEvent
  # Crop 'r' when action button is pressed
  actionButton(inputId = "action", label = "Crop"),
  ## Plot the cropped raster
  plotOutput("cropimg")
)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    leaflet() %>% 
      addProviderTiles(group = "OSM (default)", providers$OpenStreetMap) %>% 
      addRasterImage(group="Worldclim", r, opacity = 0.75) %>% 
      addDrawToolbar(polylineOptions = F, circleOptions = F, markerOptions = F,  circleMarkerOptions = F, polygonOptions = F) %>% 

      addLayersControl(
        baseGroups = c("OSM (default)"),
        overlayGroups = c("Worldclim"),
        options = layersControlOptions(collapsed = FALSE)
      )

  })

  bboxRV <- reactiveVal(NULL)

  observeEvent(input$map_draw_new_feature, {
    feat <- input$map_draw_new_feature
    coords <- unlist(feat$geometry$coordinates)
    coords <- matrix(coords, ncol = 2, byrow = T)
    poly <- st_sf(st_sfc(st_polygon(list(coords))), crs = st_crs(27700))
    # use Extent not BBOX
    bbox <- extent(poly)
    bboxRV(bbox)
  })

  output$poly <- renderPrint({
    req(bboxRV())
    bboxRV()
  })

  ## ReactiveValue for the cropped Image
  croppedRaster <- reactiveVal(NULL)

  observeEvent(input$action, {
    req(bboxRV())
    getbbox <- bboxRV()
    print("Do whatever with bbox after the actionButton is clicked")
    cropedr <- crop(r, getbbox)
    ## Assign cropped raster to reactiveVal
    croppedRaster(cropedr)
  })

  output$cropimg <- renderPlot({
    req(croppedRaster())
    ## Plot cropped raster
    plot(croppedRaster())
  })
}

shinyApp(ui, server)

是否还可以从addProviderTiles()裁剪基础层(即开放街道地图层),以便同时输出Worldclim栅格图层r和OSM?我对OSM数据的图像对象(例如.png)感到满意-它不必是包含地理空间信息的栅格文件。实际上,我认为平铺地图无论如何都使用.png文件,因此它们不会包含地理空间数据。

在此先感谢您的帮助。

0 个答案:

没有答案