[R-sig-Geo] Helping with write a function. Merging shape file with all fields?
Zia Ahmed
zua3 at cornell.edu
Sat Jan 23 20:29:10 CET 2010
Thanks a lot! I will try accordingly.
Thanks again
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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20100123/aefaf358/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/20100123/aefaf358/attachment.vcf>
More information about the R-sig-Geo
mailing list