import osmnx as ox
Interactive Maps with Folium
This page is generated from a Jupyter notebook and shows examples of embedding interactive maps produced using Folium.
Finding the shortest route
This example finds the shortest route between the Art Musuem and the Liberty Bell using osmnx.
First, identify the lat/lng coordinates for our places of interest. Use osmnx to download the geometries for the Libery Bell and Art Museum.
= ox.features_from_place("Philadelphia, PA", tags={"tourism": True}) philly_tourism
= philly_tourism.query("name == 'Philadelphia Museum of Art'").squeeze()
art_museum
art_museum.geometry
= philly_tourism.query("name == 'Liberty Bell'").squeeze()
liberty_bell
liberty_bell.geometry
Now, extract the lat and lng coordinates
For the Art Museum geometry, we can use the .geometry.centroid
attribute to calculate the centroid of the building footprint.
= liberty_bell.geometry.x
liberty_bell_x = liberty_bell.geometry.y liberty_bell_y
= art_museum.geometry.centroid.x
art_museum_x = art_museum.geometry.centroid.y art_museum_y
Next, use osmnx to download the street graph around Center City.
= ox.graph_from_address(
G_cc "City Hall, Philadelphia, USA", dist=1500, network_type="drive"
)
Next, identify the nodes in the graph closest to our points of interest.
# Get the origin node (Liberty Bell)
= ox.nearest_nodes(G_cc, liberty_bell_x, liberty_bell_y)
orig_node
# Get the destination node (Art Musuem)
= ox.nearest_nodes(G_cc, art_museum_x, art_museum_y) dest_node
Find the shortest path, based on the distance of the edges:
# Get the shortest path --> just a list of node IDs
= ox.shortest_path(G_cc, orig_node, dest_node, weight="length") route
How about an interactive version?
osmnx
has a helper function ox.utils_graph.route_to_gdf()
to convert a route to a GeoDataFrame of edges.
="length").explore(
ox.utils_graph.route_to_gdf(G_cc, route, weight="cartodb positron",
tiles="red",
color )