Skip to contents

This function converts the prioritization solution that uses patches to a raster or sf object (depending on the format of your spatial_grid) that clearly marks areas suggested for protection (1) and areas not suggested for protection (0)

Usage

convert_solution(solution, patch_df, spatial_grid)

Arguments

solution

the solution that results from using prioritizr::solve() on prioritizr::problem()

patch_df

a dataframe generated by create_patch_df() that includes constraints for each patch and grid cell combination

spatial_grid

a raster or sf template with the desired resolution and coordinate reference system; values in areas of interest are 1, while all other values are NA (only required if feature is a sf object)

Value

A raster or sf object that denotes areas suggested for protection (value = 1)

Examples

if (FALSE) { # \dontrun{
#import planning units/ cost raster
pu_raster <- terra::rast(system.file("extdata/pu_raster.tif", package = "patchwise"))
#import species distributions - feature data for planning
species_distributions <- terra::rast(system.file("extdata/spp_distributions.tif", package = "patchwise"))

#import seamounts
seamounts <- terra::rast(system.file("extdata/seamounts.tif", package = "patchwise"))

# Create seamount patches as multi-layer raster
patches_raster <- create_patches(seamounts)

patches_df <- create_patch_df(spatial_grid = pu_raster, features = species_distributions,
patches = patches_raster, costs = pu_raster)

# Create targets for protection - use 20% for each feature (including 20% of whole seamounts) in this example
targets_df <- features_targets(targets = rep(0.2, (terra::nlyr(species_distributions) + 1)), features = species_distributions, pre_patches = seamounts)

# Add these targets to targets for protection for the "constraints" we introduced to protect entire seamount patches
constraints_df <- constraints_targets(feature_targets = targets_df, patch_df = patches_df)

library(prioritizr)

# Create the prioritization problem
problem_patches <- problem(x = patches_df, features = constraints_df$feature, cost_column = "cost") |>
 add_min_set_objective() |>
 add_manual_targets(constraints_df) |>
 add_binary_decisions() |>
 add_default_solver()

# Solve the problem
solution_patches_tbl <- solve(problem_patches)

# Convert the solution into a raster using `convert_solution()`
sol_rast_patches <- convert_solution(solution = solution_patches_tbl, patch_df = patches_df, spatial_grid = pu_raster)

terra::plot(sol_rast_patches)
} # }