R: 无法从 url 下载 .zip

Jam*_*s S 2 zip r

我正在尝试从互联网下载以下 zip 并从中提取 .shp 文件: https://www.wpc.ncep.noaa.gov/archives/ero/20181227/shp_94e_2018122701.zip

在 .zip 文件中,我尝试提取 .shp 文件:94e2701.shp

(我想在 R 中执行此操作,以便我可以自动执行此过程来下载多个日期的 .shp 文件)。

根据一些阅读,这是我尝试过的代码:

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)
Run Code Online (Sandbox Code Playgroud)

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

我在这里做错了什么?非常感谢任何帮助或指导!谢谢!

r2e*_*ans 5

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

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

  1. 只是94e2701.shp:错误

  2. .shp.prj:错误

  3. .shp.dbf文件:错误

  4. .shpand .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
    
    Run Code Online (Sandbox Code Playgroud)
  5. .shp.shx、 和.dbf:与 4 相同,无CRS

  6. .shp、、、和:成功.shx.dbf.prj

    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
    
    Run Code Online (Sandbox Code Playgroud)

顺便说一句,让我检查这一点的是以下一段?sf::st_read

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

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

我不相信 R 中有一种方法unz(..)可以访问所有文件。如果您不想将它们解压缩到当前目录(只需“查看”文件并稍后丢弃),那么您可以创建一个临时目录,解压缩到该目录,然后从那里打开文件。

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
Run Code Online (Sandbox Code Playgroud)