Decoding the shape of the route as a flexible-polyline returned in Routing API v8 by python
Last Updated: November 2022
Product: HERE Routing API v8
Audience: External developers
Article Type: How-to / Technical Guide
Overview
In HERE Routing API v8, the route geometry is not returned as a list of coordinates. Instead, the route shape is provided as an encoded string using the Flexible Polyline format.
This article explains:
- What a flexible polyline is
- Where to find it in a Routing API v8 response
- How to decode it using Python
What Is a Flexible Polyline?
A flexible polyline is a compact string representation of a sequence of geographic coordinates (and optionally relative heights).
It significantly reduces payload size compared to raw coordinate arrays.
The Routing API v8 returns this encoded polyline in the polyline field of each route section.
Prerequisites
Before proceeding, ensure the following:
- A valid HERE API key
- Python 3.x
pip install requests flexpolyline
Example: Calling Routing API v8 and Decoding the Polyline
The following Python example demonstrates:
- Calling the Routing API v8
- Extracting the
polylinefrom the response - Decoding it into latitude/longitude coordinates
Step 1: Import Required Libraries
import requests
import json
import flexpolyline as fp
Step 2: Define Parameters
apikey = 'YOUR_API_KEY'
origin_point = '43.435236,-80.444766'
destination_point = '43.41117,-80.4937'
via_point = '43.41517,-80.5917'
Step 3: Call the Routing API v8
url = 'https://router.hereapi.com/v8/routes'
headers = {'Content-Type': 'application/json'}
params = {
'origin': origin_point,
'destination': destination_point,
'via': via_point,
'transportMode': 'car',
'return': 'summary,polyline,travelSummary,instructions,actions,elevation',
'spans': 'names,speedLimit,carAttributes',
'apikey': apikey
}
response = requests.get(url, params=params, headers=headers)
print('HTTP Status Code:', response.status_code)
Step 4: Parse the Response and Extract the Polyline
output = json.loads(response.text)
# Retrieve the encoded flexible polyline
polyline = output["routes"][0]["sections"][0]["polyline"]
print('Response Language:',
output["routes"][0]["sections"][0]["language"])
print('Encoded Flexible Polyline:')
print(polyline)
Step 5: Decode the Flexible Polyline
decoded_coordinates = fp.dict_decode(polyline)
for index, coordinate in enumerate(decoded_coordinates, start=1):
print(f'Decoded waypoint #{index}: {coordinate}')
Each decoded waypoint is returned as a dictionary containing latitude and longitude values.
Result
After decoding, the flexible polyline is converted into a list of geographic coordinates, which can be:
- Rendered on a map
- Exported to GeoJSON
- Used for spatial analysis or visualization
Related Links
- HERE Flexible Polyline GitHub Repository
- HERE Routing API v8 Documentation
Notes
- Each route may contain multiple sections, each with its own polyline.
- Always decode the correct
sections[n].polylineentry for the part of the route you need.