# [R-sig-Geo] sample.Spatial question

Chen, Shaofei shaofei.chen at utdallas.edu
Thu Nov 12 04:20:59 CET 2009

```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")

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.