EVOLUTION-MANAGER
Edit File: st_sample.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: sample points on or in (sets of) spatial features</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for st_sample {sf}"><tr><td>st_sample {sf}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>sample points on or in (sets of) spatial features</h2> <h3>Description</h3> <p>Sample points on or in (sets of) spatial features. By default, returns a pre-specified number of points that is equal to <code>size</code> (if <code>type = "random"</code>) or an approximation of <code>size</code> (for other sampling types). <code>spatstat</code> methods are interfaced and do not use the <code>size</code> argument. </p> <h3>Usage</h3> <pre> st_sample(x, size, ...) ## S3 method for class 'sf' st_sample(x, size, ...) ## S3 method for class 'sfc' st_sample(x, size, ..., type = "random", exact = TRUE) ## S3 method for class 'sfg' st_sample(x, size, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>object of class <code>sf</code> or <code>sfc</code></p> </td></tr> <tr valign="top"><td><code>size</code></td> <td> <p>sample size(s) requested; either total size, or a numeric vector with sample sizes for each feature geometry. When sampling polygons, the returned sampling size may differ from the requested size, as the bounding box is sampled, and sampled points intersecting the polygon are returned.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>passed on to <a href="../../base/html/sample.html">sample</a> for <code>multipoint</code> sampling, or to <code>spatstat</code> functions for spatstat sampling types (see details)</p> </td></tr> <tr valign="top"><td><code>type</code></td> <td> <p>character; indicates the spatial sampling type; one of <code>random</code>, <code>hexagonal</code> (triangular really), <code>regular</code>, or one of the <code>spatstat</code> methods such as <code>Thomas</code> for calling <code>spatstat::rThomas</code> (see Details).</p> </td></tr> <tr valign="top"><td><code>exact</code></td> <td> <p>logical; should the length of output be exactly the same as specified by <code>size</code>? <code>TRUE</code> by default. Only applies to polygons, and when <code>type = "random"</code>.</p> </td></tr> </table> <h3>Details</h3> <p>The function is vectorised: it samples <code>size</code> points across all geometries in the object if <code>size</code> is a single number, or the specified number of points in each feature if <code>size</code> is a vector of integers equal in length to the geometry of <code>x</code>. </p> <p>if <code>x</code> has dimension 2 (polygons) and geographical coordinates (long/lat), uniform random sampling on the sphere is applied, see e.g. <a href="http://mathworld.wolfram.com/SpherePointPicking.html">http://mathworld.wolfram.com/SpherePointPicking.html</a> </p> <p>For <code>regular</code> or <code>hexagonal</code> sampling of polygons, the resulting size is only an approximation. </p> <p>As parameter called <code>offset</code> can be passed to control ("fix") regular or hexagonal sampling: for polygons a length 2 numeric vector (by default: a random point from <code>st_bbox(x)</code>); for lines use a number like <code>runif(1)</code>. </p> <p>Sampling methods from package <code>spatstat</code> are interfaced (see examples), and need their own parameters to be set. For instance, to use <code>spatstat::rThomas()</code>, set <code>type = "Thomas"</code>. </p> <h3>Value</h3> <p>an <code>sfc</code> object containing the sampled <code>POINT</code> geometries </p> <h3>Examples</h3> <pre> nc = st_read(system.file("shape/nc.shp", package="sf")) p1 = st_sample(nc[1:3, ], 6) p2 = st_sample(nc[1:3, ], 1:3) plot(st_geometry(nc)[1:3]) plot(p1, add = TRUE) plot(p2, add = TRUE, pch = 2) x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,90),c(0,90),c(0,0)))), crs = st_crs(4326)) plot(x, axes = TRUE, graticule = TRUE) if (sf_extSoftVersion()["proj.4"] >= "4.9.0") plot(p <- st_sample(x, 1000), add = TRUE) x2 = st_transform(st_segmentize(x, 1e4), st_crs("+proj=ortho +lat_0=30 +lon_0=45")) g = st_transform(st_graticule(), st_crs("+proj=ortho +lat_0=30 +lon_0=45")) plot(x2, graticule = g) if (sf_extSoftVersion()["proj.4"] >= "4.9.0") { p2 = st_transform(p, st_crs("+proj=ortho +lat_0=30 +lon_0=45")) plot(p2, add = TRUE) } x = st_sfc(st_polygon(list(rbind(c(0,0),c(90,0),c(90,10),c(0,90),c(0,0))))) # NOT long/lat: plot(x) p_exact = st_sample(x, 1000, exact = TRUE) p_not_exact = st_sample(x, 1000, exact = FALSE) length(p_exact); length(p_not_exact) plot(st_sample(x, 1000), add = TRUE) x = st_sfc(st_polygon(list(rbind(c(-180,-90),c(180,-90),c(180,90),c(-180,90),c(-180,-90)))), crs=st_crs(4326)) # FIXME: #if (sf_extSoftVersion()["proj.4"] >= "4.9.0") { # p = st_sample(x, 1000) # st_sample(p, 3) #} # hexagonal: sfc = st_sfc(st_polygon(list(rbind(c(0,0), c(1,0), c(1,1), c(0,0))))) plot(sfc) h = st_sample(sfc, 100, type = "hexagonal") h1 = st_sample(sfc, 100, type = "hexagonal") plot(h, add = TRUE) plot(h1, col = 'red', add = TRUE) c(length(h), length(h1)) # approximate! pt = st_multipoint(matrix(1:20,,2)) ls = st_sfc(st_linestring(rbind(c(0,0),c(0,1))), st_linestring(rbind(c(0,0),c(.1,0))), st_linestring(rbind(c(0,1),c(.1,1))), st_linestring(rbind(c(2,2),c(2,2.00001)))) st_sample(ls, 80) plot(st_sample(ls, 80)) # spatstat example: if (require(spatstat)) { x <- sf::st_sfc(sf::st_polygon(list(rbind(c(0, 0), c(10, 0), c(10, 10), c(0, 0))))) # for spatstat::rThomas(), set type = "Thomas": pts <- st_sample(x, kappa = 1, mu = 10, scale = 0.1, type = "Thomas") } </pre> <hr /><div style="text-align: center;">[Package <em>sf</em> version 0.9-5 <a href="00Index.html">Index</a>]</div> </body></html>