簡體   English   中英

R Leaflet GeoJSON着色

[英]R Leaflet GeoJSON Coloring

我還在研究這個R Leaflet自我項目,我正在嘗試在北卡羅來納州羅利市的Wake County地區的一些多邊形中着色。 下面是我想要着色的圖像。

https://imgur.com/a/xdvNLvM

基本上我試圖讓每個多邊形的顏色不同。 我嘗試過addPolygons,但我想我沒有正確的Polygon數據。 我看過顏色分類,但我似乎沒有想法。 以下是我的代碼。 我甚至試圖取消GeoJSON數據並創建一個因子調色板,但這似乎不起作用。

library(shiny)
library(leaflet.extras)
library(geojsonio)
library(rgdal)

dataurl <- 'https://opendata.arcgis.com/datasets/f5c3b84a6fcc499d8f9ece78602258eb_0.geojson'
data <- geojson_read(dataurl, method = 'web', parse = FALSE, what = 'list')

wake <- readOGR(dataurl)
wake$zips <- factor(sample.int(39L, nrow(wake), TRUE))

#bikedata <- 'D:/bicycle-crash-data-chapel-hill-region.geojson'
#bike <- geojson_read(bikedata)

vtdata <- 'http://geodata.vermont.gov/datasets/4c206846699947429df59c8cb552ab5c_11.geojson'
vt <- geojson_read(vtdata)

factpal <- colorFactor(topo.colors(39), wake$zips)

ui <- shinyUI(
    fluidPage(
        leafletOutput("map", width = "100%", height = "900px")
    )
)

server <- function(input, output) {

    wakegeojson <- reactive({
        data
    })

    #bikegeojson <- reactive({
    #    bike
    #})

    vtgeojson <- reactive({
        vt
    })

    output$map <- renderLeaflet({
        leaflet() %>%
            addTiles() %>%
            setView(-93.65, 42.0285, zoom = 4)

    })

    observe({
        leafletProxy("map") %>%
            addWMSTiles("http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
            layers = "nexrad-n0r-900913",
            options = WMSTileOptions(format = "image/png", transparent = TRUE),
            attribution = "") %>%
            addGeoJSON(wakegeojson(), weight = 3, fill = factpal) %>%
            #addGeoJSON(bikegeojson()) %>%
            addGeoJSON(vtgeojson(), fill = FALSE, color = "black")
    })
}

app <- shinyApp(ui = ui, server = server)
runApp(app, launch.browser = TRUE)

我想我需要更多地探索addPolygons功能,但我不確定如何做到這一點或如何解析/取消我的GeoJSON數據以完成填充不同顏色的Wake County Zipcodes。 任何幫助總是受到贊賞。 謝謝。

我會切換到sf 您可以直接加載geojson並生成Multipolygon和Multilinestring對象,該對象的讀取速度也比readOGR

然后你可以把這些對象放在addPolygonsaddPolylines

以下示例應該有效:

library(shiny)
library(leaflet.extras)
library(geojsonio)
library(rgdal)
library(sf)

dataurl <- 'https://opendata.arcgis.com/datasets/f5c3b84a6fcc499d8f9ece78602258eb_0.geojson'
wake <- st_read(dataurl)
wake$zips <- factor(sample.int(39L, nrow(wake), TRUE))

vtdata <- 'http://geodata.vermont.gov/datasets/4c206846699947429df59c8cb552ab5c_11.geojson'
vt <- st_read(vtdata)

factpal <- colorFactor(topo.colors(39), wake$zips)

ui <- shinyUI(
  fluidPage(
    leafletOutput("map", width = "100%", height = "900px")
  )
)

server <- function(input, output) {

  wakegeojson <- reactive({
    wake
  })

  vtgeojson <- reactive({
    vt
  })

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addPolygons(data=wakegeojson(), color=factpal(wake$zips)) %>%
      addPolylines(data=vtgeojson(), color="red")
  })
}

app <- shinyApp(ui = ui, server = server)
runApp(app, launch.browser = TRUE)

暫無
暫無

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

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