[R-sig-Geo] Helping with write a function. Merging shape files?
Zia Ahmed
zua3 at cornell.edu
Thu Jan 21 02:56:37 CET 2010
Thank you so much Matt. I will try this code with my data. I like to
merge all shape files of a county (SSURGO polygons) and want to
create a single shape file for a state. I did it before for 15 states
of NE-region of USA. I used ArcGIS. I do not like to work with ArcGIS.
I now it is not possible to do this kind of analysis in R under Windows
O/S due to memory issue. Once I able to run this code correctly, then I
will try this code in my new linux system (32 GB memory )
Thanks again
Zia
Matt Beard wrote:
> 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
> <mailto: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 <http://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
>
More information about the R-sig-Geo
mailing list