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:
DetourRanker(credentials, at_pos, stop_pos, on_routing_request=..., route_cache=...)DetourRanker.retrieve_detours(...)DetourRanker.get_route_with_via(...)DetourRanker.rerank(resp, max_excursion=...)The
_detouritem 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
Truethe Search API request includesranking=excursionDistanceso results are pre-sorted by proximity to the route on the server side.- travel_time:
When
Truethe client appliesDetourRankerto 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
rankingrequest parameter, orNone.
- 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 fetchedat → stoproute is reused as the baseline.- routing_api:
Optional
RoutingAPIinstance; 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
Responsewhose items are sorted by ascending travel time.When all_along is
Trueitems are ranked bydur_to + dur_from(minimise total round-trip time).When all_along is
Falseitems are ranked bydur_toonly (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_excursionmetres are filtered out. Each surviving item gains a_detourannotation, 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 theat → itemleg_detour["polyline_from"]– encoded polyline for theitem → stopleg
The excursion distance is computed relative to the direct
at → stoproute (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-*headersraw – 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, elseNone
- 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