ConScape / ConScapeR

Other
3 stars 1 forks source link

ConScapeR

ConScapeR provides a R wrapper to the ConScape.jl library in Julia, which allows the use of ConScape from an R interface. However, using ConScape from the Julia interface is still recommended for full flexibility.

ConScape (i.e. 'connected landscapes') is a software library implemented in the high-performance open-source Julia language to compute landscape ecological metrics — originally developed in metapopulation ecology (such as 'metapopulation capacity' and 'probability of connectivity') — for large landscapes. Moreover,in addition to traditional distance metrics used in ecology (i.e. Euclidean and least-cost distance), ConScape implements the randomized shortest paths framework to represent connectivity along the continuum from optimal to random movements.

Installation

To run ConScape through R, you need to download and install Julia to your computer, which R will run on the background.

You will need to provide the path to this Julia installation. An easy way to do this is by adding it to the .Renviron file (more information here), one way to edit this file is with the usethis library:

library(usethis)
usethis::edit_r_environ()

Then add to this file the following line:

JULIA_PATH = "your_path_to...../Julia-1.9.3/bin

From here on you can call this path with:

Sys.getenv("JULIA_PATH")

To install the development version of the ConScapeR package in R:

library(devtools)
devtools::install_github("ConScape/ConScapeR", ref = "HEAD")

Finally, install the ConScape library in Julia:

library(ConScapeR)

# If the ConScape library is not installed in Julia, run:
ConScapeR_setup(Sys.getenv("JULIA_PATH"), install_libraries=TRUE)

Example

This is an example demonstrating the basic workflow:

library(ConScapeR)
library(terra)

# Launch Julia
ConScapeR_setup(Sys.getenv("JULIA_PATH"))

# Create a SpatRaster from a file for the landscape permeability or affinities and habitat suitability
aff <- terra::rast(system.file("data/affinities_2000.asc", package="ConScapeR"))
hab <- terra::rast(system.file("data/suitability_2000.asc", package="ConScapeR"))
plot(aff)
plot(hab)

# Create a ConScape Grid
g <- ConScapeR::Grid(affinities=aff, sources=hab, targets=hab, costs="x -> -log(x)")

# Create a ConScape GridRSP by providing the randomness parameter theta
# note: on larger graphs this may be a computation intensive step
h <- ConScapeR::GridRSP(g, theta=0.1)

# Compute quality-weighted betweenness
betw <- ConScapeR::betweenness_qweighted(h)

# Convert matrix to raster
betw <- ConScapeR::mat2rast(betw)
plot(betw)

# Compute quality-and proximity-weighted betweenness (and convert to raster)
betw <- ConScapeR::mat2rast(ConScapeR::betweenness_kweighted(h, alpha=1/100), aff)
plot(betw)

# Compute habitat functionality (and convert to raster)
func <- ConScapeR::mat2rast(ConScapeR::connected_habitat(h, alpha=1/100), aff)
plot(func)