How to create a route on a web map (Flask) using folium and osmnx?

23 views Asked by At

So I want to create a website on Flask where users can create a route. I have no problem creating a route with just Folium, but I need to use nodes from the map so I decided to use OSMnx. However, when I use OSMnx, the website only displays the map without the route.

So here is my code:

from flask import render_template
import folium
import osmnx as ox
import networkx as nx

@views.route('/', methods=['GET', 'POST'])
def test():
    mapObj = folium.Map(location=[40.748441, -73.985664], zoom_start=15, width=1850, height=900)
    
    ox.config(log_console=True, use_cache=True)

    G_walk = ox.graph_from_place('Manhattan Island, New York City, New York, USA',
                            network_type='walk')
    
    orig_node = ox.nearest_nodes(G_walk, Y=40.748441, X=-73.985664)

    dest_node = ox.nearest_nodes(G_walk, Y=40.748441, X=-73.3)

    route = nx.shortest_path(G_walk,
                            orig_node,
                            dest_node,
                            weight='length')

    route = ox.plot_route_folium(G_walk, route, width=1850, height=910).add_to(mapObj)

    mapObj.get_root().render()

    header = mapObj.get_root().header.render()

    body_html = mapObj.get_root().html.render()

    script = mapObj.get_root().script.render()

    return render_template('home.html', user=current_user, 
                        header=header, body_html=body_html, script=script)

I also tried to render the "route", but then map wasn't displayed either.

0

There are 0 answers