| 1 |
\name{spsample} |
| 2 |
\docType{methods} |
| 3 |
\alias{spsample-methods} |
| 4 |
\alias{spsample,Spatial-method} |
| 5 |
\alias{spsample,Line-method} |
| 6 |
\alias{spsample,Lines-method} |
| 7 |
\alias{spsample,SpatialLines-method} |
| 8 |
\alias{spsample,Polygon-method} |
| 9 |
\alias{spsample,Polygons-method} |
| 10 |
\alias{spsample,SpatialGrid-method} |
| 11 |
\alias{spsample,SpatialPixels-method} |
| 12 |
\alias{spsample,SpatialPolygons-method} |
| 13 |
\alias{spsample} |
| 14 |
\alias{makegrid} |
| 15 |
|
| 16 |
\title{ sample point locations in (or on) a spatial object } |
| 17 |
|
| 18 |
\description{ sample point locations within a square area, a grid, |
| 19 |
a polygon, or on a spatial line, using regular or random sampling |
| 20 |
methods; the methods used assume that the geometry used is not spherical, |
| 21 |
so objects should be in planar coordinates } |
| 22 |
\usage{ |
| 23 |
spsample(x, n, type, ...) |
| 24 |
makegrid(x, n = 10000, nsig = 2, cellsize, offset = rep(0.5, nrow(bb)), |
| 25 |
pretty = TRUE) |
| 26 |
} |
| 27 |
\arguments{ |
| 28 |
\item{x}{Spatial object; \code{spsample(x,...)} is a generic method for the |
| 29 |
existing \code{sample.Xxx} functions} |
| 30 |
\item{...}{ optional arguments, passed to the appropriate \code{sample.Xxx} |
| 31 |
functions; see NOTES for \code{nclusters} and \code{iter}} |
| 32 |
\item{n}{ (approximate) sample size } |
| 33 |
\item{type}{ character; \code{"random"} for completely spatial random; |
| 34 |
\code{"regular"} for regular (systematically aligned) sampling; |
| 35 |
\code{"stratified"} for stratified random (one single random location in |
| 36 |
each "cell"); \code{"nonaligned"} for nonaligned systematic sampling |
| 37 |
(nx random y coordinates, ny random x coordinates); \code{"hexagonal"} |
| 38 |
for sampling on a hexagonal lattice; \code{"clustered"} for clustered sampling; |
| 39 |
\code{"Fibonacci"} for Fibonacci sampling on the sphere (see references).} |
| 40 |
\item{bb}{ bounding box of the sampled domain; setting this to a smaller |
| 41 |
value leads to sub-region sampling } |
| 42 |
\item{offset}{ for square cell-based sampling types (regular, stratified, |
| 43 |
nonaligned, hexagonal): the offset (position) of the regular |
| 44 |
grid; the default for \code{spsample} methods is a random location in |
| 45 |
the unit cell [0,1] x [0,1], leading to a different grid after |
| 46 |
each call; if this is set to \code{c(0.5,0.5)}, the returned grid is |
| 47 |
not random (but, in Ripley's wording, "centric systematic"). For |
| 48 |
line objects, a single offset value is taken, where the value varies within |
| 49 |
the [0, 1] interval, and 0 is the beginning of each Line object, and 1 |
| 50 |
its end } |
| 51 |
\item{cellsize}{ if missing, a cell size is derived from the sample size |
| 52 |
\code{n}; otherwise, this cell size is used for all sampling methods |
| 53 |
except \code{"random"} } |
| 54 |
\item{nsig}{ for "pretty" cell size; \code{spsample} does not result in |
| 55 |
pretty grids } |
| 56 |
\item{pretty}{logical; if \code{TRUE}, choose pretty (rounded) coordinates} |
| 57 |
} |
| 58 |
|
| 59 |
\value{ an object of class \link{SpatialPoints-class}. The number of |
| 60 |
points is only guaranteed to equal \code{n} when sampling is done in a |
| 61 |
square box, i.e. (\code{sample.Spatial}). Otherwise, the obtained number |
| 62 |
of points will have expected value \code{n}. |
| 63 |
|
| 64 |
When \code{x} is of a class deriving from \link{Spatial-class} for which |
| 65 |
no \link{spsample-methods} exists, sampling is done in the bounding box |
| 66 |
of the object, using \code{spsample.Spatial}. An overlay using |
| 67 |
\link{over} may be necessary to select the features inside the geometry |
| 68 |
afterwards. |
| 69 |
|
| 70 |
Sampling type \code{"nonaligned"} is not implemented for line objects. |
| 71 |
|
| 72 |
Some methods may return NULL if no points could be successfully placed. |
| 73 |
|
| 74 |
\code{makegrid} makes a regular grid that covers \code{x}; when |
| 75 |
\code{cellsize} is not given it derives one from the number of grid |
| 76 |
points requested (approximating the number of cells). It tries to |
| 77 |
choose pretty cell size and grid coordinates. |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
\section{Methods}{ |
| 82 |
\describe{ |
| 83 |
\item{x = "Spatial"}{ sample in the bbox of \code{x} } |
| 84 |
\item{x = "Line"}{ sample on a line } |
| 85 |
\item{x = "Polygon"}{ sample in a Polygon } |
| 86 |
\item{x = "Polygons"}{ sample in a Polygons object, consisting of possibly |
| 87 |
multiple Polygon objects (holes must be correctly defined, use \code{checkPolygonsHoles} if need be) } |
| 88 |
\item{x = "SpatialPolygons"}{ sample in an SpatialPolygons object; sampling |
| 89 |
takes place over all Polygons objects present, use subsetting to vary |
| 90 |
sampling intensity (density); holes must be correctly defined, use \code{checkPolygonsHoles} if need be } |
| 91 |
\item{x = "SpatialGrid"}{ sample in an SpatialGrid object } |
| 92 |
\item{x = "SpatialPixels"}{ sample in an SpatialPixels object } |
| 93 |
}} |
| 94 |
|
| 95 |
\note{If an \link{Polygon-class} object has zero area (i.e. is a line), |
| 96 |
samples on this line element are returned. If the area is very close |
| 97 |
to zero, the algorithm taken here (generating points in a square |
| 98 |
area, selecting those inside the polygon) may be very resource |
| 99 |
intensive. When numbers of points per polygon are small and |
| 100 |
type="random", the number searched for is inflated to ensure hits, |
| 101 |
and the points returned sampled among these. |
| 102 |
|
| 103 |
The following two arguments can be further specified: |
| 104 |
|
| 105 |
\code{nclusters} Number of clusters (strata) to sample from. |
| 106 |
|
| 107 |
\code{iter}(default = 4) number of times to try to place sample points |
| 108 |
in a polygon before giving up and returning NULL - this may occur when |
| 109 |
trying to hit a small and awkwardly shaped polygon in a large bounding |
| 110 |
box with a small number of points |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
\references{Chapter 3 in B.D. Ripley, 1981. Spatial Statistics, Wiley |
| 115 |
|
| 116 |
Fibonacci sampling: Alvaro Gonzalez, 2010. Measurement of Areas on a |
| 117 |
Sphere Using Fibonacci and Latitude-Longitude Lattices. Mathematical |
| 118 |
Geosciences 42(1), p. 49-64 } |
| 119 |
|
| 120 |
\author{Edzer Pebesma, \email{[email protected]}} |
| 121 |
|
| 122 |
\seealso{\link{over}, \link{point.in.polygon}, \link{sample}} |
| 123 |
\examples{ |
| 124 |
|
| 125 |
data(meuse.riv) |
| 126 |
meuse.sr = SpatialPolygons(list(Polygons(list(Polygon(meuse.riv)), "x"))) |
| 127 |
|
| 128 |
plot(meuse.sr) |
| 129 |
points(spsample(meuse.sr, n = 1000, "regular"), pch = 3) |
| 130 |
|
| 131 |
plot(meuse.sr) |
| 132 |
points(spsample(meuse.sr, n = 1000, "random"), pch = 3) |
| 133 |
|
| 134 |
plot(meuse.sr) |
| 135 |
points(spsample(meuse.sr, n = 1000, "stratified"), pch = 3) |
| 136 |
|
| 137 |
plot(meuse.sr) |
| 138 |
points(spsample(meuse.sr, n = 1000, "nonaligned"), pch = 3) |
| 139 |
|
| 140 |
plot(meuse.sr) |
| 141 |
points(spsample(meuse.sr@polygons[[1]], n = 100, "stratified"), pch = 3, cex=.5) |
| 142 |
|
| 143 |
data(meuse.grid) |
| 144 |
gridded(meuse.grid) = ~x+y |
| 145 |
image(meuse.grid) |
| 146 |
points(spsample(meuse.grid,n=1000,type="random"), pch=3, cex=.5) |
| 147 |
image(meuse.grid) |
| 148 |
points(spsample(meuse.grid,n=1000,type="stratified"), pch=3, cex=.5) |
| 149 |
image(meuse.grid) |
| 150 |
points(spsample(meuse.grid,n=1000,type="regular"), pch=3, cex=.5) |
| 151 |
image(meuse.grid) |
| 152 |
points(spsample(meuse.grid,n=1000,type="nonaligned"), pch=3, cex=.5) |
| 153 |
|
| 154 |
fullgrid(meuse.grid) = TRUE |
| 155 |
image(meuse.grid) |
| 156 |
points(spsample(meuse.grid,n=1000,type="stratified"), pch=3,cex=.5) |
| 157 |
|
| 158 |
} |
| 159 |
\keyword{manip} |
| 160 |
\keyword{methods} |