Description Usage Arguments Value See Also Examples
Given a set of points (points
), find the nearest cells in raster
which are not NA
, up to a maximum search radius of max_distance
.
1 | nearestLand(points, raster, max_distance)
|
points |
A two-column matrix or dataframe giving x and y coordinates, or a |
raster |
A |
max_distance |
The maximum distance to search before giving up and returning |
A two-column matrix giving the coordinates of the nearest cells, or NA
values if there was no non-NA
cell within max_distance
of the points.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # load the template raster
data(template)
# generate 100 random points on the raster
n <- 100
pts <- matrix(c(runif(n, -10, 10), runif(n, -5, 15)), ncol = 2)
# plot them
plot(template)
points(pts, pch = 16)
# find those which fall in NA areas on the raster
vals <- extract(template, pts)
outside_mask <- is.na(vals)
outside_pts <- pts[outside_mask, ]
# plot these in blue
points(outside_pts, pch = 16, col = 'blue')
# find the nearest land within 5 decimal degrees of these
land <- nearestLand(outside_pts, template, 2)
# count how many were moved
sum(!is.na(land[, 1]))
# and how many were too far out
sum(is.na(land[, 1]))
# plot the new points (for those which were reassigned) in green
points(land, pch = 16, col = 'dark green')
# plot where they moved to
arrows(outside_pts[, 1],
outside_pts[, 2],
land[, 1],
land[, 2], length = 0.1)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.