Decode Route Geometry from HERE Routing API v8 Flexible Polyline Using Python
Problem
-------
When using the HERE Routing API v8, route geometry is not returned as a traditional array of latitude and longitude coordinates. Instead, route shapes are encoded in the Flexible Polyline format.
Developers who need to visualize routes on a map, export route geometry, or perform spatial analysis must first decode the polyline returned by the API.
This article explains how to extract and decode route geometry from a Routing API v8 response using Python.
---
Environment
-----------
HERE Routing API v8
Python 3.x
Valid HERE API Key
Required Python packages:
pip install requests flexpolyline
---
Background
----------
### What is a Flexible Polyline?
Flexible Polyline is a compact encoding format used to represent a sequence of geographic coordinates. Compared with returning raw coordinate arrays, flexible polyline significantly reduces response payload size while preserving route geometry.
In Routing API v8 responses, the encoded route shape is returned in the following field:
routes[].sections[].polyline
Each route section contains its own polyline and must be decoded separately.
---
Solution
--------
### Step 1: Import Required Libraries
import json
import requests
import flexpolyline as fp
---
### Step 2: Configure Request 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 Routing API v8
url = "https://router.hereapi.com/v8/routes"
headers = {<br /><br />"Content-Type": "application/json"<br /><br />}
params = {<br /><br />"origin": origin_point,<br /><br />"destination": destination_point,<br /><br />"via": via_point,<br /><br />"transportMode": "car",<br /><br />"return": "summary,polyline,travelSummary,instructions,actions,elevation",<br /><br />"spans": "names,speedLimit,carAttributes",<br /><br />"apikey": apikey<br /><br />}
response = requests.get(
url,
params=params,
headers=headers
)
print("HTTP Status Code:", response.status_code)
---
### Step 4: Extract the Flexible Polyline
output = json.loads(response.text)
polyline = output["routes"][0]["sections"][0]["polyline"]
print(
"Response Language:",
output["routes"][0]["sections"][0]["language"]
)
print("Encoded Flexible Polyline:")
print(polyline)
Example:
BFoz5xJ67i1B1B7PzIhaxL7Y...
---
### Step 5: Decode the Polyline
decoded_coordinates = fp.dict_decode(polyline)
for index, coordinate in enumerate(
decoded_coordinates,
start=1):
print(
f"Decoded waypoint #{index}: {coordinate}"
)
---
Expected Result
---------------
After decoding, the flexible polyline is converted into geographic coordinates that can be used for:
Displaying route geometry on a map
Converting route shapes to GeoJSON
Performing spatial analysis
Route visualization and debugging
Example output:
{<br /><br />"lat": 43.435236,<br /><br />"lng": -80.444766<br /><br />}
---
Validation
----------
To verify successful decoding:
1. Confirm the Routing API request returns HTTP 200.
2. Verify that the response contains a value in: routes[0].sections[0].polylin
3. Run the decoding function without exceptions.
4. Confirm that latitude and longitude coordinates are returned.
5. Optionally plot the decoded coordinates on a map to validate the route shape.
---
Additional Information
----------------------
A route may contain multiple sections.
Each section contains its own encoded polyline.
Always decode the appropriate sections[n].polyline value corresponding to the route segment you want to process.
* Flexible Polyline can also encode additional dimensions such as elevation when returned by the service.
Updated 3 days ago