Title: | Species distribution and niche modelling in R |
---|---|
Description: | Functions for niche modelling and SDM. |
Authors: | Francisco Rodriguez-Sanchez [aut, cre] |
Maintainer: | Francisco Rodriguez-Sanchez <[email protected]> |
License: | GPL-3 |
Version: | 0.4.0 |
Built: | 2024-11-13 04:59:54 UTC |
Source: | https://github.com/Pakillo/rSDM |
Convert matrix or data frame with point coordinates to a spatial (sf) object
locs2sf(locs, crs = 4326, lon.col = NULL, lat.col = NULL)
locs2sf(locs, crs = 4326, lon.col = NULL, lat.col = NULL)
locs |
A matrix or data frame containing point coordinates data.
If a matrix, the first two columns will be assumed to contain longitude
and latitude coordinates, respectively.
If a data frame, the function will try to guess the columns containing the coordinates
based on column names, unless |
crs |
A character string, number (EPSG value), or |
lon.col |
Character (optional). Name of the column containing longitude data. |
lat.col |
Character (optional). Name of the column containing latitude data. |
An sf::sf()
object.
locs <- matrix(runif(20), ncol = 2) locs2sf(locs) locs <- data.frame(species = rep("Laurus nobilis", 10), x = runif(10), y = runif(10)) locs2sf(locs)
locs <- matrix(runif(20), ncol = 2) locs2sf(locs) locs <- data.frame(species = rep("Laurus nobilis", 10), x = runif(10), y = runif(10)) locs2sf(locs)
Convert matrix or data frame with point coordinates to a SpatVector object
locs2vect(locs, crs = 4326, lon.col = NULL, lat.col = NULL)
locs2vect(locs, crs = 4326, lon.col = NULL, lat.col = NULL)
locs |
A matrix or data frame containing point coordinates data.
If a matrix, the first two columns will be assumed to contain longitude
and latitude coordinates, respectively.
If a data frame, the function will try to guess the columns containing the coordinates
based on column names, unless |
crs |
A number specifying the EPSG code (see https://spatialreference.org). Default is geographic (unprojected) coordinates, datum WGS84 (EPSG = 4326). |
lon.col |
Character (optional). Name of the column containing longitude data. |
lat.col |
Character (optional). Name of the column containing latitude data. |
A terra::SpatVector()
object.
locs <- matrix(runif(20), ncol = 2) locs2vect(locs) locs <- data.frame(species = rep("Laurus nobilis", 10), x = runif(10), y = runif(10)) locs2vect(locs)
locs <- matrix(runif(20), ncol = 2) locs2vect(locs) locs <- data.frame(species = rep("Laurus nobilis", 10), x = runif(10), y = runif(10)) locs2vect(locs)
Plot map of species occurrences (or any set of point coordinates) on top of different background layers.
occmap( locs, ras = NULL, bg = "Esri.WorldImagery", type = c("base", "ggplot", "leaflet"), pcol = "red", alpha = 1, psize = 1, add = FALSE, prev.map = NULL, ... )
occmap( locs, ras = NULL, bg = "Esri.WorldImagery", type = c("base", "ggplot", "leaflet"), pcol = "red", alpha = 1, psize = 1, add = FALSE, prev.map = NULL, ... )
locs |
An |
ras |
A |
bg |
Character. Type of background map to be used if |
type |
Character. One of "base", "ggplot" or "leaflet" to define the type of map produced. |
pcol |
Colour to be used for points. Default is "red". |
alpha |
Colour transparency for points, between 0 (fully transparent) and 1 (fully opaque). |
psize |
Point size. Default is 1 (cex = 1). |
add |
Logical. Add |
prev.map |
Map to be used as basemap to add further points (only applicable for "leaflet" and "ggplot" map types). |
... |
additional parameters to be passed to
|
A map plus a leaflet or ggplot object, depending on type
.
## Example coordinates locs <- data.frame(lon = c(-5.8, -5.5, -5.9), lat = c(35.7, 36.2, 36.5)) locs <- locs2sf(locs, crs = 4326) ## Default map occmap(locs, psize = 6) occmap(locs, psize = 6, bg = "CartoDB.Positron") # Change background ## Interactive (leaflet) map occmap(locs, psize = 6, type = "leaflet") occmap(locs, psize = 6, type = "leaflet", bg = "CartoDB.Positron") ## ggplot map occmap(locs, psize = 6, type = "ggplot") ## Adding points to a previous map new.locs <- data.frame(lon = c(-5.8, -5.4), lat = c(36.2, 36.5)) new.locs.sf <- locs2sf(new.locs, crs = 4326) ## base map <- occmap(locs, psize = 6) occmap(new.locs.sf, add = TRUE, psize = 6, pcol = "blue") ## Adding points to a previous map (leaflet) map <- occmap(locs, psize = 6, type = "leaflet") occmap(new.locs.sf, prev.map = map, psize = 6, pcol = "blue") ## Adding points to a previous map (ggplot) map <- occmap(locs, psize = 6, type = "ggplot") occmap(new.locs.sf, prev.map = map, psize = 6, pcol = "blue")
## Example coordinates locs <- data.frame(lon = c(-5.8, -5.5, -5.9), lat = c(35.7, 36.2, 36.5)) locs <- locs2sf(locs, crs = 4326) ## Default map occmap(locs, psize = 6) occmap(locs, psize = 6, bg = "CartoDB.Positron") # Change background ## Interactive (leaflet) map occmap(locs, psize = 6, type = "leaflet") occmap(locs, psize = 6, type = "leaflet", bg = "CartoDB.Positron") ## ggplot map occmap(locs, psize = 6, type = "ggplot") ## Adding points to a previous map new.locs <- data.frame(lon = c(-5.8, -5.4), lat = c(36.2, 36.5)) new.locs.sf <- locs2sf(new.locs, crs = 4326) ## base map <- occmap(locs, psize = 6) occmap(new.locs.sf, add = TRUE, psize = 6, pcol = "blue") ## Adding points to a previous map (leaflet) map <- occmap(locs, psize = 6, type = "leaflet") occmap(new.locs.sf, prev.map = map, psize = 6, pcol = "blue") ## Adding points to a previous map (ggplot) map <- occmap(locs, psize = 6, type = "ggplot") occmap(new.locs.sf, prev.map = map, psize = 6, pcol = "blue")
This function examines which points fall within a raster cell with data (not NA). Returns TRUE for points falling in a raster cell with data, and FALSE otherwise.
point_in_cell(locs = NULL, ras = NULL, layer = 1)
point_in_cell(locs = NULL, ras = NULL, layer = 1)
locs |
An |
ras |
|
layer |
Integer. Raster layer to use for comparing with point locations (default = 1). |
A logical vector.
locs <- data.frame(lon = c(1, 2, 1, 2), lat = c(1, 1, 2, 3)) 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) point_in_cell(locs.sf, ras) # adding column to original point data locs.sf$inras <- point_in_cell(locs.sf, ras) locs.sf
locs <- data.frame(lon = c(1, 2, 1, 2), lat = c(1, 1, 2, 3)) 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) point_in_cell(locs.sf, ras) # adding column to original point data locs.sf$inras <- point_in_cell(locs.sf, ras) locs.sf
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") )
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)
## 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)