Routing APIs

Routing directions between locations, travel distance or time origin-destination matrices and isolines for points of interest (POIs) based on the ‘HERE Routing’, ‘HERE Matrix Routing’ and ‘HERE Isoline Routing’ APIs.

Routing directions

In order to calculate route geometries (LINESTRING) between pairs of points using the ‘HERE Routing API’ the function route() is used. The function takes origin and destination locations as sf objects containing geometries of type POINT as input. Routes can be created for various transport modes, as for example car or public transport. Optionally the current or predicted traffic information is considered. For routes using the transport mode "car" a vehicle type can be specified, to obtain an estimate of the energy consumption on the routes.

origin <- poi[1:2, ]
destination <- poi[3:4, ]
routes <- route(
  origin = origin,
  destination = destination
)
id rank section departure arrival type mode distance duration duration_base consumption tolls
1 1 1 2022-07-24 15:28:45 2022-07-24 17:38:26 vehicle car 211765 7781 7581 83.1847 40.91
2 1 1 2022-07-24 15:28:45 2022-07-24 18:31:36 vehicle car 265717 10971 10015 102.8417 40.91

Construct a route label and print the routes on an interactive leaflet map:

routes$label <- paste(origin$city[routes$id],
                      destination$city[routes$id],
                      sep =  " - ")

if (requireNamespace("mapview", quietly = TRUE)) {
  mapview::mapview(routes,
                   zcol = "label",
                   layer.name = "Route [O-D]",
                   map.types = c("Esri.WorldTopoMap"),
                   homebutton = FALSE
  )
}

Matrix routing

The function route_matrix() calculates a matrix of route summaries between given POIs. The function takes origin and destination locations as sf objects containing geometries of type POINT as input. If only one sf object is provided as origin an origin-destination matrix, which covers all route combinations, is constructed. Various transport modes and current or predicted traffic information are supported. The requested matrix is split into (sub-)matrices of dimension 15x100 in order to use the maximum allowed matrix size per request. Thereby the number of overall needed requests is minimized. The return value of the function route_matrix is one route summary matrix, that fits the order of the provided POIs: orig_id, dest_id.

# From - to
mat <- route_matrix(
  origin = poi[1:2, ],
  destination = poi[3:4, ]
)

# Construct O-D matrix (all routes between the POIs)
mat <- route_matrix(
  origin = poi
)

Print the first 10 rows of the matrix table, created from the POIs above, where the distance is in meters, the travel time in seconds and the consumption in cost factor units:

orig_id dest_id request_id departure arrival type mode distance duration error_code
1 1 1 2022-07-24 15:28:45 2022-07-24 15:28:45 fast car 0 0 0
1 2 1 2022-07-24 15:28:45 2022-07-24 17:42:15 fast car 173394 8010 0
1 3 1 2022-07-24 15:28:45 2022-07-24 17:38:14 fast car 211774 7769 0
1 4 1 2022-07-24 15:28:45 2022-07-24 16:33:28 fast car 99336 3883 0
1 5 1 2022-07-24 15:28:45 2022-07-24 16:48:53 fast car 117576 4808 0
1 6 1 2022-07-24 15:28:45 2022-07-24 16:15:09 fast car 55400 2784 0
1 7 1 2022-07-24 15:28:45 2022-07-24 18:20:27 fast car 266526 10302 0
1 8 1 2022-07-24 15:28:45 2022-07-24 16:56:48 fast car 131879 5283 0
2 1 1 2022-07-24 15:28:45 2022-07-24 17:40:58 fast car 171885 7933 0
2 2 1 2022-07-24 15:28:45 2022-07-24 15:28:45 fast car 0 0 0

Isoline routing

Isolines are constructed by the function isoline(). The calculated polygons (POLYGON or MULTIPOLYGON) connect the end points of all routes leaving from defined centers (POIs) with either a specified length (isodistance), a specified travel time (isochrone) or consumption (isoconsumption), whereby time is measured in seconds, distance in meters and consumption. By default the aggregate parameter is set to TRUE, which means that the isoline polygons are intersected and the minimum range value (time, distance or consumption) is taken in all intersecting areas, then the polygons are aggregated to polygons of geometry type MULTIPOLYGON. Thereby overlapping isolines are avoided.

iso <- isoline(
  poi,
  range = seq(5, 30, 5) * 60,
  range_type = "time",
  routing_mode = "fast",
  transport_mode = "car",
  aggregate = TRUE,
  traffic = FALSE
)

Convert range from seconds to minutes and print the aggregated isolines on an interactive leaflet map:

iso$minutes <- iso$range/60

if (requireNamespace("mapview", quietly = TRUE)) {
  mapview::mapview(iso,
          zcol = "minutes",
          layer.name = "Isoline [min]",
          alpha = 0,
          map.types = c("Esri.WorldTopoMap"),
          homebutton = FALSE
  )
}

API Reference