[R-sig-Geo] shape of circles

Agustin Lobo Agustin.Lobo at ija.csic.es
Sat Feb 23 21:02:26 CET 2008


Thanks!

This is what I've made. I'm sure it can be done
in a much more elegant way, but hopefully it will
be useful for other people.

"circles" <- function(x=presspUTM2 at coords[,1],y=presspUTM2 at coords[,2],
		r=rep(10,nrow(presspUTM2 at coords)), n=100, 					ID=presspUTM2 at data$ID, 
datos=presspUTM2 at data)
#According to message from b.rowlingson at lancaster.ac.uk
#Makes polygonal circles at x,y with radii r ready to be saved as shp:
#presspUTM2circles <- circles()
#writeOGR(presspUTM2circles, "J:/Lidia", "presspUTM2circles", 
driver="ESRI Shapefile")

{
   require(sp)
   lista <- vector(length=length(x),mode="list")
   names(lista) <- ID
   for (i in 1:length(x)){
     t=seq(0,2*pi,len=n+1)[-1]
     circunf <- cbind(r[i]*sin(t)+x[i],r[i]*cos(t)+y[i])
     circunf <- rbind(circunf,circunf[1,])
     Sr1 <- Polygon(cbind(circunf[,1], circunf[,2]))
     Sr1 <- Polygons(list(Sr1), ID[i])
     lista[i] <- Sr1
   }
   row.names(datos) <- ID
   Sr<- SpatialPolygons(lista,pO=1:length(x),CRS("+proj=tmerc +lat_0=0 
+lon_0=2.999999982811267 +k=0.999600 +x_0=500000 +y_0=0 +ellps=intl 
+units=m +no_defs"))
   SpatialPolygonsDataFrame(Sr, data=datos, match.ID = TRUE)
}


Barry Rowlingson escribió:
> Agustin Lobo wrote:
>> Hi!
>>
>> I have to create a shp of "circular" polygons
>> out of a file with x,y coordinates and radii.
>>
>> I've found that disc() from package GRID makes
>> an owin object for each circle, but then how can
>> I convert to sp (from there to shp using writeOGR)?
> 
> To generate n points on a circle centred at (x,y) of radius r you can do:
> 
> circle = 
> function(x,y,r,n=100){t=seq(0,2*pi,len=n+1)[-1];return(cbind(r*sin(t),r*cos(t)))} 
> 
> 
> Increasing the n parameter makes the circle smoother. n=4 gives a 
> square! Note this function doesn't generate duplicate points at start 
> and finish.
> 
>  From here to sp should be trivial...
> 
> Barry
> 
> 

-- 
Dr. Agustin Lobo
Institut de Ciencies de la Terra "Jaume Almera" (CSIC)
LLuis Sole Sabaris s/n
08028 Barcelona
Spain
Tel. 34 934095410
Fax. 34 934110012
email: Agustin.Lobo at ija.csic.es
http://www.ija.csic.es/gt/obster




More information about the R-sig-Geo mailing list