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.

import osmnx as ox

First, identify the lat/lng coordinates for our places of interest. Use osmnx to download the geometries for the Libery Bell and Art Museum.

philly_tourism = ox.features_from_place("Philadelphia, PA", tags={"tourism": True})
art_museum = philly_tourism.query("name == 'Philadelphia Museum of Art'").squeeze()

art_museum.geometry

liberty_bell = philly_tourism.query("name == 'Liberty Bell'").squeeze()

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_x = liberty_bell.geometry.x
liberty_bell_y = liberty_bell.geometry.y
art_museum_x = art_museum.geometry.centroid.x
art_museum_y = art_museum.geometry.centroid.y

Next, use osmnx to download the street graph around Center City.

G_cc = ox.graph_from_address(
    "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)
orig_node = ox.nearest_nodes(G_cc, liberty_bell_x, liberty_bell_y)

# Get the destination node (Art Musuem)
dest_node = ox.nearest_nodes(G_cc, art_museum_x, art_museum_y)

Find the shortest path, based on the distance of the edges:

# Get the shortest path --> just a list of node IDs
route = ox.shortest_path(G_cc, orig_node, dest_node, weight="length")

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.

ox.utils_graph.route_to_gdf(G_cc, route, weight="length").explore(
    tiles="cartodb positron",
    color="red",
)
Make this Notebook Trusted to load map: File -> Trust Notebook