Polyline Simplification

Quick reference

The Search API rejects a route corridor parameter holding more than 2000 points, while a route of a few hundred kilometers comes back with several thousand. This notebook opens the simplify_polyline(...) method used in OneBoxMap:

  • the Douglas–Peucker simplification

  • the binary search that turns a tolerance into a point budget

  • the deviation it costs in meters

  • its effect on the corridor search.

Most-used elements:

  1. RoutingAPI.routes(...) → RouteSection.polyline and flexpolyline.decode/encode

  2. shapely.simplify(line, eps, preserve_topology=False) and shapely.get_num_coordinates(...)

  3. simplify_polyline(points, max_points, iterations=15)

  4. RouteEngine.max_waypoints_count / RouteEngine.build_flexpolyline(...)

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

  6. ipyleaflet.Polyline and a HERE TileLayer

The corridor polygon drawn in OneBoxMap app by the build_corridor(...) method is covered in the Route Engine. For the complete reference, see the API reference.

Methods used in this notebook

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]]

async here_search_demo.widgets.route_geometry.build_corridor(points, width)

Return a Shapely polygon that is a width-metre buffer around the polyline.

Parameters

points:

Route points in (lat, lon) or (lat, lon, z) order.

width:

Buffer width in metres.

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

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

class here_search_demo.route_engine.RouteEngine(credentials, *, routing_api=None, on_routing_request=None, route_cache=None, width=100, retrieve_route_fn=None)

Head-agnostic route state and routing retrieval service.

This class intentionally has no widget dependencies. It stores route/ranking state, computes derived search context, manages route cache, and retrieves route geometry through RoutingAPI.

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 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