Re: [R-sig-Geo] uniformly sample points on a border of a polygon
Thanks Jeff, this solution also works quite well! Thanks again to all those that proposed different solutions. Best Paolo Da: R-sig-Geo per conto di Hollister, Jeff Inviato: domenica 16 ottobre 2016 15.30 A: r-sig-geo@r-project.org Oggetto: Re: [R-sig-Geo] uniformly sample points on a border of a polygon And for another option, using sp::spsample mypol<-round(matrix(c(-13.8447497369687, -3.51439434200449, 6.09494902836977, 6.83498916728338, 9.20403746769121, 15.3061452155498, 18.4050681631565, 15.334153355932, 9.21809033073377, 6.90467983448734, 6.17942233200763, -3.4864867866601, -13.8299219386242, -17.5237987124776, -17.2262670680261, -17.5217563171495, -2.29667185082115, -7.72275721405543, -9.77084968112857, -8.81725304021858, -8.32894043391822, -4.76080777897439, -0.0600572363382094, 4.62779963258511, 8.20771806467615, 8.70484104396818, 9.68531129857718, 7.67574865642846, 2.46081860449754, 1.31152149442131, 0.0845735294613392, -1.11988475144136), ncol=2),digits=2) mypol <- SpatialPolygons(list(Polygons(list(Polygon(mypol)),ID = "1"))) pts <- spsample(as(mypol, "SpatialLines"), 100, "regular") plot(mypol) plot(pts, add=T,col="red") Cheers, Jeff [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo R-sig-Geo Info Page<https://stat.ethz.ch/mailman/listinfo/r-sig-geo> stat.ethz.ch R-sig-Geo -- R Special Interest Group on using Geographical data and Mapping About R-sig-Geo [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] uniformly sample points on a border of a polygon
And for another option, using sp::spsample mypol<-round(matrix(c(-13.8447497369687, -3.51439434200449, 6.09494902836977, 6.83498916728338, 9.20403746769121, 15.3061452155498, 18.4050681631565, 15.334153355932, 9.21809033073377, 6.90467983448734, 6.17942233200763, -3.4864867866601, -13.8299219386242, -17.5237987124776, -17.2262670680261, -17.5217563171495, -2.29667185082115, -7.72275721405543, -9.77084968112857, -8.81725304021858, -8.32894043391822, -4.76080777897439, -0.0600572363382094, 4.62779963258511, 8.20771806467615, 8.70484104396818, 9.68531129857718, 7.67574865642846, 2.46081860449754, 1.31152149442131, 0.0845735294613392, -1.11988475144136), ncol=2),digits=2) mypol <- SpatialPolygons(list(Polygons(list(Polygon(mypol)),ID = "1"))) pts <- spsample(as(mypol, "SpatialLines"), 100, "regular") plot(mypol) plot(pts, add=T,col="red") Cheers, Jeff [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] uniformly sample points on a border of a polygon
Thanks Mike, I'll look forward for this solution too best paolo Da: mgm mgm Inviato: venerd� 14 ottobre 2016 23.12 A: Paolo Piras Cc: Adrian Baddeley; Rolf Turner; Ege Rubak; r-sig-geo Oggetto: Re: [R-sig-Geo] uniformly sample points on a border of a polygon Another possible solution is to use the spsurvey package, and think of each edge as a level of stratification and use the grts function to design a survey of sample points on the edges. An advantage of the grts function is that it uses a local neighborhood variance estimator, which can produce smaller confidence limits compared to simple random sample variiance. Mike On Friday, October 14, 2016, Paolo Piras mailto:paolo.pi...@uniroma3.it>> wrote: Dear Adrian, even this solution is pretty cool. Again, thanks to all who suggested me how to do that. Best Paolo Da: Adrian Baddeley Inviato: venerd� 14 ottobre 2016 02.14 A: Paolo Piras; Rolf Turner Cc: r-sig-geo; Ege Rubak Oggetto: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of a polygon You can use the spatstat function 'edges' to extract the edges of a polygonal window. Example: W <- letterR#polygonal window E <- edges(W) X <- runifpointOnLines(20, E) plot(E) plot(X, add=TRUE) Prof Adrian Baddeley DSc FAA Department of Mathematics and Statistics Curtin University, Perth, Western Australia From: Paolo Piras Sent: Friday, 14 October 2016 5:22 AM To: Rolf Turner Cc: r-sig-geo; Adrian Baddeley; Ege Rubak Subject: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of a polygon Thanks a lot Rolf! This is virtually exactly what I need; I'm very grateful for that All the best Paolo Da: Rolf Turner Inviato: gioved� 13 ottobre 2016 23.12 A: Paolo Piras Cc: r-sig-geo; adrian.badde...@curtin.edu.au; Ege Rubak Oggetto: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of a polygon On 14/10/16 07:03, Paolo Piras wrote: > HI folks, > > I write for a (hopefully) relatively simple question: > > I would need to uniformly sample 1000 or more points **along the border** of > a polygon (not within the area enclosed) that is identified by ordered but > not equally spaced points; which is the fastest way? > > In a first moment I thought to sample between any pair of consecutive points > but, given that starting points are not uniformly distributed, the final > result would be very far from a uniform distribution. > > here my polygon: > > > mypol<-round(matrix(c(-13.8447497369687, -3.51439434200449, > 6.09494902836977, 6.83498916728338, 9.20403746769121, 15.3061452155498, > 18.4050681631565, 15.334153355932, 9.21809033073377, 6.90467983448734, > 6.17942233200763, -3.4864867866601, -13.8299219386242, -17.5237987124776, > -17.2262670680261, -17.5217563171495, -2.29667185082115, -7.72275721405543, > -9.77084968112857, -8.81725304021858, -8.32894043391822, -4.76080777897439, > -0.0600572363382094, 4.62779963258511, 8.20771806467615, 8.70484104396818, > 9.68531129857718, 7.67574865642846, 2.46081860449754, 1.31152149442131, > 0.0845735294613392, -1.11988475144136),ncol=2),digits=2) > plot(mypol,asp=1,cex=0) > text(mypol[,1],mypol[,2],c(1:nrow(mypol))) > Thanks in advance for any hints This can be done reasonably easily using the spatstat package, for some value of the word "reasonably". Here's how: require(spatstat) W <- owin(poly=mypol) m <- cbind(mypol[-nrow(mypol),],mypol[-1,]) m <- rbind(m,c(mypol[nrow(mypol),],mypol[1,])) m <- as.data.frame(m) names(m) <- c("x0","y0","x1","y1") L <- with(m,psp(x0,y0,x1,y1,window=boundingbox(W))) set.seed(42) #X <- runifpointOnLines(1000,L) X <- runifpointOnLines(100,L) plot(W,main="Piras's Polygon") plot(X,add=TRUE) Note that I have just generated 100 uniform points, r.t. 1000, so that the resulting plot is a little less cluttered. There may be a sexier way of accomplishing your desideratum; I have cc-ed this email to my co-authors Adrian and Ege who may come up with better ideas. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 [[alternative HTML version deleted]] [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Re: [R-sig-Geo] uniformly sample points on a border of a polygon
Note that the spsurvey package can also do IRS (simple independent random sample) on points, polylines (e.g., stream networks), and area (polygons). While the irslin() function only shows shapefile and not sp object, the irs() function allows you to specify an sp object. So, you can convert your polygon boundary to an sp spatialLines object boundary, then: design1 <- list("Stratum 1"=list(panel=c(Panel=1000), seltype="Equal") draw <- irs(design=design1, type.frame='linear', src.frame='sp.object', sp.object='boundary') # I'm pretty sure boundary is a quoted name This is roughly the same design object & syntax for a grts draw. Tom 2 On Fri, Oct 14, 2016 at 2:12 PM, mgm mgm wrote: > Another possible solution is to use the spsurvey package, and think of each > edge as a level of stratification and use the grts function to design a > survey of sample points on the edges. An advantage of the grts function is > that it uses a local neighborhood variance estimator, which can produce > smaller confidence limits compared to simple random sample variiance. > Mike > > On Friday, October 14, 2016, Paolo Piras wrote: > > > Dear Adrian, > > > > even this solution is pretty cool. > > > > Again, thanks to all who suggested me how to do that. > > > > Best > > > > Paolo > > > > > > > > Da: Adrian Baddeley > > > Inviato: venerdì 14 ottobre 2016 02.14 > > A: Paolo Piras; Rolf Turner > > Cc: r-sig-geo; Ege Rubak > > Oggetto: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of > a > > polygon > > > > > > You can use the spatstat function 'edges' to extract the edges of a > > polygonal window. > > > > > > Example: > > > > W <- letterR#polygonal window > > > > E <- edges(W) > > > > X <- runifpointOnLines(20, E) > > > > plot(E) > > > > plot(X, add=TRUE) > > > > > > > > Prof Adrian Baddeley DSc FAA > > > > Department of Mathematics and Statistics > > > > Curtin University, Perth, Western Australia > > > > > > > > From: Paolo Piras > > > Sent: Friday, 14 October 2016 5:22 AM > > To: Rolf Turner > > Cc: r-sig-geo; Adrian Baddeley; Ege Rubak > > Subject: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of > a > > polygon > > > > > > Thanks a lot Rolf! > > > > This is virtually exactly what I need; > > > > I'm very grateful for that > > > > All the best > > > > Paolo > > > > > > > > Da: Rolf Turner > > > Inviato: giovedì 13 ottobre 2016 23.12 > > A: Paolo Piras > > Cc: r-sig-geo; adrian.badde...@curtin.edu.au ; Ege Rubak > > Oggetto: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of > a > > polygon > > > > On 14/10/16 07:03, Paolo Piras wrote: > > > HI folks, > > > > > > I write for a (hopefully) relatively simple question: > > > > > > I would need to uniformly sample 1000 or more points **along the > > border** of a polygon (not within the area enclosed) that is identified > by > > ordered but not equally spaced points; which is the fastest way? > > > > > > In a first moment I thought to sample between any pair of consecutive > > points but, given that starting points are not uniformly distributed, the > > final result would be very far from a uniform distribution. > > > > > > here my polygon: > > > > > > > > > mypol<-round(matrix(c(-13.8447497369687, -3.51439434200449, > > 6.09494902836977, 6.83498916728338, 9.20403746769121, 15.3061452155498, > > 18.4050681631565, 15.334153355932, 9.21809033073377, 6.90467983448734, > > 6.17942233200763, -3.4864867866601, -13.8299219386242, -17.5237987124776, > > -17.2262670680261, -17.5217563171495, -2.29667185082115, > -7.72275721405543, > > -9.77084968112857, -8.81725304021858, -8.32894043391822, > -4.76080777897439, > > -0.0600572363382094, 4.62779963258511, 8.20771806467615, > 8.70484104396818, > > 9.68531129857718, 7.67574865642846, 2.46081860449754, 1.31152149442131, > > 0.0845735294613392, -1.11988475144136),ncol=2),digits=2) > > > plot(mypol,asp=1,cex=0) > > > text(mypol[,1],mypol[,2],c(1:nrow(mypol))) > > > Thanks in advance for any hints > > > > > > This can be done reasonably easily using the spatstat package, for some > > value of the word "reasonably". Here's how: > > > > require(spatstat) > > W <- owin(poly=mypol) > > m <- cbind(mypol[-nrow(mypol),],mypol[-1,]) > > m <- rbind(m,c(mypol[nrow(mypol),],mypol[1,])) > > m <- as.data.frame(m) > > names(m) <- c("x0","y0","x1","y1") > > L <- with(m,psp(x0,y0,x1,y1,window=boundingbox(W))) > > set.seed(42) > > #X <- runifpointOnLines(1000,L) > > X <- runifpointOnLines(100,L) > > plot(W,main="Piras's Polygon") > > plot(X,add=TRUE) > > > > Note that I have just generated 100 uniform points, r.t. 1000, so that > > the resulting plot is a little less cluttered. > > > > There may be a sexier way of accomplishing your desideratum; I have > > cc-ed this email to my co-authors Adrian and Ege who may come up with > > better id
Re: [R-sig-Geo] uniformly sample points on a border of a polygon
Another possible solution is to use the spsurvey package, and think of each edge as a level of stratification and use the grts function to design a survey of sample points on the edges. An advantage of the grts function is that it uses a local neighborhood variance estimator, which can produce smaller confidence limits compared to simple random sample variiance. Mike On Friday, October 14, 2016, Paolo Piras wrote: > Dear Adrian, > > even this solution is pretty cool. > > Again, thanks to all who suggested me how to do that. > > Best > > Paolo > > > > Da: Adrian Baddeley > > Inviato: venerdì 14 ottobre 2016 02.14 > A: Paolo Piras; Rolf Turner > Cc: r-sig-geo; Ege Rubak > Oggetto: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of a > polygon > > > You can use the spatstat function 'edges' to extract the edges of a > polygonal window. > > > Example: > > W <- letterR#polygonal window > > E <- edges(W) > > X <- runifpointOnLines(20, E) > > plot(E) > > plot(X, add=TRUE) > > > > Prof Adrian Baddeley DSc FAA > > Department of Mathematics and Statistics > > Curtin University, Perth, Western Australia > > > > From: Paolo Piras > > Sent: Friday, 14 October 2016 5:22 AM > To: Rolf Turner > Cc: r-sig-geo; Adrian Baddeley; Ege Rubak > Subject: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of a > polygon > > > Thanks a lot Rolf! > > This is virtually exactly what I need; > > I'm very grateful for that > > All the best > > Paolo > > > > Da: Rolf Turner > > Inviato: giovedì 13 ottobre 2016 23.12 > A: Paolo Piras > Cc: r-sig-geo; adrian.badde...@curtin.edu.au ; Ege Rubak > Oggetto: Re: [FORGED] [R-sig-Geo] uniformly sample points on a border of a > polygon > > On 14/10/16 07:03, Paolo Piras wrote: > > HI folks, > > > > I write for a (hopefully) relatively simple question: > > > > I would need to uniformly sample 1000 or more points **along the > border** of a polygon (not within the area enclosed) that is identified by > ordered but not equally spaced points; which is the fastest way? > > > > In a first moment I thought to sample between any pair of consecutive > points but, given that starting points are not uniformly distributed, the > final result would be very far from a uniform distribution. > > > > here my polygon: > > > > > > mypol<-round(matrix(c(-13.8447497369687, -3.51439434200449, > 6.09494902836977, 6.83498916728338, 9.20403746769121, 15.3061452155498, > 18.4050681631565, 15.334153355932, 9.21809033073377, 6.90467983448734, > 6.17942233200763, -3.4864867866601, -13.8299219386242, -17.5237987124776, > -17.2262670680261, -17.5217563171495, -2.29667185082115, -7.72275721405543, > -9.77084968112857, -8.81725304021858, -8.32894043391822, -4.76080777897439, > -0.0600572363382094, 4.62779963258511, 8.20771806467615, 8.70484104396818, > 9.68531129857718, 7.67574865642846, 2.46081860449754, 1.31152149442131, > 0.0845735294613392, -1.11988475144136),ncol=2),digits=2) > > plot(mypol,asp=1,cex=0) > > text(mypol[,1],mypol[,2],c(1:nrow(mypol))) > > Thanks in advance for any hints > > > This can be done reasonably easily using the spatstat package, for some > value of the word "reasonably". Here's how: > > require(spatstat) > W <- owin(poly=mypol) > m <- cbind(mypol[-nrow(mypol),],mypol[-1,]) > m <- rbind(m,c(mypol[nrow(mypol),],mypol[1,])) > m <- as.data.frame(m) > names(m) <- c("x0","y0","x1","y1") > L <- with(m,psp(x0,y0,x1,y1,window=boundingbox(W))) > set.seed(42) > #X <- runifpointOnLines(1000,L) > X <- runifpointOnLines(100,L) > plot(W,main="Piras's Polygon") > plot(X,add=TRUE) > > Note that I have just generated 100 uniform points, r.t. 1000, so that > the resulting plot is a little less cluttered. > > There may be a sexier way of accomplishing your desideratum; I have > cc-ed this email to my co-authors Adrian and Ege who may come up with > better ideas. > > cheers, > > Rolf Turner > > -- > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > > [[alternative HTML version deleted]] > > [[alternative HTML version deleted]] ___ R-sig-Geo mailing list R-sig-Geo@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo