2013-09-23 4 views
1

이 기능을 확장하고 싶습니다. 현재이 함수는 웹에서 모양 파일을 다운로드하고 압축을 풉니 다. 나는 SO에 다음 코드를 발견 R.셰이프 파일 기능 다운로드 및 읽기 R

library(rgdal) 
dlshape=function(location) { 
    temp=tempfile() 
    download.file(location, temp) 
    unzip(temp) 
} 

으로 파일을 읽을 수 'rgdal'을 구현하고 싶습니다,하지만 난 그것을 적응에 실패했습니다. 이 함수는 여전히 .shp 확장자로 끝나는 파일에 대해 grep이 아닌 첫 번째 파일을 압축 해제 한 것으로 간주합니다.

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

누군가이 문제를 해결할 수 있도록 도와 주시겠습니까? 기꺼이 감사하겠습니다.

편집 : "적응"부분에 대한 설명을위한 나의 적응이 포함됩니다.

+0

압축을 푼 후에 중첩 된 폴더에있는 모양 파일입니까? 그렇다면 list.files에서 재귀 적 인수를 설정하십시오. – mengeln

+0

중첩 폴더가 없습니다. – user2340706

답변

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

고마워,이게 내가 찾고 있었던거야. – user2340706