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... Thanks, Pierre Pierre.racine at sbf.ulaval.ca
Generating Random Transects of Same Length
5 messages · Pierre Racine, Marcelino de la Cruz, Barry Rowlingson +1 more
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
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
2009/2/12 Marcelino de la Cruz <marcelino.delacruz at upm.es>:
As an spatstat aficionado, I would use the following code:
Very very nice, but:
#compute a circle around each point cosadisc<- apply(cosaxy,1, function(x) disc(r=ltransect, x))
generates 128 points on a circle around each point. There's a teeny
tiny chance that none of those points will be in your polygon :) What
you've done is a rejection method where you generate 128 and throw 127
away!
I can see the following ways of doing this:
A: 1. generate first point from CSR on the polygon
2. generate second point at distance D from first point
3. goto *2* until second point is in polygon.
B: 1. generate first point from CSR on the polygon
2. generate second point at distance D from the first point
3. goto *1* until second point is in polygon
C: buffer zone method
your code essentially does A, where the first points are CSR but the
second points arent (I think), and B will have neither first points
nor second points as CSR since it avoids the edges.
the buffer zone method produces CSR in the inner zone for the first
points and non-CSR for the second points.
all depends on what the original questioner wanted - if just a bunch
of segments of length D roughly scattered around inside the polygon
then wham! your lovely spatstat code is exactly that!
Barry
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20090212/5517af28/attachment.pl>