---
title: "Isobath corridors"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Isobath corridors}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)
```

```{r setup, message = FALSE}
library(blueterra)
```

Isobath corridors summarize terrain along depth horizons. They are useful when
observations are collected along contours or when terrain structure needs to be
compared at equivalent depths while retaining along-contour variability.

```{r data}
bathy <- read_bathy(blueterra_example("hitw"))
prepared <- prepare_bathy(bathy, depth_range = c(-220, -25), smooth = TRUE)
terrain <- derive_terrain(
  prepared,
  metrics = c("slope", "bpi", "curvature", "surface_area_ratio")
)
```

The example bathymetry stores depth as negative elevation, so contour depths are
passed as negative values.

```{r isobaths}
isobaths <- extract_isobaths(prepared, depths = c(-50, -80, -120))
isobaths[, c("contour_value", "depth_label")]
```

```{r isobath-map, eval=requireNamespace("ggplot2", quietly = TRUE), fig.alt="Isobaths over hillshaded bathymetry."}
plot_bathy(
  prepared,
  contours = TRUE,
  contour_interval = 25,
  vectors = isobaths,
  vector_color = "black",
  title = "Isobaths Over Hillshaded Bathymetry"
)
```

Buffers are measured in map units, so the raster should use a projected CRS.
The example raster is projected; longitude/latitude rasters generate a warning
because their map-unit widths are usually not interpretable distances.
Here `width = 5` is a one-sided buffer distance, so the nominal full corridor
width is 10 m.

```{r corridors}
corridors <- make_isobath_corridors(
  prepared,
  depths = c(-50, -80, -120),
  width = 5
)
corridors[, c(
  "contour_value", "depth_label", "corridor_id", "buffer_distance",
  "nominal_corridor_width", "overlap_policy"
)]
```

```{r corridor-map, eval=requireNamespace("ggplot2", quietly = TRUE), fig.alt="Isobath corridors over hillshaded bathymetry."}
plot_isobath_corridors(
  corridors,
  prepared,
  isobaths = isobaths,
  background_contours = FALSE,
  title = "Isobath Corridors and Source Isobaths",
  subtitle = "5 m is the one-sided buffer distance (10 m nominal full width)"
)
```

The black lines are the source isobaths. The corridor polygons buffer those
depth horizons by 5 m on each side and define the terrain extraction zones used
in the summary. Corridors are independent buffers and may overlap; values in an
overlap can contribute to more than one corridor, so corridor summaries are not
mutually exclusive or additive.

## Extract and Summarize Terrain

```{r summarize}
cells <- extract_isobath_corridors(terrain, corridors)
head(cells)

summary <- summarize_isobath_terrain(terrain, corridors)
summary[, c("contour_value", "slope_deg_mean", "bpi_3x3_mean", "curvature_mean")]
```

The summary compares terrain structure at the selected depth horizons. BPI
values should be interpreted using the raster's stored vertical convention.
