[R-sig-Geo] Generating Random Transects of Same Length
Marcelino de la Cruz
marcelino.delacruz at upm.es
Thu Feb 12 20:16:09 CET 2009
As an spatstat aficionado, I would use the following code:
Enjoy!
Marcelino
library(spatstat)
# Define polygon, length of transect and number of (points)transects
data(letterR)
mywindow <- letterR
ltransect <- 0.3
npoints <- 100
s<- 1:npoints
# Generate random origin points
cosa <- runifpoint(npoints, w=mywindow)
plot(cosa)
cosaxy <- data.frame(cosa$x,cosa$y)
#compute a circle around each point
cosadisc<- apply(cosaxy,1, function(x) disc(r=ltransect, x))
# Test if every circle point is inside polygonal boundary
cosadisc.df <- lapply(cosadisc, function(W){
inside.owin(W$bdry[[1]]$x,W$bdry[[1]]$y
,w=mywindow)})
#function to sample circle points within the window
samplea2 <- function(cosaxy, l1=cosadisc, l2=cosadisc.df){
result<-c(0,0)
for (i in 1:length(l1)){
truinside<-sum(l2[[i]])
inside
<-cbind(l1[[i]]$bdry[[1]]$x,l1[[i]]$bdry[[1]]$y)[l2[[i]],]
result<-rbind(result, inside[sample(1:truinside, size=1),])
}
result<-result[-1,]
result<-cbind(cosaxy,result)
return(result)
}
#the result is a matrix with x0,y0, x1, y1 for each transect
#Plot the random transects:
segmentos<-samplea2(cosaxy)
segments(segmentos[,1][s], segmentos[,2][s],segmentos[,3][s], segmentos[,4][s])
At 19:33 12/02/2009, Barry Rowlingson wrote:
>2009/2/12 Pierre Racine <Pierre.Racine at sbf.ulaval.ca>:
> > Hi,
> >
> > Anybody knows a tool or algorythm (R, GRASS or ArcGIS Script, anything!)
> > to generate a number of random same length transects (e.g. 1000) within
> > the extent of a given polygon or raster?
> >
> > I can't just create random point in the extent and joint hem two by two.
> > This would result in different length transects.
> >
> > A simple algorythm would be:
> >
> > -create a transect of a certain length from a random point and a random
> > angle
> > -reject any transect having an end falling outside the given extent
> >
> > But there is chances that this takes years to run...
>
> What do you mean by 'random' transects?
>
> You could define a buffer line of distance d around the inside your
>polygon. This divides space into three regions - the outside area, the
>buffer zone area, and the inside area. The buffer zone area and the
>inside area make up your original polygon.
>
> Then choose your first point from a spatial uniform distribution on
>the inside area. Then pick another point at a random U(0,2*pi) angle
>and distance d. This is guaranteed to be in the original polygon, so
>there's no rejection step. However, you'll never get transects with
>start and end both within the buffer zone. But they would still, in
>some sense of the word, be 'random'.
>
> If this isn't clear then I think a picture would make it so! I think
>you can do buffer operations in R... Grass certainly can!
>
>Barry
>
>_______________________________________________
>R-sig-Geo mailing list
>R-sig-Geo at stat.math.ethz.ch
>https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>---------------------------------------------------------------------------------------------------
>Texto añadido por Panda IS 2008:
>
> Este mensaje NO ha sido clasificado como SPAM.
> Si se trata de un mensaje de correo no
> solicitado (SPAM), haz clic en el siguiente
> vínculo para reclasificarlo:
> http://localhost:6083/Panda?ID=pav_4376&SPAM=true&path=C:\Documents%20and%20Settings\mcr\Configuración%20local\Datos%20de%20programa\Panda%20Software\AntiSpam
>---------------------------------------------------------------------------------------------------
________________________________
Marcelino de la Cruz Rot
Departamento de Biología Vegetal
E.U.T.I. Agrícola
Universidad Politécnica de Madrid
28040-Madrid
Tel.: 91 336 54 35
Fax: 91 336 56 56
marcelino.delacruz at upm.es
More information about the R-sig-Geo
mailing list