How to Quantify the Impact of COVID-19 Measures on Urban Density? A data-driven approach with Python and REST APIs

To slow down the spread of COVID-19, governments introduced public health strategies such as social distancing and mobility restrictions. Enforcing mobility restrictions is one of the top strategies because it has a direct impact on the evolution of number of cases: it flattens the peak of the curve and delays it.

In such conditions, it is more critical than ever to quickly have feedback from the field with the relevant data in time: what is the actual impact of these decisions on collective mobility ? Does it match the desired outcome, or should stricter measures be introduced?

In this article, we will show you how to Swisscom heatmaps REST API to quantify the urban density evolution for any time period and geographical region in Switzerland, with a focus on COVID-19 period. The Python code is available on GitHub.

Rail vs. Airline

Starting 13 of March 2020, the Swiss confederation announced multiple measures to slow down the spread of COVID-19 by limiting the population mobility. We show in the plots below the impact of these measures on the density of the postal codes associated with two major transportations hubs: Zurich train station and Geneva airport.

Density evolution for the postal code 8001 associated with the train station of Zurich
Density evolution for the postal code 1215 associated with Geneva airport

How to produce these plots for any region in Switzerland?

We share below the Python3 code to produce the density plots. In order to be able to run the code, you need to register to the Swisscom Digital Marketplace and subscribe to the demo plan (free but limited to one day of data) or the standard plan (paying and unlimited access).

If you only register to the API demo plan we recommend one of these two options:

If you register to the API standard plan the connection to the API is straighforward and is done with a few lines of code

The connection to the API is straighforward and is done with a few lines of code. The client_id and client_secret are obtained once you register to the heatmaps API

The function get_tile_ids_postal_code returns the tile ids associated with a given postal code. To increase code simplicity and redability, we choose to randomly sample from these tile ids the maximum number of tiles (100) we can query in only one API call.

The function build_api_query returns the API request needed to get the data associated with a given list of tiles for a given date. We use it in the function get_daily_density to query the API and get the sum of density scores associated with the given list of tiles.

We share in Github the code used to generate the plots shown in this article.