download - 为什么我不能让 st_read 从压缩文件中打开 shapefile? (如果保存在本地 Onedrive 文件夹中但未在 R 中解压缩,则会读取 map )

标签 download unzip shapefile sf

我正在创建一份报告,其中包含美国俄克拉荷马州的矢量图。我找到了我需要的 shapefile,但它们位于压缩文件中。

我在 Stackoverflow(此处链接)上发现了一个关于下载和提取压缩数据的类似问题,并使用了那里建议的方法。但是,我收到有关无法在压缩文件中找到该文件的错误消息——即使它存在。原始问题链接如下:

https://stackoverflow.com/questions/3053833/using-r-to-download-zipped-data-file-extract-and-import-data#:~:text=8%20Answers.%201%20Create%20a%20temp.%20file%20name,simpler%29%20looks%20like%20temp%20%3C-%20tempfile%28%29%20download.%20

这是我使用的 R 代码,其中包含链接到压缩矢量数据的链接:

library(tidyverse)
library(sf)
library(tmap)

ok_zip_code_tempfile <- tempfile()
download.file("https://okmaps.org/OGI/Downloads/okzip/OGR-SHP-ZIP/okzip__state_EPSG_32124.zip", ok_zip_code_tempfile)
ok_zip_map <- st_read(unzip(ok_zip_code_tempfile, files="okzip.shp"))
unlink(ok_zip_code_tempfile)

ok_municipalities_tempfile <- tempfile()
download.file("https://www.odot.org/hqdiv/p-r-div/maps/shp-files/munibnd.zip", ok_municipalities_tempfile)
ok_municipalities <- st_read(unzip(ok_municipalities_tempfile, files='munibnd.shp'))
unlink(ok_municipalities_tempfile)

运行这些时,出现以下错误:

> Error: Cannot open
> "C:\Users\[myname]\OneDrive\Desktop\Documents\okzip.shp"; The source
> could be corrupt or not supported. See `st_drivers()` for a list of
> supported formats. In addition: Warning message: In CPL_read_ogr(dsn,
> layer, query, as.character(options), quiet,  :   GDAL Error 4: Unable
> to open C:\Users\[myname]\OneDrive\Desktop\Documents\okzip.shx or
> C:\Users\[myname]\OneDrive\Desktop\Documents\okzip.SHX. Set
> SHAPE_RESTORE_SHX config option to YES to restore or create it.
> 
> 
> Error: Cannot open
> "C:\Users\[myname]\OneDrive\Desktop\Documents\munibnd.shp"; The source
> could be corrupt or not supported. See `st_drivers()` for a list of
> supported formats. In addition: Warning message: In CPL_read_ogr(dsn,
> layer, query, as.character(options), quiet,  :   GDAL Error 4: Unable
> to open C:\Users\[myname]\OneDrive\Desktop\Documents\munibnd.shx or
> C:\Users\[myname]\OneDrive\Desktop\Documents\munibnd.SHX. Set
> SHAPE_RESTORE_SHX config option to YES to restore or create it.

更令人困惑的是,如果我在 OneDrive 的桌面上保存一份副本,那么文件会正确加载:

ok_zip_map <- st_read("C:\\Users\\[myname]\\OneDrive\\Desktop\\OK_Data\\okzip.shp")

> Reading layer `okzip' from data source
> `C:\Users\[myname]\OneDrive\Desktop\OK_Data\okzip.shp' using driver
> `ESRI Shapefile' Simple feature collection with 682 features and 4
> fields geometry type:  MULTIPOLYGON dimension:      XY bbox:          
> xmin: -103.0025 ymin: 33.61579 xmax: -94.43066 ymax: 37.00231
> geographic CRS: WGS 84
 
 
ok_municipalities <- st_read("C:\\Users\\[myname]\\OneDrive\\Desktop\\OK_Data\\munibnd.shp")

> Reading layer `munibnd' from data source
> `C:\Users\[myname]\OneDrive\Desktop\OK_Data\munibnd.shp' using driver
> `ESRI Shapefile' Simple feature collection with 1414 features and 8
> fields geometry type:  MULTIPOLYGON dimension:      XY bbox:          
> xmin: -102.5219 ymin: 33.76577 xmax: -94.43182 ymax: 36.99937 CRS:    
> NA

但是,如果我将相同的文件保存在本地计算机上(而不是 Onedrive 文件夹中),则会出现错误。

ok_zip_map <- st_read("C:\\Users\\[myname]\\Documents\\okzip.shp")

> Error: Cannot open "C:\Users\[myname]\Documents\okzip.shp"; The source
> could be corrupt or not supported. See `st_drivers()` for a list of
> supported formats. In addition: Warning message: In CPL_read_ogr(dsn,
> layer, query, as.character(options), quiet,  :   GDAL Error 4: Unable
> to open C:\Users\[myname]\Documents\okzip.shx or
> C:\Users\[myname]\Documents\okzip.SHX. Set SHAPE_RESTORE_SHX config
> option to YES to restore or create it.
 
 
ok_municipalities <- st_read("C:\\Users\\[myname]\\Documents\\munibnd.shp") 

> Error: Cannot
> open "C:\Users\[myname]\Documents\munibnd.shp"; The source could be
> corrupt or not supported. See `st_drivers()` for a list of supported
> formats. In addition: Warning message: In CPL_read_ogr(dsn, layer,
> query, as.character(options), quiet,  :   GDAL Error 4: Unable to open
> C:\Users\[myname]\Documents\munibnd.shx or
> C:\Users\[myname]\Documents\munibnd.SHX. Set SHAPE_RESTORE_SHX config
> option to YES to restore or create it

我还尝试在另一台计算机(Mac)上运行原始代码(将链接下载到一个临时文件中并在 R 中解压缩),但失败并出现类似错误。

理想情况下希望生成不需要依赖文件(即 map 的本地副本)的报告,因为它将由其他人在他们自己的计算机上运行。有人可以帮助我了解可能发生的情况吗?

提前感谢您提供的任何指导!

托尼

最佳答案

this answer 中所述,这里的关键是解压下载的文件,使用sf::read_sfsf::st_read读取解压后的文件夹。这是我之前使用的包装函数:

library(sf)

read_shape_URL <- function(URL){
  cur_tempfile <- tempfile()
  download.file(url = URL, destfile = cur_tempfile)
  out_directory <- tempfile()
  unzip(cur_tempfile, exdir = out_directory)
  
  st_read(dsn = out_directory) #read_sf also works here
}

使用它,我们可以加载压缩的 shapefile:

ok_zip_map <- read_shape_URL("https://okmaps.org/OGI/Downloads/okzip/OGR-SHP-ZIP/okzip__state_EPSG_32124.zip")

关于download - 为什么我不能让 st_read 从压缩文件中打开 shapefile? (如果保存在本地 Onedrive 文件夹中但未在 R 中解压缩,则会读取 map ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64850643/

相关文章:

linux - 从 Linux 中的 tgz 中提取未压缩的文件

java - 使用 java.util.ZipFile 在同一层次结构中解压缩 zip 文件

java - 从 ESRI shapefile 'one-by-one' 读取几何图形

Java 二进制文件下载提前停止

html - 如何制作 HTML 链接,使用户下载 .html 文件

java - 从下载链接发现文件名和扩展名

d3.js - 如何/在哪里获取非美国国家/地区的州、省和行政区的 geoJSON 数据?

svn - 下载 SVN 存储库/文件夹的在线服务?

c# - 压缩和解压缩文件

java - 如何将R包 "spatstat"中的示例数据集转换成shapefile