DetourRanker

Quick reference

This notebook focuses on the client-side detour reranking: via-route Routing API calls, caching, excursion filtering and result annotations. Most-used elements:

  1. DetourRanker(credentials, at_pos, stop_pos, on_routing_request=..., route_cache=...)

  2. DetourRanker.retrieve_detours(...)

  3. DetourRanker.get_route_with_via(...)

  4. DetourRanker.rerank(resp, max_excursion=...)

  5. The _detour item annotation (label, excursion_m, polyline_to, polyline_from)

For the complete reference, see the API reference. For the ranking behaviour and label semantics, see Search Along the Route. For the reranking rules, see Route math.

Methods used in this notebook

class here_search_demo.ranking.RankingMode(all_along=False, travel_time=False)

Immutable description of the current ranking configuration.

Parameters

all_along:

When True the Search API request includes ranking=excursionDistance so results are pre-sorted by proximity to the route on the server side.

travel_time:

When True the client applies DetourRanker to reorder results by actual driving time and annotate each item with detour polylines.

property needs_client_rerank: bool

Whether the response requires client-side travel-time reranking.

property needs_route_polyline: bool

Whether a route (corridor) must be sent with the search request.

property server_ranking: str | None

Value for the ranking request parameter, or None.

class here_search_demo.detour.DetourRanker(credentials, at_pos, stop_pos, on_routing_request=None, route_cache=None, routing_api=None)

Reranks search result items by minimal excursion distance from a route.

Parameters

credentials:

HERE API credentials used to authenticate Routing requests.

at_pos:

(latitude, longitude) of the current position on the route. Used as the origin for routing calculations.

stop_pos:

(latitude, longitude) of the route destination.

on_routing_request:

Optional callback invoked before each Routing request that is not served by route_cache.

route_cache:

Route cache shared with RouteEngine, so an already fetched at → stop route is reused as the baseline.

routing_api:

Optional RoutingAPI instance; one is created from credentials when omitted.

async get_route_with_via(session, start_lat, start_lon, via_lat, via_lon, stop_lat, stop_lon, headers, name_hint=None)

Call the Routing API with a via waypoint and return route segments.

Return type:

tuple[int, int, str, int, int, str]

Returns:

(dur_to, dist_to, poly_to, dur_from, dist_from, poly_from) where: - dur_to, dist_to, poly_to: segment from start to via - dur_from, dist_from, poly_from: segment from via to stop

async rerank(resp, all_along=False, max_excursion=None)

Return a new Response whose items are sorted by ascending travel time.

  • When all_along is True items are ranked by dur_to + dur_from (minimise total round-trip time).

  • When all_along is False items are ranked by dur_to only (minimise time to reach the result from the current position; the return leg is shown in the label but does not affect order).

Items whose excursion distance exceeds max_excursion metres are filtered out. Each surviving item gains a _detour annotation, whose most useful keys are:

  • _detour["label"] – <title> (<dur_to>min, <dur_from>min, +<detour_km>km)

  • _detour["rank_before"] – 0-based position of the item in resp, before reranking

  • _detour["polyline_to"] – encoded polyline for the at → item leg

  • _detour["polyline_from"] – encoded polyline for the item → stop leg

The excursion distance is computed relative to the direct at → stop route (not the full route from its origin), so it correctly reflects the extra distance added to the remaining journey.

Parameters

resp:

The original search response.

max_excursion:

Maximum allowed excursion in metres. Items beyond this threshold are dropped from the reranked response.

rtype:

Response

class here_search_demo.entity.response.Response(req, data, x_headers=<factory>, raw=None)

Immutable HERE Search response wrapper.

Variables:
  • req – originating request

  • data – parsed JSON payload view

  • x_headers – captured response X-* headers

  • raw – optional raw response body

bbox()

Return response bounding rectangle.

Return type:

Tuple[float, float, float, float] | None

Returns:

tuple (south_lat, north_lat, east_lng, west_lng) when positioned items exist, else None

geojson()

Return a GeoJSON FeatureCollection for items that include position.

Return type:

dict

Returns:

GeoJSON mapping containing only items with coordinates

property titles

Return response item titles in display order.

Returns:

list of item titles