Locally Generating Routing GPX files in Python

There is a dated online guide and forum topic on how this could work. However, they were made in 2017 and 2019, respectively, and some values may have changed since then.

I tried putting these lines in a Python file to create a GPX file from my starting point to a CSV (XY_id) file of end points but it doesn’t seem to work.

import pandas as pd
import urllib.request

coordinates = pd.read_csv('my_csv_file')
urlStart = 'http://localhost:8989/route?'
point = 'point='
urlEnd = '&type=gpx&instructions=false&vehicle=car' 
separator = '%2C'

startY = 'my_starting_lat'
startX = 'my_starting_long'

for index, row in coordinates.iterrows():
    req = urlStart + point + startY + separator + startX + '&' + point + str(row['Y']) + separator + str(row['X']) + urlEnd
    try:
        resp = urllib.request.urlopen(req)
        gpxData  = str(resp.read(),'utf-8')
        fileName = 'island_' + str(index)
        saveFile = open('gpx_files/{0}.gpx'.format(fileName), 'w')
        print('processed index ' + str(index))
        saveFile.write(gpxData)
        saveFile.close()
    except: 
        print('bad request on index' + str(index))
        pass

There seems to be a problem with the urlStart/urlEnd values or req sequence here which I cannot find on the Routing API documentation.

EDIT1: After some tweaking in the browser, the urlEnd value seems to be the problem: “vehicle” is now “profile” but this results to a GPX with RTEPT that cant be removed and with points that have no elevations.

I tried adding “&elevation=true&points_encoded=false” but it says Elevation not supported.

It has been fixed i just added the following as my urlEnd:

&type=gpx&profile=car&instructions=false&points_encoded=false&details=time