Dear Researchers,

I wish to improve the code of "Generating Random Transects of Same Length",
by  Marcellino De la Cruz to do a forest invetory analysis
I am trying to use with "Stratified Random Point" inside a grid.

the problem is: sometimes the transect line enter inside another square (if
You run the code you can see very easy the problem). Eventually with
vertices(tiles(Z)) I can change in w=
.....................

# Test if every circle point is inside polygonal boundary
srp.disc.df <- lapply(srp.disc, function(W){
                          inside.owin(W$bdry[[1]]$x,W$bdry[[1]]$y
,w=mywindow)})

.........................

library(spatstat)
mywindow <- owin(c(0,3000),c(0,2000))
plot(mywindow)
# Divide Region into Quadrats
Z <- quadrats(mywindow, 10, 10)
plot(Z, add=T)
# Stratified Random Point
srp <- rstrat(win=Z, nx=10,k=1)
plot(srp,pch=20,cex = 0.8, add=T,col="blue")

# transect
ltransect <- 100

srp.xy <- data.frame(srp$x,srp$y)

#compute a circle around each point
srp.disc <- apply(srp.xy,1, function(x) disc(r=ltransect, x))

# Plot all circle buffer
for(i in 1:length(srp.disc)) {
                    plot(srp.disc[[i]],add=T)
}


# the problem is here. I need to change in w=with the each part of Z(=Divide
Region into Quadrats),
# and compte for each points the following test

# Test if every circle point is inside polygonal boundary
srp.disc.df <- lapply(srp.disc, function(W){
                          inside.owin(W$bdry[[1]]$x,W$bdry[[1]]$y
,w=mywindow)})


# function to sample circle points within the window
samplea2 <- function(srp.xy, l1=srp.disc, l2=srp.disc.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(srp.xy,result)
                            return(result)
}

# the result is a matrix with x0,y0, x1, y1 for each transect
# Plot the random transects:
segmentos <- samplea2(srp.xy)
segments(segmentos[,1][s], segmentos[,2][s],segmentos[,3][s],
segmentos[,4][s])





this is the orginal code:

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])

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to