Skip to content
Prev 7418 / 29559 Next

Helping with write a function. Merging shape files?

Thanks! I am trying to write a function to merge or combine several 
shape files.

First I try to use following function when all shape files in one 
folder. It showed error.

setwd("E:/Zia/SURRGO_Data/SSURGO_RAW_DATA/Test1")
library(rgdal)
## obtain shapefile names in current directory
fs <- list.files(pattern = "shp")
 ## read the first one
 d <- readOGR(fs[1], gsub(".shp", "", fs[1]))
 for (i in 2:length(fs)) {
 d.tmp <- readOGR(fs[i], gsub(".shp", "", fs[i]))
 }
 d <- rbind(d, d.tmp)
 >  d <- rbind(d, d.tmp)
Error in validObject(.Object) :
invalid class "SpatialPolygons" object: non-unique Polygons ID slot values


Then I am trying to use following code, but it did  not work. it  
_created a empty layer. _I think I did something wrong.  Any idea, __

# Code for similar file name (shape.shp)
setwd("E:/Zia/SURRGO_Data/SSURGO_RAW_DATA/Testf")
library(rgdal)

files <- list.files(pattern = "shp")  # get all the names in the directory
files <- files[file.info(files)$isdir]  # only keep the directories

layer<- file('layer.shp')  # output file
for (Dir in files){
    input <- readOGR(file.path(Dir, "shape.shp")) 
    rbind(input, layer)  
}
close(layer)

I have a situations  like   all shape files are different names 
(like...001.shp, ...003.shp, ....005.shp  so on ) and were saved in 
different folders (like fn001, fn003, fn005....... If someone helps me 
to write correct code  for solving this it will be great.

Zia
Hannes Reuter wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20100120/2aed8f54/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: zua3.vcf
Type: text/x-vcard
Size: 281 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20100120/2aed8f54/attachment.vcf>