Package 'rSDM'

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

Help Index


Convert matrix or data frame with point coordinates to a spatial (sf) object

Description

Convert matrix or data frame with point coordinates to a spatial (sf) object

Usage

locs2sf(locs, crs = 4326, lon.col = NULL, lat.col = NULL)

Arguments

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 lon.col and lat.col are provided.

crs

A character string, number (EPSG value), or crs object specifying the coordinate reference system (see sf::st_crs() or 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.

Value

An sf::sf() object.

Examples

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

Description

Convert matrix or data frame with point coordinates to a SpatVector object

Usage

locs2vect(locs, crs = 4326, lon.col = NULL, lat.col = NULL)

Arguments

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 lon.col and lat.col are provided.

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.

Value

A terra::SpatVector() object.

Examples

locs <- matrix(runif(20), ncol = 2)
locs2vect(locs)

locs <- data.frame(species = rep("Laurus nobilis", 10), x = runif(10), y = runif(10))
locs2vect(locs)

Map species occurrences

Description

Plot map of species occurrences (or any set of point coordinates) on top of different background layers.

Usage

occmap(
  locs,
  ras = NULL,
  bg = "Esri.WorldImagery",
  type = c("base", "ggplot", "leaflet"),
  pcol = "red",
  alpha = 1,
  psize = 1,
  add = FALSE,
  prev.map = NULL,
  ...
)

Arguments

locs

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

ras

A terra::SpatRaster() object to be used as background for points. If NULL (default), a background map defined by bg will be used.

bg

Character. Type of background map to be used if ras is not provided. bg should be one of the providers listed in maptiles::get_tiles() if type is 'base' or 'ggplot', or one of the providers listed in leaflet::addProviderTiles() if type is 'leaflet'.

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 locs coordinates to a previous 'base' map? (e.g. for a new species).

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 terra::plot() if type = "base" tidyterra::geom_spatraster() if type = "ggplot" or leaflet::addCircleMarkers() if type = "leaflet".

Value

A map plus a leaflet or ggplot object, depending on type.

Examples

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

Check if points (occurrences) fall within raster cells with data

Description

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.

Usage

point_in_cell(locs = NULL, ras = NULL, layer = 1)

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

Value

A logical vector.

Examples

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