【问题标题】:Read GeoJSON file using SF library?使用 SF 库读取 GeoJSON 文件?
【发布时间】:2020-10-30 03:26:48
【问题描述】:

我一天前刚开始使用 R,我正在尝试使用 SF 库读取 geojson 文件,但我不确定正确的方法是什么。

library(dplyr)
library(sf)
geo <- system.file('/my/path/zones.geojson', package = 'sf')
st_read(geo)

当我运行这些代码行时,我收到错误消息:

Cannot open data source
Error in CPL_read_ogr(dsn, layer, as.character(options), quiet, type,  :
  Open failed.

请告诉我如何更改它,谢谢大家!

【问题讨论】:

    标签: r geojson


    【解决方案1】:

    sf 包无法原生读取 geojson。但是,有一个名为 geojsonsf 的包将 geojson 读取到您可以在 sf 中使用的数据中。

    显然我没有你的geojson文件可以使用,但是包本身带有一些示例数据,以便我可以向你展示原理:

    install.packages("geojsonsf")
    library(geojsonsf)
    library(ggplot2)
    
    geo <- geojson_sf("C:/R/R-3.6.2/library/geojsonsf/examples/geo_melbourne.geojson")
    
    ggplot(geo) + geom_sf(aes(fill = SA2_NAME)) + guides(fill = guide_none())
    

    【讨论】:

    • 我不知道自从你写了你的答案后这是否已经改变了,但是sf 可以直接读取geojson:sf::st_read( geojsonsf::geo_melbourne )(但geojsonsf 仍然可以更快地进行基准测试)
    猜你喜欢
    • 1970-01-01
    • 2019-02-17
    • 2018-05-11
    • 1970-01-01
    • 2012-05-09
    • 2021-10-21
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多