【问题标题】:How to read & plot from .shp files using sf package in r?如何使用 r 中的 sf 包从 .shp 文件中读取和绘图?
【发布时间】:2021-07-25 08:37:55
【问题描述】:

我是 geospatial 数据的新手,尝试使用 .shp 文件进行绘图,但出现错误。

这个.shp文件中的几何类型是LINESTRING,这似乎与我在使用sf之前绘制的MULTIPOLYGON不同

形状文件来源: https://github.com/johnsnow09/covid19-df_stack-code/blob/main/in_country_boundaries.shp

shapefile的原始来源: https://github.com/wri/wri-bounds/blob/master/dist/in_countries.zip

预期结果:

代码尝试:

library(tidyverse)
library(sf)

ind_global <- sf::read_sf("path/in_country_boundaries.shp") 
ind_global

输出

Simple feature collection with 412 features and 9 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -141.0056 ymin: -54.88624 xmax: 140.9776 ymax: 70.07531
Geodetic CRS:  WGS 84
ind_global %>% 
  st_as_sf() %>% 
  ggplot() +
  geom_sf()

st_cast.POINT(X[[i]], ...) 中的错误:无法创建 MULTILINESTRING 起点

我需要以其他方式处理LINESTRINGgeometry .shp 文件吗?

【问题讨论】:

    标签: r ggplot2 geospatial sf


    【解决方案1】:

    删除st_as_sf() %&gt;% 后代码运行良好。我已经从https://github.com/wri/wri-bounds/blob/master/dist/in_countries.zip 下载了 shapefile,它只是 MULTIPOLYGON。

    library(tidyverse)
    library(sf)
    
    ind_global <- sf::read_sf("...\\in_countries.shp") 
    ind_global
    
    ind_global %>% 
      ggplot() +
      geom_sf()
    

    【讨论】:

    • 感谢@Bappa Das,我不知道为什么之前提取的 .shp 文件存在问题。再次下载/提取后得到多面体文件,它按你说的那样工作。再次感谢您的帮助。欣赏!!
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2012-04-02
    • 1970-01-01
    相关资源
    最近更新 更多