Skip to content

handling circles

2 messages · milton ruser, Roger Bivand

#
On Sat, 7 Jun 2008, milton ruser wrote:

            
The error message here was:

Error in validityMethod(object) : ring not closed

so you need a line closing it in your function:

createCircle <- function(x,y,r,start=0,end=2*pi,nsteps=20,...){
    rs <- seq(start,end,len=nsteps)
    xc <- x+r*cos(rs)
    yc <- y+r*sin(rs)
    my.pol<-cbind(xc,yc)
    my.pol <- rbind(my.pol, my.pol[1,])
    my.pol
}
my.circle1.Sr <- Polygons(list(Polygon(my.circle1)), ID="1")
...
my.circle1.Sr.SpatPol <- SpatialPolygons(list(my.circle1.Sr))

where the list would contain your collection of circles as a Polygon 
object inside a Polygons object, before assembling them in a 
SpatialPolygons object.

rn <- sapply(slot(my.circle1.Sr.SpatPol, "polygons"), function(x) slot(x,
   "ID"))
df <- data.frame(rn=rn, row.names=rn)
all.circles <- SpatialPolygonsDataFrame(my.circle1.Sr.SpatPol, data=df)

To output it as a shapefile, it needs a data frame too, so you will need 
to make something like all.circles.

Thanks for a well-structured question, it makes answering so much more 
straight-forward.

Roger