Create Builder input
Intro
This script creates the three required input to run BUILDER (neighbour, seeds and catchments tables).
The first step is to locate the catchments layer and point on an output directory where the derived files will be saved. The script uses three BUILDER functions: neighbour
, seeds
and reserve_seeds
.
The neighbours()
function calculates a list of neighbouring CATCHNUM pairs and returns them in a long tibble. Neighbours are defined as having at least on point in common (within 0.1m).
The seeds()
function creates a list of seed catchments and area targets. Prior to run the function, the user can apply a filter on the catchments dataset by specifying the level of intactness or the STHRALER index the seeds should have.
The reserve_seeds()
function creates a seeds table based on conservation areas.
It identifies seed catchments inside a conservation area and assigns them an area target.
All functions are input to builder()
and need the catchments dataset with unique identifier column ‘CATCHNUM’.
Generate neighbour table
# Load required libraries
library(sf)
library(utils)
# --------------------------------------
# SET PARAMS --------------------
# --------------------------------------
# Set working directory
<- here(".")
dirpath
source("./R/builder.R")
source("./R/utils.R")
# Create the folder structure
<- file.path(dirpath, "Builder_input")
input_dir if (!dir.exists(input_dir)) {
dir.create(input_dir, recursive = TRUE)
}
#Set access path catchment layer and initialize
<- st_read(file.path(dirpath, "data", "catchments_sample.shp"))
catchments_sf
# --------------------------------------
#--RUN
# --------------------------------------
# Generate neighbour table
<- neighbours(catchments_sf)
nghbrs
write.csv(nghbrs, file=file.path(dirpath, input_dir, "nghbrs.csv"), row.names=FALSE)
Generate seeds table
# Load required libraries
library(sf)
library(utils)
library(dplyr)
# --------------------------------------
# SET PARAMS --------------------
# --------------------------------------
# Set working directory
<- here(".")
dirpath
source("./R/builder.R")
source("./R/utils.R")
# Create the folder structure
<- file.path(dirpath, "Builder_input")
input_dir if (!dir.exists(input_dir)) {
dir.create(input_dir, recursive = TRUE)
}
#Set access path catchment layer and initialize
<- st_read(file.path(dirpath, "data", "catchments.shp"))
catchments
##OPTION: Filter potential seeds according to catchments intactness and STHRALER index
<- "intactKBA" # set catchment intactness column
intactColname <- 0.9 # set catchment intactness threshold
intact_threshold <- NULL # set STRAHLER index
STRAHLER_ref
##OPTION: Set area target value by choosing one of the following options. Leave argument to NULL if not chosen
## Option 1:
<- 100000000 #ex : areatarget <- 10000
areatarget ## Option 2:
<- NULL #ex : areatarget_col <- "area_thres"
areatarget_col ## Option 3:
<- NULL # ex: areatarget_poly <- st_read(file.path(dirpath, "data", "poly_areatarget.shp"))
areatarget_poly <- NULL # ex: areatarget_poly_col <-"area_thres"
areatarget_poly_col
# --------------------------------------
#--RUN
# --------------------------------------
# Filter catchments based on intactness and STRAHLER index, create seeds and convert to csv
<- catchments_sf %>%
seed_sf ::filter(.data[[intactColname]] >= intact_threshold,
dplyrif (!is.null(STRAHLER_ref)) STRAHLER == as.numeric(STRAHLER_ref) else TRUE)
if (!is.null(areatarget)) {
<- seeds(catchments_sf = seed_sf, areatarget_value = as.numeric(areatarget))
seed else if (!is.null(areatarget_col)) {
} # If areatarget is found in a column
<- seeds(catchments_sf = seed_sf, areatarget_col = areatarget_col)
seed else if (!is.null(areatarget_poly)) {
} # If area target is found in a polygon that intersect the catchments_sf
<- seeds(catchments_sf = seed_sf, areatarget_polygon = areatarget_poly, areatarget_polygon_col = areatarget_poly_col)
seed else {
} stop("You need to set at least one option") # Handle case where neither exists
}
write.csv(seed, file=file.path(dirpath, input_dir, "seeds.csv"), row.names=FALSE)
Generate reserve seeds table
# Load required libraries
library(sf)
library(dplyr)
library(utils)
# --------------------------------------
# SET PARAMS --------------------
# --------------------------------------
# Set working directory
<- here(".")
dirpath
source("./R/builder.R")
source("./R/utils.R")
# Create the folder structure
<- file.path(dirpath, "Builder_input")
input_dir if (!dir.exists(input_dir)) {
dir.create(input_dir, recursive = TRUE)
}
#Set access path catchment layer and initialize
<- st_read(file.path(dirpath, "data", "catchments_sample.shp"))
catchments_sf <- st_read(file.path(dirpath, "data", "reserves_sample.shp"))
pas_sf
# --------------------------------------
#--RUN
# --------------------------------------
<- reserve_seeds(catchments_sf, CAs_sf=pas_sf, CAs_name = "PARENT_ID", areatarget_value = 10000, joinType = "INTERSECT")
reserve_seed
# Save
write.csv(reserve_seed, file=file.path(dirpath, input_dir, "reserve_seeds.csv"), row.names=FALSE)
Generate catchments table
# Load required libraries
library(sf)
library(utils)
# --------------------------------------
# SET PARAMS --------------------
# --------------------------------------
# Set working directory
<- here(".")
dirpath
# Create the folder structure
<- file.path(dirpath, "Builder_input")
input_dir if (!dir.exists(input_dir)) {
dir.create(input_dir, recursive = TRUE)
}
#Set access path and initialize
<- st_read(file.path(dirpath, "data", "catchments.shp"))
catchments_sf
# --------------------------------------
#--RUN
# --------------------------------------
<- catchments_sf %>%
catchment st_drop_geometry()
write.csv(catchment, file=file.path(dirpath, input_dir, "catchments.csv"), row.names=FALSE)