Alternatively, you can use osmnx to retrieve a graph for a specific place and populate InMemMap. There are two primary approaches:
Approach 1: Using projected GeoDataFrames
Retrieve a projected graph, convert it to GeoDataFrames, and add nodes/edges using the projected coordinates.
Approach 2: Using unprojected GeoDataFrames or NetworkX
Convert GeoDataFrames to a specific CRS (e.g., EPSG:3395) to access lat/lon for nodes, or iterate directly through the networkx graph edges to add them to the map.
import osmnx as ox
# ... setup map_con ...
graph = ox.graph_from_place('Leuven, Belgium', network_type='drive', simplify=False)
graph_proj = ox.project_graph(graph)
# Approach 1
nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)
for nid, row in nodes_proj[['x', 'y']].iterrows():
map_con.add_node(nid, (row['x'], row['y']))
for eid, _ in edges_proj.iterrows():
map_con.add_edge(eid[0], eid[1])
# Approach 2
nodes, edges = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)
nodes_proj = nodes.to_crs("EPSG:3395")
edges_proj = edges.to_crs("EPSG:3395")
for nid, row in nodes_proj.iterrows():
map_con.add_node(nid, (row['lat'], row['lon']))
# Extract edges directly from networkx graph
for nid1, nid2, _ in graph.edges:
map_con.add_edge(nid1, nid2)