Rocco Meli

Interactive Maps in Python

One of the sweet deals of working towards a PhD is that you get to travel around the world in order to attend interesting conferences where you network with people in your field and present your work. I thought it would be nice to create an interactive map showing all the conferences I’ve been to and therefore I started to look around for possible solutions.

I immediately came across to the D3.js library, which allows to create data-driven documents using JavaScript. The library seems well designed and relatively easy to use, but I wasn’t very keen to learn a new programming language (not JavaScript) just to create a simple map. Therefore I tried to find a Python library and I discovered folium.

Folium

folium is a Python library that builds on the data wrangling strengths of the Python ecosystem and the mapping strengths of the leaflet.js library. Of course, there is a Python library that wraps an useful JavaScript library! Looking at folium quickstart it was immediately clear that I could use this library to achieve my goal of visualising the conferences I’ve been to on an interactive map.

import folium

Creating a map

Creating an interactive map with folium is extremely easy: I just need to instantiate a folium.Map() object.

folium.Map()

I now have an interactive map of the world that can be navigated using the mouse or the keyboard.

However, I want to personalise the map a bit more. Since most of the conferences I’ve been are in Europe, I would like to center the map on Oxford and set an initial zoom that shows most of Europe (it is too soon for jokes about Brexit…):

m = folium.Map(
    location = [51.751944, -1.257778], # Oxford coordinates
    zoom_start = 4,
)

The map is stored on the variable m so that it can be further processed.

m

Adding Markers

Information about the conferences I attended are stored in a JSON file:

!head conferences.json
{
    "conferences": {
        "Big Ideas for Big Data in Drug Discovery": {
            "location": "London, UK",
            "coordinates": [51.507222, -0.1275],
            "start-date": "12-04-2019",
            "end-date": "12-04-2019"
        },
        
        "AI in Chemistry": {

The data for each conference contains a location, the coordinates of that location (from Wikipedia) as well as the start and end dates of the conference. I would like to add markers to the map, indicating the location of the conference and containing some information in a pop-up window.

First things first, I can use the json package of the Python standard library to load the content of the JSON file as a series of nested dictionaries:

import json

with open("conferences.json", "r") as fin:
    data = json.load(fin)
    
print(data)
{'conferences': {'Big Ideas for Big Data in Drug Discovery': {'location': 'London, UK', 'coordinates': [51.507222, -0.1275], 'start-date': '12-04-2019', 'end-date': '12-04-2019'}, 'AI in Chemistry': {'location': 'Cambridge, UK', 'coordinates': [52.205278, 0.119167], 'start-date': '02-09-2019', 'end-date': '03-09-2019'}, 'AI@Oxford': {'location': 'Oxford, UK', 'coordinates': [51.751944, -1.257778], 'start-date': '17-09-2019', 'end-date': '18-09-2019'}, 'SBDD Congress': {'location': 'Sestri Levante, IT', 'coordinates': [44.266667, 9.4], 'start-date': '23-09-2019', 'end-date': '26-09-2019'}, 'PyPharma': {'location': 'Basel, CH', 'coordinates': [47.554722, 7.590556], 'start-date': '21-11-2019', 'end-date': '22-11-2019'}, "MGMS Young Modellers' Forum": {'location': 'Greenwich, UK', 'coordinates': [51.48, 0], 'start-date': '29-11-2019', 'end-date': '29-11-2019'}}}

I now want to add the conferences on the map as a series of markers. Adding a marker to the map with folium is straightforward and I can define a simple utility function to do just that:

def add_marker(m, conference, coordinates):
    folium.Marker(
        location=coordinates,
        popup=conference,
    ).add_to(m)

Finally I can loop over all the conferences and add the corresponding markers to the map:

for conference, info in data["conferences"].items():
    add_marker(m, conference, info["coordinates"])
m

The interactive map now locates all the conferences I’ve been to and clicking on a marker will open a pop-up window with the name of the corresponding conference. Sweet!

Conclusions

I have to say that I’m very pleased with the simplicity of use of folium. I’ve been able to setup a map with markers in very little time and without any problems. I can now save the final map in HTML and add it to my website:

m.save("map.html")

The code for this post is available on GitHub: blogcode