[R-sig-Geo] handling circles
Roger Bivand
Roger.Bivand at nhh.no
Sat Jun 7 21:28:56 CEST 2008
On Sat, 7 Jun 2008, milton ruser wrote:
> Dear All
>
>
> I need to handly some circular-shaped polygons on R. First I adapted the
> code from Barry Rowlingson to create a function that create the list of X-Y
> points which create a circle with a specified radii. After that I need to
> save each circle as shapefile with writeOGR. My last task is to identify the
> region that are overlaped by three circles.
>
> I tryed to convert my points to Polygon, and after to SpatialPolygons, but
> got error. Can anyone help me on this task?
>
> Kind regards,
>
> miltinho
>
>
> createCircle <- function(x,y,r,start=0,end=2*pi,nsteps=20,...){
> ## adapted from Barry Rowlingson (2004)
> rs <- seq(start,end,len=nsteps)
> xc <- x+r*cos(rs)
> yc <- y+r*sin(rs)
> polygon(xc,yc,...)
>
> my.pol<-cbind(xc,yc)
> return<-my.pol
> }
>
>
> plot(1:10, type="n")
> my.circle1<-createCircle(5,5,2, col="blue")
> my.circle2<-createCircle(7,6,1,col="transparent")
> my.circle3<-createCircle(7,4.5,1.5,col="transparent")
>
>
> my.circle1.Sr <- Polygon(my.circle1)
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
}
>From there:
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
> my.circle1.Sr.SpatPol <- SpatialPolygons(my.circle1.Sr)
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list