Draws a systematic, or grid, sample from a SpatialPolygons
or
SpatialPolygonsDataFrame
object. Optional parameters control
control the relative spacing in horizontal and vertical directions, whether
a square or triangular grid is produced, and whether the grid baseline has
random orientation.
sss.polygon(x, n, spacing = c(1, 1), triangular = FALSE, rand.dir = FALSE)
A SpatialPolygons
or SpatialPolygonsDataFrame
object. This object
must contain at least 1 polygon. If it contains more than 1 polygon, the
SSS sample is drawn from the union of all polygons. Holes are respected.
Sample size. Number of locations to draw from the union of all
polygons contained in x
.
A vector of length 2 containing the RELATIVE spacing of grid points in the horizontal (X) and vertical (Y) directions. See details.
Boolean scalar specifying whether to produce a rectangular
(triangular==FALSE
) or triangular (triangular==TRUE
) grid.
See Details.
Either a boolean scalar specifying whether to randomly
orient the
grid's horizontal axis (rand.dir==TRUE
) or not
(rand.dir==FALSE
), or
a user-specified fixed direction for the horizontal axis. If
FALSE, orientation of the grid is parallel to the X and Y axes. If TRUE,
the X axis of the grid is randomly rotated by an angle between -pi/4
(-45 degrees) and pi/4 (45 degrees).
If rand.dir
is a number, the
grid is rotated by that many radians. No range check is performed
on user-specified rand.dir
, so for example, rotation by pi/8
is
equivalent to rotation by pi/8 + 2*pi
. User-specified, but random,
direction of the grid can be specified by rand.dir = runif(1,0,pi)
.
Note, relative spacing
of the grid cells is computed prior to rotation.
A SpatialPointsDataFrame
containing locations in the SSS sample,
in row-wise order starting in the south (see sampleID
, row
, col
in returned data frame). Attributes of the sample points (in the
embedded data frame) are
as follows:
sampleID
: A unique identifier for every sample point. For
rectangular grids, sampleID
is incremented west to east by row from
the south. For triangular grids, sampleID
is assigned west to east
to points in every other row from the south. Then,
starts over in the southwest and assigns ID's to previously-skipped
rows.
row
: Row number of the sampled point in the grid. Row numbers
are the vertical indices of the grid in a direction perpendicular to
the (potentially rotated) main horizontal axis. Cell (1,1) is in
the lower left (southwest) corner of the shape's bounding box.
Thus, row 1 is defined
along the lower (southern) boundary of the shape's bounding box.
Points in row 1 may not be inside the shape and therefore
may not appear in the sample. Consequently, the lowest row appearing
in the sample may not be 1. Visualize row i with
points(samp[samp$row==i,])
.
col
: Column number of the sampled point in the grid. Column
numbers are the horizontal indices of the grid in a direction parallel to
the (potentially rotated) main horizontal axis. Cell (1,1) is in
the lower left (southwest) corner of the shape's bounding box.
Thus, column 1 is defined
along the left (western) boundary of the shape's bounding box.
Points in column 1 may not be inside the shape and therefore
may not appear in the sample. Consequently, the lowest column appearing
in the sample may not be 1. Visualize column i with
points(samp[samp$col==i,])
.
geometryID
: The ID of the polygon in x
which each
sample point falls. The
ID of polygons in x
are row.names(geometry(x))
.
Any attributes of the original polygons (in x
).
Additional attributes of the output object, beyond those which
make it a SpatialPointsDataFrame
, are:
frame
: Name of the input sampling frame.
frame.type
: Type of resource in sampling frame. (i.e., "polygon").
sample.type
: Type of sample drawn. (i.e., "SSS").
spacing.m
: A vector of length 2 giving the dimensions of
cells in units of the coordinates of x
. (e.g., meters). This
is the final delta
computed above. Each cell has size
prod(spacing.m)
= Area / n
.
rand.dir
: The (potentially randomly chosen) direction for the grid's
horizontal axis. This is in radians between -pi/4 and pi/4.
rand.dir
= 0 corresponds to no rotation
(i.e., rand.dir =
FALSE
).
rand.shift
: The random shift of the grid. This is a vector
of length 2 containing the random shifts in the horizontal and vertical
directions before rotation. The random shift in both directions
is chosen between 0 and the corresponding element of the spacing.m
attribute (described above).
triangular
: TRUE or FALSE depending on whether the output
grid is triangular or rectangular, respectively.
The projection system of the input shape object (x
) is
not considered. But, a projected coordinate system is necessary to
obtain correct spacing on the ground.
The author STRONGLY recommends converting x
to a UTM coordinate
system prior to calling this function.
Spacing (size and shape of grid cells) is determined by n
and
spacing
. If spacing
is not given,
grid spacing is equal in X and Y directions, which produces square grid
cells. In this case, grid spacing is delta
(= sqrt(A/n)
, where
A
= area of union of all polygons in x
.
Relative shape of grid cells is controlled by the spacing
vector. If
spacing = c(rx, ry)
, spacing in X and Y directions is
spacing*delta/rev(spacing)
, where delta
= sqrt(A/n)
. Conceptually, a square cell of size delta^2
is "stretched" multiplicatively by rx
in the X direction and ry
in the
Y direction. After stretching, the area of each cell remains
delta^2
while the relative lengths of the (rectangular) cell
sides is 1 to (ry/rx)^2
. That is, vertical dimension of each cell
is (ry/rx)^2
times the horizontal dimension. Vice versa, the horizontal
dimension is (rx/ry)^2
times the vertical.
In general, realized sample size is not fixed. Across multiple calls,
realized sample size will not always equal n
. Across an infinite number
of calls, the average sample size will be n
In all cases, the grid is randomly shifted in the X and Y directions, before rotation (if called for). The amount of the random shift is less than the X and Y extent of cells, and is returned as an attribute of the sample.
# NOT RUN {
# A square grid oriented east-west
WA.samp <- sss.polygon( WA, 100 )
plot( WA )
points( WA.samp )
# A rectangular grid oriented east-west, with relative spacing c(0.667, 1.5),
# or 1 to 2.25.
WA.samp <- sss.polygon( WA, 100, spacing=c(2,3) )
plot( WA )
points( WA.samp )
# A rectangular grid oriented east-west, with x spacing = 2*(y spacing).
WA.samp <- sss.polygon( WA, 100, spacing=c(sqrt(2),1) )
# A rectangular grid, random orientation, with y spacing = 3*(x spacing)
WA.samp <- sss.polygon( WA, 100, spacing=c(1,sqrt(3)), rand.dir=TRUE )
# A triangular grid oriented east-west
WA.samp <- sss.polygon( WA, 100, triangular=TRUE )
# A triangular grid oriented east-west, with relative spacing c(.667,1.5)
WA.samp <- sss.polygon( WA, 100, spacing=c(2,3), triangular=TRUE )
# }
Run the code above in your browser using DataLab