我想在 R 中解压并读取来自网络的形状文件,而不依赖于 rgdal。我找到了read.shp
的功能fastshp
软件包显然可以在环境中安装 rgdal 的情况下完成此操作,但是,我在实施时遇到了麻烦。
我想要一个可以解压缩然后读取形状文件的函数,类似于在此找到的内容SO帖子但对于read.shp
功能。我尝试了以下方法但没有成功:
dlshape=function(shploc, format) {
temp=tempfile()
download.file(shploc, temp)
unzip(temp)
shp.data <- sapply(".", function(f) {
f <- file.path(temp, f)
return(read.shp(".", format))
})
}
shp_object<-dlshape('https://www2.census.gov/geo/tiger/TIGER2017/COUNTY/tl_2017_us_county.zip', 'polygon')
Error in read.shp(".", format) : unused argument (format)
我还尝试了以下方法:
dlshape=function(shploc) {
temp=tempfile()
download.file(shploc, temp)
unzip(temp)
shp.data <- sapply(".", function(f) {
f <- file.path(temp, f)
return(read.shp("."))
})
}
shp_object<-dlshape('https://www2.census.gov/geo/tiger/TIGER2017/COUNTY/tl_2017_us_county.zip')
Error in file(shp.name, "rb") : cannot open the connection
In addition: Warning messages:
1: In file(shp.name, "rb") : 'raw = FALSE' but '.' is not a regular file
2: In file(shp.name, "rb") :
Show Traceback
Rerun with Debug
Error in file(shp.name, "rb") : cannot open the connection
我怀疑这与函数中的事实有关read.shp()
我向它提供文件夹名称而不是 .shp 名称(例如readOGR
这有效但不适合read.shp
)。非常感谢任何帮助。