Routing Client

Quick reference

This notebook introduces RoutingAPI, the low-level HERE Routing API v8 client used by RouteEngine and DetourRanker, two classes used in the OneBoxMap application. It is the entry point for a team that wants to add detour reranking to an application built on its own route and ranking classes. Most-used elements:

  1. RoutingAPI(credentials, cache=..., options=..., on_request_sent=...)

  2. RoutingAPI.routes(session, origin, destination, via=..., return_fields=..., spans=...)

  3. Waypoint(latitude, longitude, name_hint=...) / RoutingOptions(...)

  4. RoutingAPI.sections(response) → RouteSection(duration, length, polyline, spans)

  5. flexpolyline.decode/encode and simplify_polyline(...)

  6. API.browse(..., polyline=..., width=...)

For the high-level equivalent, see Route Engine page. For the complete reference, see the API reference.

Methods used in this notebook

class here_search_demo.routing_api.RoutingAPI(credentials=None, cache=None, raise_for_status=True, options=None, log_fn=None, on_request_sent=None)

https://www.here.com/docs/bundle/routing-api-v8-api-reference/page/index.html

auth_headers(token)

Return HTTP request headers with the bearer token in the Authorization field.

Parameters:

token (str) – an OAuth2 access token

Return type:

dict

Returns:

authorization headers

static query(params)

Return params in the form expected by the runtime HTTP session.

lite.HTTPSession encodes the query with doseq=True and therefore accepts list values, while aiohttp requires repeated two-tuples.

Return type:

Any

async routes(session, origin, destination, via=None, transport_mode=None, return_fields=None, spans=None, x_headers=None, **kwargs)

Calls the HERE Routing /v8/routes endpoint.

Parameters:
  • session (ClientSession) – instance of HTTPSession

  • origin – route origin, a Waypoint or a (lat, lon[, name_hint]) tuple

  • destination – route destination, in the same formats as origin

  • via – optional intermediate waypoint(s), in the same formats as origin

  • transport_mode (str | None) – overrides the instance transportMode option

  • return_fields (Sequence[str] | None) – overrides the instance return option

  • spans (Sequence[str] | None) – overrides the instance spans option

  • x_headers (dict | None) – Optional X-* headers (X-Request-Id, …)

  • kwargs – additional request URL parameters

Return type:

Response

Returns:

a Response object whose data is the routing payload

static sections(response, route_index=0)

Return the sections of one route of a routing response.

The Routing API starts a new section at each via waypoint and wherever the journey changes transport mode – a Berlin/London route is split by the channel ferry, for instance. Callers that need the at → result and result → stop legs must therefore group these sections with split_at() and merge_sections() rather than assume one section per leg.

Parameters:
  • response (Response) – a Response returned by routes()

  • route_index (int) – index in the routes[] array

Return type:

list[RouteSection]

Returns:

list of RouteSection

async send(session, request)

Return the Routing API response for request, from the cache when possible.

Parameters:
  • session (ClientSession) – instance of HTTPSession

  • request (Request) – Routing Request object

Return type:

Response

Returns:

a Response object

class here_search_demo.routing_api.Waypoint(latitude, longitude, name_hint=None)

A Routing API waypoint.

Variables:
  • latitude – waypoint latitude

  • longitude – waypoint longitude

  • name_hint – optional street name passed as the nameHint waypoint option; it helps the router pick the right side of a complex intersection

classmethod make(value)

Return value as a Waypoint.

Parameters:

value – a Waypoint, a (lat, lon) or a (lat, lon, name_hint) tuple

Return type:

Waypoint

Returns:

a Waypoint instance

class here_search_demo.routing_api.RouteSection(duration, length, polyline, spans=<factory>)

One sections[] entry of a routing response.

Variables:
  • duration – travel duration in seconds

  • length – travel distance in meters

  • polyline – encoded flexible polyline of the section geometry

  • spans – the section spans[] when they were requested

class here_search_demo.routing_api.RoutingOptions(transport_mode='car', routing_mode='fast', return_fields=('summary', 'polyline'), spans=None)

Default request parameters of a RoutingAPI instance.

Variables:
  • transport_mode – value of the transportMode parameter

  • return_fields – values joined into the return parameter

  • spans – values joined into the spans parameter, or None

here_search_demo.widgets.route_geometry.simplify_polyline(points, max_points, iterations=15)

Reduce points to at most max_points using GEOS Douglas–Peucker.

Return type:

list[tuple[float, float]]

class here_search_demo.api.API(credentials=None, cache=None, raise_for_status=True, options=None, log_fn=None, on_request_sent=None, testing_header=False)

https://docs.here.com/geocoding-and-search/reference

async autosuggest(session, q, latitude, longitude, polyline=None, width=None, all_along=None, x_headers=None, **kwargs)

Calls HERE Search Autosuggest endpoint

Parameters:
  • session (ClientSession) – instance of ClientSession

  • q (str) – query text

  • latitude (float) – search center latitude

  • longitude (float) – search center longitude

  • x_headers (dict | None) – Optional X-* headers (X-Request-Id, X-AS-Session-ID, …)

Param:

kwargs: additional request URL parameters

Return type:

Response

Returns:

a Response object

async browse(session, latitude, longitude, categories=None, food_types=None, chains=None, polyline=None, width=None, all_along=None, x_headers=None, **kwargs)

Calls HERE Search Browse endpoint

Parameters:
  • session (ClientSession) – instance of HTTPSession

  • latitude (float) – search center latitude

  • longitude (float) – search center longitude

  • categories (Sequence[str] | None) – Places category ids for filtering

  • food_types (Sequence[str] | None) – Places cuisine ids for filtering

  • chains (Sequence[str] | None) – Places chain ids for filtering

  • x_headers (dict | None) – Optional X-* headers (X-Request-Id, X-AS-Session-ID, …)

Param:

kwargs: additional request URL parameters

Return type:

Response

Returns:

a Response object

async discover(session, q, latitude, longitude, polyline=None, width=None, all_along=None, x_headers=None, **kwargs)

Calls HERE Search Discover endpoint

Parameters:
  • session (ClientSession) – instance of HTTPSession

  • q (str) – query text

  • latitude (float) – search center latitude

  • longitude (float) – search center longitude

  • x_headers (dict | None) – Optional X-* headers (X-Request-Id, X-AS-Session-ID, …)

Param:

kwargs: additional request URL parameters

Return type:

Response

Returns:

a Response object