【问题标题】:Download and read shapefile function in R在 R 中下载和读取 shapefile 函数
【发布时间】:2013-09-23 19:56:43
【问题描述】:

我想扩展这个功能。截至目前,该功能从网络下载并解压缩形状文件。我想实现 '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)}

有人可以帮我解决这个问题吗?我会很高兴。

编辑:包括我的改编,以澄清“改编”部分。

【问题讨论】:

  • 解压后shapefile是否在嵌套文件夹中?如果是,打开 list.files 中的递归参数。

标签: r import gis shapefile


【解决方案1】:

试试这个。

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

【讨论】:

  • 谢谢,这就是我要找的。​​span>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2018-05-15
  • 2018-11-06
相关资源
最近更新 更多