gpt4 book ai didi

R: 无法从 url 下载 .zip

转载 作者:行者123 更新时间:2023-12-03 07:56:31 26 4
gpt4 key购买 nike

我正在尝试从 Internet 下载以下 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)

当我转到临时文件的位置时,我看到它是这样的:“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: 成功

    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 中的一段:

Note that stray files in data source directories (such as *.dbf) may lead to spurious errors that accompanying '*.shp' are missing.

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

我不相信 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

关于R: 无法从 url 下载 .zip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75931975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com