---
title: "2. Getting OSM data for delineation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{2. Getting OSM data for delineation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

bucharest_osm <- rcrisp::get_osm_example_data()
bucharest_dem <- rcrisp::get_dem_example_data()
```

```{r setup, include = FALSE}
library(rcrisp)
library(purrr)

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

city_boundary <- bucharest_osm$boundary
river_surface <- bucharest_osm$river_surface
river_centerline <- bucharest_osm$river_centerline
railways <- bucharest_osm$railways
streets <- bucharest_osm$streets
```

In this notebook we download OSM data needed for the delineation of the urban river corridor of River Dâmbovița in Bucharest, Romania. We make sure that we include a given area around the city boundaries.

```{r}
city_name <- "Bucharest"
river_name <- "Dâmbovița"
crs <- 32635  # EPSG code for UTM zone 35N
bbox_buffer <- 2000  # in m, expand bbox for street network
```

We start by getting the bounding box for the study area:

```{r}
bb <- get_osm_bb(city_name)
```

Using the obtained bounding box, we get the different layers of OSM data needed for the delineation of the urban river corridor. We will get the city boundary, the waterways, the street network, and the rail network using built-in functions from the `rcrisp` package.

```{r}
city_boundary <- get_osm_city_boundary(city_name, bb, crs)
river <- get_osm_river(river_name, bb, crs)
streets <- get_osm_streets(bb, crs)
railways <- get_osm_railways(bb, crs)
```

Individual layers can be written to disk before being read in for delineation.

```{r}
bucharest_osm <- list(
  bb = bb,
  boundary = city_boundary,
  river_centerline = river$centerline,
  river_surface = river$surface,
  streets = streets,
  railways = railways
)

walk2(
  bucharest_osm,
  names(bucharest_osm),
  ~ st_write(
    .x,
    dsn = sprintf("%s_%s.gpkg", .y, city_name),
    append = FALSE,
    quiet = TRUE
  )
)
```

The above layers can also be obtained with the all-in-one function `get_osmdata()`. Optionally, a bounding box buffer can be specified to expand the bounding box.

```{r}
bucharest_osm <- get_osmdata(city_name, river_name, buffer = bbox_buffer)
```

The resulting object is a list with all the layers obtained individually above.

```{r}
names(bucharest_osm)
```


```{r, eval=TRUE, fig.alt="All layers combined", fig.cap="All layers combined"}
if (requireNamespace("ggplot2", quietly = TRUE)) {
  library(ggplot2)
  ggplot() +
    geom_sf(data = bucharest_osm$boundary, fill = "grey", color = "black") +
    geom_sf(data = bucharest_osm$railways, color = "orange") +
    geom_sf(data = bucharest_osm$streets, color = "black") +
    geom_sf(data = bucharest_osm$river_surface, fill = "blue", color = "blue") +
    geom_sf(data = bucharest_osm$river_centerline, color = "blue") +
    xlim(417000, 439000) +
    ylim(4908800, 4932500)
} else {
  message("ggplot2 not available; skipping plot examples.")
}
```

