【问题标题】:Trouble reading geojson data into R将geojson数据读入R时遇到问题
【发布时间】:2019-04-28 18:02:37
【问题描述】:

我正在尝试读取 geojson 数据以与 leaflet 包一起使用。我尝试过多种方式读取数据,但我无法将其绘制出来。数据为here

  • 我尝试使用this StackOverflow post 中的代码。第一个街区给了我一张空白地图。第二个给了我一张没有添加新线的地图。

  • 我还尝试了here 的代码。我是新手,所以任何帮助将不胜感激。

【问题讨论】:

    标签: r geojson


    【解决方案1】:

    这是你想要的吗?

    library(leaflet)
    library(jsonlite)
    download.file("https://opendata.arcgis.com/datasets/772f3621fa354ec9abf3ba33f3ace59e_0.geojson", "RPD_Sections.geojson")
    x = fromJSON("RPD_Sections.geojson", FALSE)
    leaflet() %>% addTiles() %>% addGeoJSON(x) %>% setView(
        lng = mean(vapply(x$features[[1]]$geometry$coordinates[[1]], "[[", 1, 1)),
        lat = mean(vapply(x$features[[1]]$geometry$coordinates[[1]], "[[", 1, 2)),
        zoom = 12)
    

    【讨论】:

    • 是的!非常感谢!我有几个不同的 geojson 文件,这对他们都有效!
    【解决方案2】:

    更简洁一些,但给出相同的结果

    library(sf)
    library(leaflet)
    sf <- sf::st_read("https://opendata.arcgis.com/datasets/772f3621fa354ec9abf3ba33f3ace59e_0.geojson")
    
    leaflet() %>%
      addTiles() %>%
      addPolygons(data = sf)
    
    

    这也假设您的 GeoJSON 仅包含多边形

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 2015-10-19
      相关资源
      最近更新 更多