## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(fig.width = 7, fig.height = 5, collapse = TRUE, comment = "#>")

## ----inputs-------------------------------------------------------------------
library(connectivity)
library(terra)

condition_file <- system.file("extdata", "site_condition.tif", package = "connectivity")
pa_file <- system.file("extdata", "pa_proportion.tif", package = "connectivity")
climate_dir <- system.file("extdata", "transgrids", package = "connectivity")

condition <- rast(condition_file)
pa <- rast(pa_file)
condition

global(condition, c("min", "max"), na.rm = TRUE)
global(pa, c("min", "max", "mean"), na.rm = TRUE)

## ----input-maps, fig.width=9, fig.height=4, fig.cap="Bundled habitat condition and protected-area proportions."----
plot(
    c(condition, pa),
    main = c("Habitat condition", "Protected fraction"),
    col = hcl.colors(100, "viridis"),
    range = c(0, 1)
)

## ----resolution-planning------------------------------------------------------
resolution_plan <- resolution_info(
    condition,
    levels = 2^(0:9),
    outer_window = 7,
    min_dimension = 16
)

## ----connectedness------------------------------------------------------------
conns <- connectedness(
    condition_file = condition,
    lambdas = c(2, 20, 50),
    max_cost = 2,
    window_size = 5,
    outer_window = 7,
    levels = c(2, 4, 8, 16, 32),
    option = 1,
    window_mode = "circular",
    n_threads = 2
)

plot(
    conns,
    main = "Connectedness",
    col = hcl.colors(100, "fall", rev = TRUE),
    range = c(0, 0.8)
)

## ----connected-habitat--------------------------------------------------------
connhabitat <- connectedness(
    condition_file = condition,
    lambdas = c(2, 20, 50),
    max_cost = 2,
    window_size = 5,
    outer_window = 7,
    levels = c(2, 4, 8, 16, 32),
    option = 3,
    window_mode = "circular",
    n_threads = 2
)

plot(
    connhabitat,
    main = "Connected habitat",
    col = hcl.colors(100, "fall", rev = TRUE),
    range = c(0, 0.8)
)

## ----parc---------------------------------------------------------------------
parc <- connectedness(
    condition_file = condition,
    pa_file = pa,
    pa_to_pa = FALSE,
    lambdas = c(2, 20, 50),
    max_cost = 2,
    window_size = 5,
    outer_window = 7,
    levels = c(2, 4, 8, 16, 32),
    window_mode = "circular",
    n_threads = 2
)

plot(
    parc,
    main = "PARC-connectedness",
    col = hcl.colors(100, "viridis"),
    range = c(0, 1)
)

## ----climate-inputs-----------------------------------------------------------
current_file <- file.path(climate_dir, "1990.tif")
scenario_names <- c(
    "ACC50_85", "GFD50_85", "IPS50_26",
    "IPS50_45", "IPS50_60", "IPS50_85"
)

future_files <- file.path(climate_dir, paste0(scenario_names, ".tif"))
basename(future_files)

current_climate <- rast(current_file)
nlyr(current_climate)

compareGeom(condition, current_climate, lyrs = FALSE)

## ----beri---------------------------------------------------------------------
beri_map <- beri(
    condition_file = condition,
    current_file = current_file,
    future_files = future_files,
    lambdas = c(2, 20, 50),
    max_cost = 2,
    window_size = 5,
    outer_window = 7,
    levels = c(2, 4, 8, 16, 32),
    window_mode = "circular",
    n_threads = 2
)

plot(
    beri_map,
    main = "BERI",
    col = hcl.colors(100, "RdYlGn"),
    range = c(0, 0.8)
)

## ----comparison, fig.width=10, fig.height=7, fig.cap="Habitat condition and the four connectivity indicators on the Tasmanian mainland."----
mainland <- ext(144.4, 148.6, -43.7, -40.6)
maps <- list(condition, conns, connhabitat, parc, beri_map)
titles <- c(
    "(a) Habitat condition",
    "(b) Connectedness",
    "(c) Connected habitat",
    "(d) PARC-connectedness",
    "(e) BERI"
)

condcol <- hcl.colors(100, "fall", rev = TRUE)
palettes <- list(
    condcol, condcol, condcol,
    hcl.colors(100, "viridis"), hcl.colors(100, "RdYlGn")
)

old_par <- par(mfrow = c(2, 3), mar = c(3, 3, 3, 4))
for (i in seq_along(maps)) {
    plot(
        terra::crop(maps[[i]], mainland),
        main = titles[i],
        col = palettes[[i]],
        range = c(0, if (i == 5) 0.8 else 1)
    )
}
plot.new()
par(old_par)

## ----tiles--------------------------------------------------------------------
tiles <- make_tiles(condition, nrows = 2, ncols = 2, align_to = 32)
tiles

tile_maps <- lapply(seq_len(nrow(tiles)), function(i) {
    return(connectedness(
        condition_file = condition,
        polygon_mask = tiles[i],
        lambdas = c(2, 20, 50),
        max_cost = 2,
        window_size = 5,
        outer_window = 7,
        levels = c(2, 4, 8, 16, 32),
        option = 3,
        window_mode = "circular",
        n_threads = 2
    ))
})
mosaic <- merge(sprc(tile_maps))

# The merged tiles reproduce the whole-raster calculation.
all.equal(values(mosaic), values(connhabitat))

## ----tile-files, eval=FALSE---------------------------------------------------
# tile_files <- sprintf("connected_habitat_tile_%02d.tif", tiles$id)
# for (i in seq_len(nrow(tiles))) {
#     connectedness(condition_file, polygon_mask = tiles[i], filename = tile_files[i])
# }
# 
# habitat <- vrt(tile_files)
# # Or write one merged file:
# habitat <- merge(sprc(tile_files), filename = "connected_habitat.tif")

## ----data-documentation, eval=FALSE-------------------------------------------
# file.show(system.file("extdata", "README.md", package = "connectivity"))

