points2nearestcell: Move point occurrences to the nearest raster cell with data

View source: R/points2nearestcell.R

points2nearestcellR Documentation

Move point occurrences to the nearest raster cell with data

Description

Move point occurrences falling in raster cells without data (NA) to the nearest raster cell with data.

Usage

points2nearestcell(
  locs = NULL,
  ras = NULL,
  layer = 1,
  move = TRUE,
  distance = NULL,
  table = TRUE,
  map = c("base", "ggplot", "leaflet", "none")
)

Arguments

locs

An sf::sf() or terra::SpatVector() object with point coordinates, e.g. as generated from locs2sf() or locs2vect().

ras

terra::SpatRaster() object.

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 locs will not be changed.

distance

Numeric (optional). Maximum distance to move points. Point coordinates are only changed if the distance to the nearest raster cell is below distance.

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 occmap().

Value

An sf::sf() or terra::SpatVector() object (with corrected coordinates if move is TRUE).

See Also

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.

Examples


## 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)

Pakillo/rSDM documentation built on March 1, 2025, 12:30 a.m.