我想扩展这个功能。截至目前,该函数从网络下载并解压形状文件。我想实现 'rgdal' 将文件读入 R。
library(rgdal)
dlshape=function(location) {
temp=tempfile()
download.file(location, temp)
unzip(temp)
}
我在SO上找到了以下代码,但我没有成功地修改它。看来该函数仍然查看解压缩的第一个文件,而不是 grep 查找以 .shp 扩展名结尾的文件。
read.csv.zip <- function(zipfile, ...) {
# Create a name for the dir where we'll unzip
zipdir <- tempfile()
# Create the dir using that name
dir.create(zipdir)
# Unzip the file into the dir
unzip(zipfile, exdir=zipdir)
# Get a list of csv files in the dir
files <- list.files(zipdir)
files <- files[grep("\\.csv$", files)]
# Create a list of the imported csv files
csv.data <- sapply(files, function(f) {
fp <- file.path(zipdir, f)
return(read.csv(fp, ...))
})
return(csv.data)}
dlshape=function(shploc, shpfile) {
temp=tempfile()
download.file(shploc, temp)
unzip(temp, exdir = temp)
files<-list.files(temp)
files <- files[grep("\\.shp$", files)]
shp.data <- sapply(files, function(f) {
fp <- file.path(zipdir, f)
return(ReadOGR(fp, ...))
})
return(shp.data)}
有人可以帮我解决这个问题吗?我很乐意欣赏。
编辑:包括我的改编,以澄清“改编”部分。
尝试这个。
dlshape=function(shploc, shpfile) {
temp=tempfile()
download.file(shploc, temp)
unzip(temp)
shp.data <- sapply(".", function(f) {
fp <- file.path(temp, f)
return(readOGR(".",shpfile))
})
}
x = dlshape(shploc="http://www.location.com/file_name.zip", "file_name")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)