knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(rSDM)
rSDM
includes a handy function to move points to the nearest raster cell with data (i.e. not NA).
Let's generate example point coordinates:
locs <- data.frame(lon = c(1, 2, 1, 2, 2.2), lat = c(1.2, 1, 2.3, 3, 2)) locs.sf <- locs2sf(locs) locs.sf
Now let's generate an example raster:
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))
As we can see in the map below, some points fall outside raster cells with data:
occmap(locs.sf, ras, pcol = "black", psize = 3)
moved <- points2nearestcell(locs.sf, ras)
Note you can choose the format of the output map, e.g. now using ggplot (interactive leaflet maps are available too):
points2nearestcell(locs.sf, ras, map = "ggplot")
The function returns a spatial object where the coordinates of the points falling outside the raster have been moved:
moved
Let's compare the original and new coordinates:
sf::st_coordinates(locs.sf) sf::st_coordinates(moved)
If you don't want to change coordinates but only check which would the nearest raster cells for each point, use move = FALSE
.
In case you want to move points only if the nearest raster cell is within a given distance, you could use the distance
argument to set a 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.