sample.Spatial question
On Thu, 12 Nov 2009, Edzer Pebesma wrote:
This is not a full answer. To start of with, I'd suggest to use spsample rather than sample.Spatial (maybe we should not make the latter available to usage). rand.points <- spsample(SPDF,n=23,type="hexagonal") plot(rand.points, add=T,col='red') The pattern you sketch below (nice ascii art!) might not fit in a square area. I'm afraid we overlooked this, and need to modify the documentation to state that only for a limited number of sampling methods for square regions the sample size is guaranteed the requested number. And exclude the hexagonal...
Yes, the outcome reported is based on the cellsize= argument not being given, so it defaults to: area = prod(apply(bb, 1, diff))/n dx = sqrt(area)/(sqrt(3)/2) If you use: rand.points <- spsample(SPDF, cellsize=80, offset=c(0.3, 0.6), type="hexagonal") then dim(coordinates(rand.points)) is [1] 23 2 but it needs some playing around with both cellsize= and offset=. Using cellsize= rather than n= may be sensible. For sample.Polygon which is the method being used here, offset= defaults to runif(2), so should be set to a known value to anchor the grid. Roger
-- Edzer Chen, Shaofei wrote:
Dear list members, Thanks to Dr. Bivand's quick reply on my last question about creating a specific polygon. My next question follows that one. After creating a polygon, I want to sample inside the polygon, I used sample.Spatial in sp library. I want to sample 23 points in a hexagonal structure. library(sp) crds <- cbind(x=c(0, 0, 400, 400, 0), y=c(0, 400, 400, 0, 0)) Pl <- Polygon(crds) ID <- "400x400" Pls <- Polygons(list(Pl), ID=ID) SPls <- SpatialPolygons(list(Pls)) df <- data.frame(value=1, row.names=ID) SPDF <- SpatialPolygonsDataFrame(SPls, df) plot(SPDF) rand.points <- sample.Spatial(SPDF,n=23,type="hexagonal") plot(rand.points, add=T) According to my imagination, the point pattern should look like this: + + + + + + + + + + + + + + + + + + + + + + + However, everytime, the results are not 23 points, they usually come up with 16 points. I changed to regular sampling structure with a specified offset c(0.5,0.5). It can give consistent results for 4x4=16 or 5x5=25... points. But for other number of points, for example, 20 points, it cannot give a 4x5 point pattern. Also offset option is not for other sampling type. I just wonder why this happens. I look at the source code, I think it may because that the it defines the first point randomly (of course, it uses "area = prod(apply(bb, 1, diff))/n" to define the stratum), and then construct the following samples. In this way, some points may be out of the boundary. Is it possible that I can get exactly number of points that I want for sampling? And also I want the offset for hexagon is available, so that I can make sure that the sampling points locating in the centroid of the hexagon. I checked the archives of the list, Dr. Bivand has provided codes that not utilize sample.Spatial for creating hexagons and centroids (I have done it with 23 points in 400 by 400 polygon), but it may not be easy in unregular polygon with defined number of points. Thank you in advance! Best, Shaofei
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no