View source: R/points2nearestcell.R
points2nearestcell | R Documentation |
Move point occurrences falling in raster cells without data (NA) to the nearest raster cell with data.
points2nearestcell(
locs = NULL,
ras = NULL,
layer = 1,
move = TRUE,
distance = NULL,
table = TRUE,
map = c("base", "ggplot", "leaflet", "none")
)
locs |
An |
ras |
|
layer |
Integer. Raster layer to use for comparing with point locations (default = 1). |
move |
Logical. Change coordinates of points to those of the nearest raster cells?
If FALSE, the function will show the nearest raster cells but coordinates of |
distance |
Numeric (optional). Maximum distance to move points.
Point coordinates are only changed if the distance to the nearest raster cell
is below |
table |
Logical. Print table with old and new coordinates? |
map |
Character. One of "none", "base", "ggplot" or "leaflet", to choose
the type of map showing the old and new point coordinates. See |
An sf::sf()
or terra::SpatVector()
object
(with corrected coordinates if move is TRUE).
https://search.r-project.org/CRAN/refmans/spatstat.geom/html/nearest.raster.point.html and https://search.r-project.org/CRAN/refmans/gecko/html/move.html.
## Generate example point coordinates and raster
locs <- data.frame(lon = c(1, 2, 1, 2, 2.2), lat = c(1.2, 1, 2.3, 3, 2))
locs.sf <- locs2sf(locs)
library(terra)
ras <- rast(nrows = 2, ncols = 2, xmin = 0.5, xmax = 3.5, ymin = 0.5, ymax = 3.5,
resolution = 1, vals = c(NA, 1, 1, NA, NA, 1, NA, 1, 1))
occmap(locs.sf, ras, pcol = "black", psize = 3)
## Move point coordinates falling outside of raster
moved <- points2nearestcell(locs.sf, ras)
moved
## Move points only if moving distance is lower than specified threshold:
moved <- points2nearestcell(locs.sf, ras, distance = 100000)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.