从 github 下载 returns '404 未找到'加上错误的下载大小

downloading from github returns '404 not found' plus wrong download size

我正在尝试使用以下代码从 github 下载 .tif 文件:

file1 <- "modis_mcd12q1_umd_2010.tif"

dir.create("data", showWarnings = FALSE)


if (!file.exists(file.path("data", file1))) {
  download.file(paste0("https://github.com/lime-n/data/blob/main/modis_mcd12q1_umd_2010.tif", file1),
                file.path("data", file1), mode = "wb")
}

但是,它returns这个错误:

trying URL 'https://github.com/lime-n/data/blob/main/modis_mcd12q1_umd_2010.tifmodis_mcd12q1_umd_2010.tif' Error in download.file(paste0("https://github.com/lime-n/data/blob/main/modis_mcd12q1_umd_2010.tif", : cannot open URL 'https://github.com/lime-n/data/blob/main/modis_mcd12q1_umd_2010.tifmodis_mcd12q1_umd_2010.tif' In addition: Warning message: In download.file(paste0("https://github.com/lime-n/data/blob/main/modis_mcd12q1_umd_2010.tif", : cannot open URL 'https://github.com/lime-n/data/blob/main/modis_mcd12q1_umd_2010.tifmodis_mcd12q1_umd_2010.tif': HTTP status was '404 Not Found'

有什么办法可以克服这个问题?

编辑:

感谢@MrFlick,解决了之前的问题

但是,当我下载文件时,它不是原来的大小。当使用上面代码的更正版本时,它从 github 手动下载时的 8.5mb 变为 ~85kb:

if (!file.exists(file.path("data", file1))) {
  download.file(paste0("https://github.com/lime-n/data/blob/main/", file1),
                file.path("data", file1), mode = "wb")
}

我试着把它读成你想象的 raster,它不起作用。

你的URL还是不对。您正在下载的 84K 是 GitHub 页面的 html,而不是二进制文件本身。您需要:

if (!file.exists(file.path("data", file1))) {
  download.file(paste0("https://raw.githubusercontent.com/lime-n/data/main/", file1),
                file.path("data", file1), mode = "wb")
}
#. trying URL 'https://raw.githubusercontent.com/lime-n/data/main/modis_mcd12q1_umd_2010.tif'
#> Content type 'application/octet-stream' length 8791730 bytes (8.4 MB)
#> downloaded 8.4 MB