[R-sig-Geo] Helping with write a function. Merging shape files?
Matt Beard
biozealot21 at gmail.com
Thu Jan 21 02:38:10 CET 2010
Zia,
The error you received was 'invalid class "SpatialPolygons" object:
non-unique Polygons ID slot values'.
Renaming the Polygons IDs so that they are unique should fix your problem.
The following code along with some test shapefiles I used are attached.
library(rgdal)
setwd("C:/test")
# obtain shapefiles in current directory
files <- list.files(pattern = "shp")
# get polygons from first file
data.first <- readOGR(files[1], gsub(".shp","",files[1]))
polygons <- slot(data.first, "polygons")
# add polygons from remaining files
for (i in 2:length(files)) {
data.temp <- readOGR(files[i], gsub(".shp","",files[i]))
polygons <- c(slot(data.temp, "polygons"),polygons)
}
# rename IDs of Polygons
for (i in 1:length(polygons)) {
slot(polygons[[i]], "ID") <- paste(i)
}
# ain't that spatial
spatialPolygons <- SpatialPolygons(polygons)
spdf <- SpatialPolygonsDataFrame(spatialPolygons,
data.frame(fakeData=1:length(polygons)))
# output combined results
plot(spatialPolygons)
writeOGR(spdf, dsn="C:/test/combined.shp", layer="combined", driver="ESRI
Shapefile")
# end of code
My test shapefiles had very different attribute tables, so I didn't bother
combining the data tables.
However, the code above should at least show you that your method can work.
I found it interesting that when I combined the polygons in the opposite
order:
polygons <- c(polygons,slot(data.temp, "polygons"))
the SoutheasternStates polygons completely covered up the Florida polygons
in QGis, but
the Florida polygons showed up when plotting in R:
plot(spatialPolygons)
Does the R 'plot' function order the polygons before displaying them? Is it
possible to view the code
for a function like plot(SpatialPolygons)?
Matt
On Wed, Jan 20, 2010 at 4:14 PM, Zia Ahmed <zua3 at cornell.edu> wrote:
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20100120/aa6ba726/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.zip
Type: application/zip
Size: 67885 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20100120/aa6ba726/attachment.zip>
More information about the R-sig-Geo
mailing list