Returning a .gpx file with openroute service directions from a flask web application

73 Views Asked by At

I am at the moment developing a web application on which users can calculate small instance TSP from coordinates or adresses they upload and retreive the shortest route between them all. The last functionality I want to include is the ability to download a .gpx file of the directions of the route so a user can use the route in his GPS or navigation system etc.

The problem I encounter is that even though the route is plotted correctly on the map in my application, I somehow cannot convert it to a .gpx file. Everytime I convert it, in the .gpx file it only shows the main points to be visited and not every road and point in the directions, so that in the end only the flying distance is displayed in a GPS.

Below I provided a code snipped. I will be very thankful if anyone could help me as Im doing this project for my masters thesis. :)

#plotting the best route calculated by the ga
    response_ga = client.directions(coordinates = updated_list_ga_ors, profile = profile, format="geojson")
    route_coords_ga = response_ga["features"][0]["geometry"]["coordinates"]

    route_coords_ga = [[coord[1], coord[0]] for coord in route_coords_ga]

    best_ga_route = folium.PolyLine(locations=route_coords_ga, color="red")

    m.add_child(best_ga_route)

        # Generate GPX file
    gpx = gpxpy.gpx.GPX()
    gpx_track = gpxpy.gpx.GPXTrack()
    gpx.tracks.append(gpx_track)
    gpx_segment = gpxpy.gpx.GPXTrackSegment()
    gpx_track.segments.append(gpx_segment)

    for point in updated_list_ga_ors:
        gpx_segment.points.append(gpxpy.gpx.GPXTrackPoint(latitude=point[1], longitude=point[0]))

    # Get the GPX data as a string
    gpx_data = gpx.to_xml()

    iframe = m.get_root()._repr_html_()

    # Store GPX data as a session variable
    session["gpx_data"] = gpx_data

I am trying to retreive the actual route i already successfully map as a .gpx file that stores every single point of the polyline i plot. I hope you get what I mean :)

0

There are 0 best solutions below