[R-sig-Geo] Helping with write a function. Merging shape files?
Sean O'Riordain
seanpor at acm.org
Thu Jan 21 06:50:01 CET 2010
Hi Zia,
Can I suggest that you have a good read of
?list.files
and in particular the section on pattern=...
and possibly also
?glob2rx
so you might be modifying Matt's script by changing to
list.files(glob2rx('poly_shape.*.shp'))
or similar
cheers,
Sean
On Thu, Jan 21, 2010 at 2:40 AM, Zia Ahmed <zua3 at cornell.edu> wrote:
> Hi Matt,
>
> Your code work perfectly. As a test run, I am able to merge lower 48
> states and create a polygon of Con. USA when all files are in one folder.
> But SSURGO data organized like following way (not exactly)
> state_name (wd)/
> state_001 (subfolder)/spatial/several shape files (a_shape001.shp,
> *poly_shape001.shp,* l_shape001.shp)
> state_003 (subfolder)/spatial/several shape files (a_shape003.shp,
> *poly_shape003.shp*, l_shape003.shp)
> state_005 (subfolder)/spatial/several shape files (a_shape005.shp,
> *poly_shape005.shp*, l_shape005.shp)
> ---------
> state_065 (subfolder)/spatial/several shape files (a_shape065.shp,
> *poly_shape065.shp*, l_shape065.shp)
>
> like this.
>
> I like to select one shape file (suppose *poly_shape-----.shp*) from each
> of sub folder of my working directory and merge them together. Is there
> any way to do this in a loop?
> 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
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
More information about the R-sig-Geo
mailing list