是否有一种从在线源直接读取形状文件到R的方法?



我试图找到一种从在线存储库/文件夹/url直接加载shapefiles (.shp)到R中的全局环境的方法,以便使用geom_sf在ggplot2中制作地块。在第一个实例中,我使用我的谷歌驱动器来存储这些文件,但我理想情况下希望找到一个解决方案,适用于任何具有有效url和适当访问权限的文件夹。

到目前为止,我已经尝试了几个选项,前两个涉及压缩源文件夹在谷歌驱动器上的shapefiles存储,然后下载和解压缩以某种方式。使用一个小的测试shapefile包含了可复制的示例:

  1. 使用utils::download.file()检索压缩文件夹并使用base::system('unzip..')zip::unzip()解压缩(松散地遵循此线程:从ONS下载县Shapefile):
# Create destination data folder (if there isn't one)
if(!dir.exists('data')) dir.create('data')
# Download the zipped file/folder 
download.file("https://drive.google.com/file/d/1BYTCT_VL8EummlAsH1xWCd5rC4bZHDMh/view?usp=sharing", destfile = "data/test_shp.zip")
# Unzip folder using unzip (fails)
unzip(zipfile = "data/test_shp.zip", exdir = "data/test_shp", junkpaths = TRUE)
# Unzip folder using system (also fails)
system("unzip data/test_shp.zip")

如果你不能运行上面的代码,那么供参考的2个错误消息是:

警告消息:在unzip(zipfile = "data/testronghp.zip", exdir = "data/testronghp",:从zip文件中提取

错误1和

中心目录结束签名未找到。或者这个文件不是一个zip文件,或者它构成一个多部分存档的磁盘。在后一种情况下,中央目录和zipfile注释将在上面找到此存档文件的最后一个磁盘。Unzip:在data/testronghp.zip或.zip中找不到zipfile目录数据/testronghp.zip.zip,找不到数据/testronghp.zip.zip,句号

值得注意的是,我甚至不能在R之外手动解压缩这个文件夹,所以我认为download.file()步骤出了问题。

  1. 使用googledrive包:
# Create destination data folder (if there isn't one)
if(!dir.exists('data')) dir.create('data')
# Specify googledrive url:
test_shp = drive_get(as_id("https://drive.google.com/file/d/1BYTCT_VL8EummlAsH1xWCd5rC4bZHDMh/view?usp=sharing"))
# Download zipped folder
drive_download(test_shp, path = "data/test_shp.zip")
# Unzip folder
zip::unzip(zipfile = "data/test_shp.zip", exdir = "data/test_shp", junkpaths = TRUE)
# Load test.shp
test_shp <- read_sf("data/test_shp/test.shp")

这是可行的!

…除了它仍然是一个黑客的解决方案,这需要我压缩,下载,解压缩,然后使用一个单独的函数(如sf::read_sfst_read)读取数据到我的全局环境。而且,由于它使用的是googledrive包,它只适用于存储在这个系统中的文件(不包括OneDrive, DropBox和其他url)。

  1. 我也尝试了sf::read_sf,st_readfastshp::read.shp直接在文件夹url上,但这些方法都失败了。

所以,我的问题:是否有一个工作流程读取在线存储的形状文件直接到R或我应该停止寻找?如果没有,但是有一种方法可以扩展我上面的解决方案(2),而不是googledrive,我也很感激任何关于这方面的建议!

注意:我还应该补充说,我故意忽略了任何需要rgdal包的选项,因为它即将永久退役,所以我正在寻找至少在某种程度上是未来的选项(我理解所有的包都会在某个时候退出地图)。提前感谢!

我最近遇到了一个类似的问题,不得不直接从Dropbox读取shapefiles到r

因此,此解决方案仅适用于Dropbox的情况。

您需要做的第一件事是创建一个可刷新令牌使用rdrop2 Dropbox,鉴于最近的变化从单一令牌使用Dropbox限制到4小时。你可以关注这篇文章。

一旦你设置了你的可刷新令牌,识别所有文件在你的空间数据文件夹在Dropbox上使用:

shp_files_on_db<- drop_dir("Dropbox path/to your/spatial data/", dtoken = refreshable_token) %>% 
filter(str_detect(name, "adm2"))

我的'空间数据'文件夹包含两组形状文件- adm1和adm 2。我用上面的代码只选择那些与adm2有关。

然后在'spatial data'文件夹中创建一个包含shp、csv、shx、dbf、cpg文件名称的向量,如下所示:

shp_filenames<- shp_files_on_db$name

我选择将shapefiles读入临时目录,避免将文件存储在磁盘上——这在Shiny实现中也很有用。我创建这个临时目录,如下所示:

# create a new directory under tempdir
dir.create(dir1 <- file.path(tempdir(), "testdir"))
#If needed later on, you can delete this temporary directory 
unlink(dir1, recursive = T)
#And test that it no longer exists
dir.exists(dir1)

现在下载Dropbox文件到这个临时目录:

for (i in 1: length(shp_filenames)){

drop_download(paste0("Dropbox path/to your/spatial data/",shp_filenames[i]),
dtoken = refreshable_token,
local_path = dir1)

}

最后,按如下方式读取shapefile:

#path to the shapefile in the temporary directory
path1_shp<- paste0(dir1, "/myfile_adm2.shp")
#reading in the shapefile using the sf package - a recommended replacement for rgdal
shp1a <- st_read(path1_shp)

最新更新