Routing in Python with casaGeoTools
Introduction
In this tutorial, we use a simple Jupyter Notebook to walk through three practical examples that demonstrate how routing can be integrated into Python-based geospatial workflows. The examples show how to calculate multiple routes in batch, compare different routing strategies, and generate alternative routes.
This tutorial is designed for data scientists, geospatial analysts, and developers working with spatial data in Python.
Step 1
First, import the required Python packages, including Pandas, Shapely, GeoPandas, and Folium. Folium is used to visualize the calculated routes on interactive maps.
Next, adjust the Pandas display settings. This ensures that the notebook shows all rows and columns of the result tables, which is helpful when inspecting the routing results.
Step 2 - Define Routing Inputs
First, we initialize the casaGeoTools client. The API key is loaded from a local .env file to keep credentials separate from the notebook.
Next, we define several Shapely points representing the origin and destination locations used in the routing examples.
These locations are referenced throughout the examples:
-
Headquarters casaGeo, Itzehoe (izet)
-
Elmshorn Central Station (iz)
-
Hamburg Central Station (hh)
-
Kiel Central Station (ki)
-
Lübeck Central Station (hl)
Example 1 - Batch Routing
In the first example, we demonstrate batch routing. We calculate routes from our headquarters in Itzehoe as the origin to three different destinations: the central stations in Hamburg, Kiel, and Lübeck.
To do this, we create a DataFrame where each row represents a routing request. Each entry contains the origin, destination, routing mode, and transport mode. The routes are generated using the routes_batch function. casaGeoTools processes all rows in the DataFrame and returns the results as a GeoDataFrame containing the route geometries.
Finally, we visualize the routes on an interactive Folium map. Each route geometry is added to the map as a GeoJSON layer, making it easy to inspect the results.
Example 2 - Fast vs Short Route
In this example, we calculate two routes between the same origin — our headquarters — and Elmshorn Central Station as the destination. One request uses the fast routing mode, which optimizes for travel time, while the second request uses the short routing mode, which optimizes for distance.
This demonstrates how different routing strategies affect the resulting route geometry. The routes are then visualized on an interactive map to compare the paths.
Example 3 - Alternative Routes
Finally, we request several alternative routes and visualize them on an interactive map. In our example, the alternatives parameter is set to three, meaning that up to three additional route options may be returned. The parameter supports values up to seven alternatives.
Alternative routes are only returned if they represent meaningful route variations. In this example, the routing service returns four routes in total: the main route and three alternatives. As before, we create an interactive map to inspect the results.
Download
To jumpstart your development, you can download this example as a Jupyter Notebook below.