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

Mohamed Kafsi
3 min readOct 9, 2020

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.

  • We define the density variation for a given day as the percentage of variation with respect to the baseline. We define the baseline as the median density, for the corresponding day of the week, during the month of January 2020.
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
  • We observe an important decrease in density just after the federal measures of March 13 with a maximum decrease of ~80% for the train station of Zurich and of more than 90% for Geneva airport.
  • In the weeks following the measures, the density progressively increased but is still far from its usual level (~-25% for Zurich train station in August vs. ~-50% for Geneva airport for the same month).
  • This also confirms that the impact of the pandemic is more important on the airline industry than on the rail industry. Naturally, in order to measure more accurately this trend, we would need to run the same analysis for all train stations and all airports of Switzerland.

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:

  • [Easiest] Start with this code that allows for getting the hourly density evolution for any postal code.
  • Modify the code below in order to focus your analysis of density evolution on the 24 hours associated with the available day (27 Jan. 2020).

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.