---
title: "Intermodal Routing API"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Intermodal Routing API}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

if (requireNamespace("mapview", quietly = TRUE)) {
  mapview::mapviewOptions(
    fgb = FALSE,
    vector.palette = grDevices::colorRampPalette(
      c("#000004FF", "#420A68FF", "#932667FF", "#DD513AFF", "#FCA50AFF", "#FCFFA4FF")
    )
  )
}

intermodal_routes <- hereR:::example$intermodal_route
```

Routing intermodal directions between locations based on the 'HERE Intermodal Routing' API.

## Intermodal routes

In order to calculate route geometries (`LINESTRING`) between pairs of points using the 'HERE Intermodal Routing API' the function `intermodal_route()` is used. The function takes origin and destination locations as `sf` objects containing geometries of type `POINT` as input. Routes can be limited to a maximum number of allowed transfers (includes mode changes and public transit transfers), by specifying the `transfer` parameter.

```{r int_directions, eval=FALSE}
# Request routes
intermodal_routes <- route(
  origin = poi[1:3, ],
  destination = poi[4:6, ]
)
```

The `id` column corresponds to the row of the input locations (`origin` and `destination`) and the `rank` column enumerates the alternative routes. The maximum number of alternatives can be set by the `results` parameter. Each row in the returned `sf` object corresponds to a route section with a transport mode in a vehicle without a transfer.

```{r table_int_directions, eval=TRUE, echo=FALSE, out.width='100%', fig.align='center', screenshot.force=FALSE}
knitr::kable(head(as.data.frame(intermodal_routes)[, colnames(intermodal_routes) != "geometry"]), format = "html")
```

Print the intermodal routes on an interactive leaflet map:

```{r map_int_routes, eval=FALSE, out.width='100%'}
if (requireNamespace("mapview", quietly = TRUE)) {
  mapview::mapview(intermodal_routes,
    zcol = "mode",
    layer.name = "Intermodal route",
    map.types = c("Esri.WorldTopoMap"),
    homebutton = FALSE
  )
}
```

## API Reference

- [Intermodal Routing API](https://www.here.com/docs/bundle/intermodal-routing-api-developer-guide/page/README.html)