Skip to contents

This function is a metafunction with several functions inside of it it takes several spatial objects and generates a .dat file with a spatial dataset for AMPL

Usage

troublemaker(
  Rasterdomain = NULL,
  Rastercurrent = NULL,
  species_names = NULL,
  Rasterspecieslanduse = NULL,
  landuses = NULL,
  budget = NULL,
  Rastercurrentlanduse = NULL,
  directions = NULL,
  name = "Problem",
  verbose = FALSE
)

Arguments

Rasterdomain

A Raster object with any value in the cells that are part of the problem and NA values where the problem is not to be solved

Rastercurrent

raster object of current suitability

species_names

a vector with the names of species

Rasterspecieslanduse

a list of species suitability for each landuse

landuses

character vector with all landuses

budget

maximum cost for the problem

Rastercurrentlanduse

raster object of current landuses

directions

just as in adjacent character or matrix to indicated the directions in which cells are considered connected. The following character values are allowed: "rook" or "4" for the horizontal and vertical neighbors; "bishop" to get the diagonal neighbors; "queen" or "8" to get the vertical, horizontal and diagonal neighbors; or "16" for knight and one-cell queen move neighbors. If directions is a matrix it should have odd dimensions and have logical (or 0, 1) values

name

The name of the output file

verbose

Logical whether messages will be written while the function is generating calculations, defaults to FALSE

Value

A .dat file with the spatial problem formated for AMPL. This function is used for the side-effect of writing values to a file.

Author

Derek Corcoran

Examples

# Example 1 with current suitabilities
data(Species)
data(Current)
library(terra)
Test <- Species[[1]] |>
terra::unwrap()

Current <- terra::unwrap(Current)

# Generate the "Problem.dat" file

TroublemakeR::troublemaker(Rasterdomain =Test[[1]],
Rastercurrent = Current,
species_names = c("Spp1", "Spp2", "Spp3", "Spp4"),
name = "Problem")

# delete the file so the test on cran can pass this

file.remove("Problem.dat")
#> [1] TRUE

# Example 2 with landuse suitabilities

data(Species)
data("Species_Landuse")

library(terra)
Test <- Species[[1]] |>
terra::unwrap()

Species_Landuse <- Species_Landuse |> purrr::map(terra::unwrap)

# Generate the "Problem2.dat" file

TroublemakeR::troublemaker(Rasterdomain =Test[[1]],
Rasterspecieslanduse = Species_Landuse,
species_names = c("Spp1", "Spp2", "Spp3", "Spp4"),
landuses = c("Agriculture", "Forest", "Urban"),
name = "Problem2")

# delete the file so the test on cran can pass this

file.remove("Problem2.dat")
#> [1] TRUE

 # Example 3 with budget and transition cost

 data("CurrentLanduse")
 CurrentLU <- terra::unwrap(CurrentLanduse)
 TroublemakeR::troublemaker(Rasterdomain =Test[[1]],
 Rasterspecieslanduse = Species_Landuse,
 species_names = c("Spp1", "Spp2", "Spp3", "Spp4"),
 landuses = c("Agriculture", "Forest", "Urban"),
 Rastercurrentlanduse = CurrentLU,
 budget = 2,
 name = "Problem3",
 verbose = FALSE)
#>           used (Mb) gc trigger  (Mb) max used  (Mb)
#> Ncells 1665926   89    3056375 163.3  3056375 163.3
#> Vcells 3003050   23   12413039  94.8 10996699  83.9

 file.remove("Problem3.dat")
#> [1] TRUE