使用 R 从 Dropbox 导入 .asc 文件

Importing .asc files from Dropbox using R

我有一堆 .asc 光栅文件需要通过 Dropbox 共享链接访问。出于某种原因,我无法将文件读入 R。我尝试了 raster()read.csv()read.table() 函数,但所有 return 错误。 R 声称文件不存在,而 Dropbox 共享链接实际上包含 .asc 文件。对我哪里出错有什么建议吗?

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create a RasterLayer object from this file. (file does not exist)

我的代码

library(raster)

# define projection
RD_Proj4 = '+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +towgs84=565.2369,50.0087,465.658,-0.406857330322398,0.350732676542563,-1.8703473836068,4.0812 +no_defs'

# The share link for the Dropbox .asc files
pm10 <- "https://www.dropbox.com/s/vkvbabwcwdk2r3q/conc_pm10_2019.asc?dl=0"
no2 <- "https://www.dropbox.com/s/foq2yufvi3694gt/conc_no2_2019.asc?dl=0"

# Trying to fetch the files from Dropbox
rasterData <- raster(pm10, crs = RD_Proj4)

raster() 似乎无法从 URL 中读取数据。您需要先下载该文件。您可以使用 download.file() 来做到这一点。但是请注意,dl=0 部分实际上并不指向文件中的数据,它指向您在 dropbox 上查看以预览文件的 HTML 页面。您需要在 URL 中使用 dl=1 才能真正下载数据。尝试

pm10 <- "https://www.dropbox.com/s/vkvbabwcwdk2r3q/conc_pm10_2019.asc?dl=1"
download.file(pm10, "pm10.asc")
rasterData <- raster("pm10.asc", crs = RD_Proj4)

@MrFlick 指出了什么是正确的URL,你可以用它来做

library(raster)
RD_Proj4 = '+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +towgs84=565.2369,50.0087,465.658,-0.406857330322398,0.350732676542563,-1.8703473836068,4.0812 +no_defs'

pm10 <- "https://www.dropbox.com/s/vkvbabwcwdk2r3q/conc_pm10_2019.asc?dl=1"
r <- raster(pm10, crs=RD_Proj4)
r 

#class      : RasterLayer 
#dimensions : 320, 280, 89600  (nrow, ncol, ncell)
#resolution : 1000, 1000  (x, y)
#extent     : 0, 280000, 3e+05, 620000  (xmin, xmax, ymin, ymax)
#crs        : +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.2369,50.0087,465.658,-0.406857330322398,0.350732676542563,-1.8703473836068,4.0812 +units=m +no_defs 
#source     : https://www.dropbox.com/s/vkvbabwcwdk2r3q/conc_pm10_2019.asc?dl=1 
#names      : conc_pm10_2019.asc.dl.1