Route geometry + coordinates not working correctly in Python

When creating a “round trip” route within Python, the geometry and coordinate results are different (and wrong) compared to the text output of a json file derived from the openrouteservice maps website.
Below is a typical output of the geometry and coordinates derived from Python, visualized via the google decoder. It’s saying all of my steps, with the exception of the first one, are in Lake Michigan.


I changed my latitude parameter, and ended up with a nearly identical looking result. There is something wrong involving the math derived from the first longitudinal point

I will gladly post my code, if needed

Thank you

import random
import requests
import openrouteservice as ors
from openrouteservice import convert

client = ors.Client(key="YOURAPIKEY")

miles = int(input("How many miles would you like to run today? "))
miles = miles * 1609.34 #convert to meters

#ORS works in LONG, LAT format...

lon = -87.678022
lat = 41.953227
random_seed = random.randint(0, 90)  # same as website
body = {
    "coordinates": [[lon, lat]],
    "elevation": "True",
    "instructions": "True",
    "units": "mi",
    "options": {"round_trip": {"length": miles, "points": 4, "seed": random_seed}},
}

headers = {
    "Accept": "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8",
    "Authorization": "5b3ce3597851110001cf6248cae8488fe4e7406389bcec6b2d5947a5",
    "Content-Type": "application/json; charset = utf-8",
}

call = requests.post(
    f"https://api.openrouteservice.org/v2/directions/foot-walking",
    json=body,
    headers=headers,
)

route_overview = call.json()['routes']

geometry = route_overview[0]['geometry']
print(geometry)
decoded = convert.decode_polyline(geometry)
print(decoded['coordinates'])

Also, it’s throwing a strange error about 50% of the time. See below

IndexError                                Traceback (most recent call last)
<ipython-input-32-a593a8cc03be> in <module>
      2 print(geometry)
      3 
----> 4 decoded = convert.decode_polyline(geometry)

~\AppData\Local\Programs\Python\Python39\lib\site-packages\openrouteservice\convert.py in decode_polyline(polyline, is3d)
    160         shift = 0
    161         while True:
--> 162             b = ord(polyline[index]) - 63 - 1
    163             index += 1
    164             result += b << shift

IndexError: string index out of range

Hey,

the problem here is that you are requesting elevation, and per the api-docs:

Please note that elevation also gets encoded for json response encoded polyline.

That means that you get a 3-dimensional encoded polyline for your request. You can drop this line and your code should work just fine:

If you don’t want to, you can use

decoded = convert.decode_polyline(geometry, is3d=True)

The google polyline decoder (i.e. from import polyline) does not work for 3-dimensional polylines.

Two unrelated comments regarding your code:
You are importing both ors and requests. This is a bit redundant, since the ors-module can already issue requests to the ors api:

call = client.directions(
    coordinates=[lon, lat],
    profile='foot-walking',
    units='mi',
    elevation=True,
    instructions=True,
    options={"round_trip": {"length": miles, "points": 4, "seed": random_seed}},
)

should yield the same results, but get rid of the header overview.
Secondly, if you already import ors, it’s a bit redundant to from openrouteservice import convert :wink:

Best regards

Got it. Thank you!

1 Like