我正在try 从Internet下载以下压缩文件,并从中解压一个.shp文件: https://www.wpc.ncep.noaa.gov/archives/ero/20181227/shp_94e_2018122701.zip

在.zip文件中,我try 解压.shp文件:94e2701.shp

(我想在R中这样做,这样我就可以自动化这个过程来下载几个日期的.shp文件).

根据一些阅读,以下是我try 过的代码:

shp_url = "https://www.wpc.ncep.noaa.gov/archives/ero/20181227/shp_94e_2018122701.zip"
tmp = tempfile()

download.file(shp_url,tmp,mode="wb")
# I have also tried without the "mode" argument but have gotten the same result

f_name = "94e2701.shp"
data <- sf::st_read(unz(tmp,f_name))
# Error: Cannot open "3"; The file doesn't seem to exist.
unlink(tmp)

当我转到临时文件的位置时,我看到它是这样的:"file1b9026cd6821",但它不是.zip,所以我无法从其中提取任何内容/进入其中.

我到底做错了什么?任何帮助或指导都是非常感谢的!谢谢!

推荐答案

BLUF:您需要的不仅仅是.shp个文件.解压缩更多(所有)的文件,您会得到不同的结果.

对于下面的每个文件,我在命令行上使用unzip来仅解压缩该步骤中的文件.在此期间,我删除未测试的文件.我不相信在R到unz(..)中有一种方法可以到达所有的文件.

  1. 只有94e2701.shp:错误

  2. .shp.prj:错误

  3. .shp.dbf文件:错误

  4. .shp.shx:部分成功,不填充CRS

    data <- sf::st_read("94e2701.shp")
    # Reading layer `94e2701' from data source `C:\Users\r2\AppData\Local\Temp\Rtmpqoj4GE\94e2701.shp' using driver `ESRI Shapefile'
    # Simple feature collection with 2 features and 0 fields
    # Geometry type: POLYGON
    # Dimension:     XY
    # Bounding box:  xmin: -100.4 ymin: 28.27 xmax: -91.4 ymax: 39.77
    # CRS:           NA
    
  5. .shp.shx.dbf:与4相同,编号CRS

  6. .shp.shx.dbf.prj:104

    data <- sf::st_read("94e2701.shp")
    # Reading layer `94e2701' from data source `C:\Users\r2\AppData\Local\Temp\Rtmpqoj4GE\94e2701.shp' using driver `ESRI Shapefile'
    # Simple feature collection with 2 features and 7 fields
    # Geometry type: POLYGON
    # Dimension:     XY
    # Bounding box:  xmin: -100.4 ymin: 28.27 xmax: -91.4 ymax: 39.77
    # Geodetic CRS:  GCS_Sphere_EMEP
    

顺便说一句,让我判断这一点的是每?sf::st_read段中就有一段:

请注意,数据源目录(如*.dbf)中的零散文件可能会导致伴随‘*.shp’丢失的虚假错误.

这让我想知道目录中是否存在其他文件导致了您的问题.

我不相信有一种方法在R到unz(..),以达到所有的文件.如果您不想将它们解压缩到当前目录中(只需"查看"这些文件,然后将其丢弃),那么您可以创建一个temp directory,将其解压缩到其中,然后从那里打开文件.

dir.create(td <- tempfile())
unzip(tmp, exdir = td)
data <- sf::st_read(file.path(td, f_name))
# Reading layer `94e2701' from data source `C:\Users\r2\AppData\Local\Temp\Rtmpqoj4GE\file185581a1d4d01\94e2701.shp' using driver `ESRI Shapefile'
# Simple feature collection with 2 features and 7 fields
# Geometry type: POLYGON
# Dimension:     XY
# Bounding box:  xmin: -100.4 ymin: 28.27 xmax: -91.4 ymax: 39.77
# Geodetic CRS:  GCS_Sphere_EMEP

R相关问答推荐

多重RHS固定估计

更改默认系列1以更改名称

如何通过Docker部署我的shiny 应用程序(多个文件)

以更少间隔的较小表中的聚合离散频率表

在R gggplot2中是否有一种方法将绘图轴转换成连续的 colored颜色 尺度?

R Read.table函数无法对制表符分隔的数据正常工作

使用rvest从多个页面抓取时避免404错误

如何用书面利率绘制geom_bar图

将选定的索引范围与阈值进行比较

安全地测试文件是否通过R打开

在纵向数据集中创建新行

从线的交点创建面

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

具有自定义仓位限制和计数的GGPLATE直方图

GOGPLATE geom_boxploy色彩疯狂

conditionPanel不考虑以下条件

如何在GGPlot中控制多个图例和线型

如果缺少时间,如何向日期-时间列添加时间

如何在分组蜂群小区中正确定位标签

移除y轴断开的geom_bar图的外框