---
title: "7. Riverspace delineation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{7. Riverspace delineation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  warning = FALSE
)
```

```{r setup}
library(rcrisp)
library(sf)

bucharest_osm <- get_osm_example_data()
bucharest_dem <- get_dem_example_data()

if (any(is.null(bucharest_osm), is.null(bucharest_dem))) {
  cat("NOTE: Example data was not found; ",
      "subsequent code chunks will be skipped.\n", sep = "")
  knitr::opts_chunk$set(eval = FALSE)
}
```

River space delineation is a delineation step that uses the river and buildings as input to generate a polygon representing the space between the river and the first line of buildings. We will use River Dâmbovița of Bucharest as the river and the buildings surrounding it as input.

```{r data}
buildings <- bucharest_osm$buildings
river <- bucharest_osm$river_surface
```

The `delineate_riverspace()` function takes the building polygons and the river surface polygons as input. If no river is provided, it will return an error message. If no buildings are provided, it will return an unobstructed buffer of a given radius with a warning message. The function returns an sf polygon.

```{r riverspace}
riverspace <- delineate_riverspace(river, buildings)
```

We visualise the riverspace in a subset of segments.

```{r plot-riverspace, fig.alt="Riverspace delineation"}
riverspace_segment_2_4 <- riverspace |>
  st_intersection(bucharest_dambovita$segments[2:4])

buildings_segment_2_4 <- buildings |>
  st_sf() |>
  st_filter(bucharest_dambovita$segments[2:4], .predicate = st_intersects) |>
  st_filter(riverspace_segment_2_4, .predicate = st_intersects)

plot(riverspace_segment_2_4, col = "orange", border = NA)
plot(buildings_segment_2_4, add = TRUE)
```
