[R-sig-Geo] Creating a Shapefile from a List of Polygons

Vlad valdounet at gmail.com
Wed Mar 7 09:18:42 CET 2012


Hi Benjamin,

I don't know if you have read carefully the help on 
SpatialPolygonsDataFrame but it is written about the "data" arugment :

"the number of rows in ‘data’ should equal the number of Polygons-class 
objects in ‘Sr’"

In your example 'spPolys' correspond to 'Sr' and is a SpatialPolygons 
which length is 1.
You should build a SpatialPolygons with length equal to the number of 
rows of polyData.
The code below should help you :

xx <- mapply (
             FUN=function(x, y) Polygons(list(x), y),
             polyList, paste('FUBAR', 1:length(polyList), sep='.'),
             SIMPLIFY=FALSE)

spPolys <- SpatialPolygons(xx)
polyData <- data.frame (test=1:length(spPolys)) # actually the datayou 
want to use
dfReturn <- SpatialPolygonsDataFrame (spPolys, polyData, match.ID=FALSE)

Best,

Vlad

-------
Vladislav Navel

SCAN
www.scan-datamining.com

Le 07/03/2012 04:22, Benjamin Stewart a écrit :
> I feel like I have done this before, so I am sorry if I have made a stupid
> mistake, however, I am out of ideas.
>
> I am trying to make a shapefile out of a list of polygons. I want to
> include the a data frame as well, but I can't seem to create a
> SpatialPolygons-Class longer than one object. The one object has all the
> individual features when I map it, but it is only one object. Here is my
> code:
>
>       xx = Polygons(polyList, "FUBAR")
>       spPolys = SpatialPolygons(list(xx))
>       polyData = (The data frame I want to use)
>       dfReturn = SpatialPolygonsDataFrame(spPolys, polyData, match.ID=F)
>
> Any ideas? If I replace the polyData with data.frame(ID = 1) stupid thing
> works!
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list