Thanks Roman, this worked fine:
n=5
my.list <- vector('list', n)
for(i in 1:n) {
my.list[[i]] = Polygons(list(Polygon(cbind(c(dat[i,"X"]-150,dat[i,"X"]-150,dat[i,"X"]+150,dat[i,"X"]+150,dat[i,"X"]-150),
c(dat[i,"Y"]-150,dat[i,"Y"]+150,dat[i,"Y"]+150,dat[i,"Y"]-150,dat[i,"Y"]-150)))),paste("Number.",i,sep=""))
}
SpP = SpatialPolygons(my.list, 1:n)
SrDf = SpatialPolygonsDataFrame(SpP, dat[1:n,])
writePolyShape(SrDf, fn="FemRutor")

Tord

From: romunov [mailto:romunov@gmail.com]
Sent: den 1 juli 2010 11:14
To: Tord Snäll
Subject: Re: [R-sig-Geo] make thousands SpatialPolygons()

I would make a list out of all Sr/Srs objects - you will need it to make Polygons anyway.

I work with lists according to The R Inferno<http://lib.stat.cmu.edu/S/Spoetry/Tutor/R_inferno.pdf> something along the lines:
my.list <- vector('list', n)
for(i in 1:n) {
... additional manipulation...
my.list[[i]] <- your_object
}


Cheers,
Roman


On Thu, Jul 1, 2010 at 10:59 AM, Tord Snäll <Tord.Snall@ekol.slu.se<mailto:Tord.Snall@ekol.slu.se>> wrote:
Hi,
I want to make approximately 10000 square polygons (300*300) based on coordinates in a data frame, and make a .shp-file out of these.

The .pdf "S Classes and Methods for Spatial Data: the sp Package" by Pebesma and Bivand contains an instruction on how to do this, and it works very well, e.g.

i=1
Sr1 = Polygon(cbind(c(dat[i,"X"]-150,dat[i,"X"] - 150,dat[i,"X"]+150,dat[i,"X"]+150,dat[i,"X"]-150),
c(dat[i,"Y"]-150,dat[i,"Y"]+150,dat[i,"Y"]+150,dat[i,"Y"]-150,dat[i,"Y"]-150)))

Srs1 = Polygons(list(Sr1),paste("Number.",i,sep=""))

i=2
Sr2 = Polygon(cbind(c(dat[i,"X"]-150,dat[i,"X"] -150,dat[i,"X"]+150,dat[i,"X"]+150,dat[i,"X"]-150),
c(dat[i,"Y"]-150,dat[i,"Y"]+150,dat[i,"Y"]+150,dat[i,"Y"]-150,dat[i,"Y"]-150)))

Srs2 = Polygons(list(Sr2),paste("Number.",i,sep=""))

SpP = SpatialPolygons(list(Srs1, Srs2), 1:2)

SrDf = SpatialPolygonsDataFrame(SpP, dat[1:2,])

writePolyShape(SrDf, fn="TvaRutor")


However, my data frame "dat" has ~10000 coords and want to write a loop for doing what is done above. My problem is how to store Sr1, Srs1, Sr2, Srs2,... Sr10000, Srs10000 etc to be used in SpatialPolygons().

Help on this is much appreciated.

Cheers,
Tord

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch<mailto:R-sig-Geo@stat.math.ethz.ch>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



--
In God we trust, all others bring data.

	[[alternative HTML version deleted]]

