是否有一种将ShapeFile直接从在线来源中读取为R中的方法?

发布于 2025-02-11 21:59:14 字数 2516 浏览 1 评论 0原文

我正在尝试找到一种从在线存储库/文件夹/url中加载ShapeFiles(.shp)的方法,直接在R中加入我的全局环境,目的是使用geom_sf在GGPLOT2中制作图。首先,我使用Google驱动器来存储这些文件,但理想情况下,我希望找到一个解决方案,该解决方案与具有有效URL且适当访问权限的任何文件夹一起使用。

到目前为止,我已经尝试了一些选项,前两个涉及在Google Drive上汇总源文件夹,然后存储ShapeFiles然后以某种方式下载和解压缩。已包含使用小型测试shapefile的可复制示例:

  1. 使用utils :: download.file()用于检索压缩文件夹并使用base base base :: System('unzip ..')<)< /code>或zip :: unzip()(在此线程中宽松地遵循:从ONS下载County 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")

如果您无法运行上述代码,则FYI,2个错误消息是:

警告消息: 在unzip(zipfile =“ data/test_shp.zip”,exdir =“ data/test_shp”,: 从zip文件提取

中央目录签名中的错误1。这个文件不是 Zipfile或它构成一个多部分档案的一个磁盘。在 后一种情况将在中央目录和Zipfile评论中找到 该存档的最后一个磁盘。 UNZIP:在数据/test_shp.zip或 data/test_shp.zip.zip。

data/test_shp.zip.zip,并且找不到 代码> download.file()步骤。

  1. 使用GoogleRive软件包:
# 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")

这可以!

...除了它仍然是一个骇客的解决方法,它要求我zip,下载,解压缩,然后使用单独的功能(例如sf :: read_sfst_read)将数据读取到我的全球环境中。而且,由于它使用GoogleRive软件包,因此仅适用于存储在此系统中的文件(不是OneDrive,Dropbox和其他URL)。

  1. 我还尝试了sf :: read_sfst_readfastshp :: read.shp直接在文件夹URL上一个人可能期待。

因此,我的问题是:是否有一个直接在网上存储在R中的ShapeFile的工作流程,还是我应该停止寻找?如果没有,但是有一种方法可以扩展我的上述解决方案(2)超越Googledrive,我也很感谢它的任何提示!

注意:我还应该补充说,我故意忽略了任何需要包装rgdal的选择在某个时候掉下地图)。提前致谢!

I am trying to find a way of loading shapefiles (.shp) from an online repository/folder/url directly into my global environment in R, for the purpose of making plots in ggplot2 using geom_sf. In the first instance I'm using my Google Drive to store these files but I'd ideally like to find a solution that works with any folder with a valid url and appropriate access rights.

So far I have tried a few options, the first 2 involving zipping the source folder on Google Drive where the shapefiles are stored and then downloading and unzipping in some way. Have included reproducable examples using a small test shapefile:

  1. Using utils::download.file() to retrieve the compressed folder and unzipping using either base::system('unzip..') or zip::unzip() (loosely following this thread: Downloading County Shapefile from ONS):
# 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")

If you can't run the above code then FYI the 2 error messages are:

Warning message:
In unzip(zipfile = "data/test_shp.zip", exdir = "data/test_shp", :
error 1 in extracting from zip file

AND

End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of data/test_shp.zip or
data/test_shp.zip.zip, and cannot find data/test_shp.zip.ZIP, period.

Worth noting here that I can't even manually unzip this folder outside R so I think there's something going wrong with the download.file() step.

  1. Using the googledrive package:
# 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")

And that works!

...Except it's still a hacky workaround, which requires me to zip, download, unzip and then use a separate function (such as sf::read_sf or st_read) to read in the data into my global environment. And, as it's using the googledrive package it's only going to work for files stored in this system (not OneDrive, DropBox and other urls).

  1. I've also tried sf::read_sf, st_read and fastshp::read.shp directly on the folder url but those approaches all fail as one might expect.

So, my question: is there a workflow for reading shapefiles stored online directly into R or should I stop looking? If there is not, but there is a way of expanding my above solution (2) beyond googledrive, I'd appreciate any tips on that too!

Note: I should also add that I have deliberately ignored any option requiring the package rgdal due to its imminient permanent retirement and so am looking for options that are at least somewhat future-proof (I understand all packages drop off the map at some point). Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2025-02-18 21:59:14

我最近遇到了一个类似的问题,必须直接从Dropbox中读取ShapeFiles到R中。

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

您需要做的第一件事是使用RDROP2为Dropbox创建一个可刷新的令牌,鉴于Dropbox的最新更改将单个令牌使用限制为4小时。您可以关注此 a>。

设置刷新令牌后,请使用以下方式识别Dropbox上空间数据文件夹中的所有文件:

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

我的“空间数据”文件夹包含两组ShapeFiles -ADM1和ADM 2。我使用上述代码仅选择与之相关的代码ADM2。

然后创建SHP,CSV,SHX,DBF,CPG文件的名称的向量,如下所示:

shp_filenames<- shp_files_on_db$name

我选择将ShapeFiles读入临时目录中,避免需要存储文件。在我的磁盘上 - 也可用于闪亮的实现。我创建此临时目录如下:

# 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)

I ran into a similar problem recently, having to read in shapefiles directly from Dropbox into R.

As a result, this solution only applies for the case of Dropbox.

The first thing you will need to do is create a refreshable token for Dropbox using rdrop2, given recent changes from Dropbox that limit single token use to 4 hours. You can follow this SO post.

Once you have set up your refreshable token, identify all the files in your spatial data folder on Dropbox using:

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

My 'spatial data' folder contained two sets of shapefiles – adm1 and adm 2. I used the above code to choose only those associated with adm2.

Then create a vector of the names of the shp, csv, shx, dbf, cpg files in the 'spatial data' folder, as follows:

shp_filenames<- shp_files_on_db$name

I choose to read in shapefiles into a temporary directory, avoiding the need to have to store the files on my disk – also useful in a Shiny implementation. I create this temporary directory as follows:

# 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)

Now download the Dropbox files to this temporary directory:

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)
  
}

And finally, read in your shapefile as follows:

#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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文