[R-sig-Geo] Helping with write a function. Merging shape file with all fields?

Zia Ahmed zua3 at cornell.edu
Mon Jan 25 04:25:06 CET 2010


Thank you so much  Matt! It works perfectly. This R-code saves lot of my 
of my time. I appreciate you help.

Zia


Matt Beard wrote:
> Zia,
>
> Does this work?
> Set your working directory and your output filename.
>
> library(rgdal)
> library(maptools)
> setwd("C:/test")                                              # CHANGE 
> THIS
> combinedShp <- "C:/test/combined.shp"           # CHANGE THIS
>
> # obtain shapefiles in current directory
> files <- list.files(pattern = "shp")
> uid <- 1
>
> # get polygons from first file
> poly.data <- readOGR(files[1], gsub(".shp","",files[1]))
> n <- length(slot(poly.data, "polygons"))
> poly.data <- spChFIDs(poly.data, as.character(uid:(uid+n-1)))
> uid <- uid + n
>
> # add polygons from remaining files
> for (i in 2:length(files)) {
>    temp.data <- readOGR(files[i], gsub(".shp","",files[i]))
>    n <- length(slot(temp.data, "polygons"))
>    temp.data <- spChFIDs(temp.data, as.character(uid:(uid+n-1)))
>    uid <- uid + n
>    poly.data <- spRbind(poly.data,temp.data)
> }
>
> writeOGR(poly.data, dsn=combinedShp, layer="combined", driver="ESRI 
> Shapefile")
>
>
> Matt Beard
> Purdue University
>
>
>
>
> On Fri, Jan 22, 2010 at 10:45 PM, Zia Ahmed <zua3 at cornell.edu 
> <mailto:zua3 at cornell.edu>> wrote:
>
>     Hi Matt:
>     With help of Jim Holtman  (my R Guru!), I am able to modified your
>     code for selecting  particular type  of shape  files from
>      different folders and able to  combine them together. Thank you
>     so much!
>     However, final polygon does not contain any filed  (suppose county
>     name, FIPS code  etc) of  the shape files those I used for merging
>     . How do I keep these fields  in  my final polygon.   Any idea?
>     Thanks
>     Zia
>



More information about the R-sig-Geo mailing list