avatarLukas Krimphove

Summary

This context provides a tutorial on how to use Python and Folium to create an interactive map of outdoor activities using GPX files.

Abstract

The text begins with the author's personal motivation for visualizing their outdoor activities and introduces the Python library Folium as a tool for creating interactive maps. The tutorial starts with setting up Jupyter Notebook, followed by parsing GPX files for trail data using the gpxpy library. The extracted data is then plotted on an interactive map using Folium's PolyLine and Marker objects. The map allows users to revisit their past outdoor activities, celebrate progress, and stay motivated for future adventures. The tutorial concludes with a mention of potential future improvements and references to the author's GitHub repository and other resources.

Bullet points

  • The author dreams of hiking from Munich to Venice and wants to visually trace their progress using GPX files.
  • GPX files contain location data such as latitude, longitude, elevation, and time, making them ideal for tracking outdoor activities.
  • Jupyter Notebook is recommended as an interactive computing environment for experimenting with data and the Folium library.
  • The gpxpy library is used to parse GPX files and extract essential trail data such as latitude, longitude, elevation, speed, and distance.
  • Folium allows for the visualization of outdoor activities on an interactive map, with unique colors and icons assigned to each activity type.
  • The map can be customized and improved by deploying a website, plotting elevation and speed profiles, and enhancing trails with pictures taken along the way.
  • The tutorial provides a starting point for creating an interactive map of outdoor activities using Python and Folium.
  • The code and Jupyter Notebook are available on the author's GitHub repository.
  • The tutorial references other resources, including a story by Patrick who did similar work and provided a base for the author's solution.

Visualizing Outdoor Activities with Python Folium

Embark on an expedition of exploration and mapping! Learn how to breathe life into your GPX files and create interactive maps using Python and Folium.

Introduction

I dream of hiking from Munich to Venice, trekking across the beautiful Alps. But as I still have to live my everyday life, my journey has to consist of multiple stages, with weeks, months, or even years between each adventure. That is ok, as it’s more about the journey than the destination. However, I always wanted a way to visually retrace these paths, to see how far I’ve come and how close I am to my goal. I wanted a way to celebrate the progress I’ve made and to motivate myself to take the journey further.

Fortunately, many outdoor and sports apps, like Adidas Running, Komoot, Strava, and others, graciously allow us to export our activities as GPX files. However, there’s nowhere to go with those GPX files.

That is where Python and Folium come into play. I recently stumbled upon Folium, a powerful Python library for creating interactive maps. It can easily incorporate geographic data, such as the GPX files, and allows for customization and exploration. Drawing upon the wealth of GPS data I’ve gathered, I started experimenting with Folium to craft a map that brings my outdoor excursions to life.

After some research and a lot of testing, I came up with a map that allows me to revisit my past outdoor activities:

So, if you are anything like me and have a treasure trove of GPS data without any purpose, you may wonder how I got there. So in this story, I will explain how to breathe life into your GPX files. Let’s embark on an expedition of exploration and mapping!

Getting Started with Jupyter Notebook

To begin this adventure, we’ll be using Jupyter Notebook. Why Jupyter Notebook? It’s a fantastic interactive computing environment that allows us to combine code, visualizations, and text, making it perfect for experimenting with our data and the Folium library.

If you haven’t installed Jupyter Notebook yet, follow the instructions on their official website. Once installed, you can create a new Jupyter Notebook and get ready for your journey.

Parsing GPX Files for Trail Data

Next, we need the raw material for our map — the GPX files. GPX (GPS Exchange Format) is a widely-used file format that stores location data, such as latitude, longitude, elevation, and time, making it ideal for tracking outdoor activities.

If you’re an avid hiker, runner, cyclist, or skier, chances are you already have tracked various excursions using an outdoor or sports app. Plenty of those apps allow you to export your activities in GPX format. So gather those GPX files, and let’s get started!

In our Python code, we’ll use the gpxpy library to parse the GPX files and extract the essential trail data, such as latitude, longitude, elevation, speed, and distance. The parse_gpx() function will do all the heavy lifting for us:

That leaves us with all the necessary data of an activity: a list of all the GPS coordinates and a Pandas DataFrame containing all kinds of metrics.

Plotting GPX Trails on the Interactive Map

With our GPS data now organized in a Pandas DataFrame, we can visualize our outdoor activities on an interactive map. Folium makes this task a breeze:

  • We’ll create a map centered at a specific location (you can choose one or let the code determine the center based on your data).
  • We’ll assign unique colors and icons to each activity type, such as hiking, running, biking, or skiing. The ACTIVITY_TYPES dictionary will help us with this.
  • We’ll use Foliums FeatureGroup concepts to group the trails based on their group name. That will allow us to show and hide certain activity groups later.
  • Now, we’ll iterate through our parsed data and plot each trail on the map using Folium’s PolyLine and Marker objects. The PolyLine will represent the actual trail, while the Marker will act as a starting point for each activity. When you click on a marker, a popup will display relevant information about the corresponding trail.

Conclusion

In this journey of exploration and mapping, we’ve learned how to use Python and Folium to transform mundane GPX files into a dynamic and interactive map. Now you can relive your outdoor adventures, celebrate your progress, and stay motivated for the next stage of your journey.

However, there are many ways to customize, extend and improve your map. So grab your GPX files, fire up Jupyter Notebook, and let your past outdoor activities come to life on the map!

Happy mapping!

What’s next?

Stay tuned because there is much more to come:

  • deploying a website with your map using AWS
  • plotting elevation and speed profiles using Python and Plotly
  • enhancing trails with pictures taken one the way
  • and much more

References

  • All the code, including the Jupyter Notebook, is on my GitHub.
  • While doing my research on Folium, I found a great story by Patrick, who did the same thing I planned to do. His work was a great base to build upon my solution, so please check it out and leave some claps.
  • Jupyter Notebook
  • Folium
  • Pandas

In Plain English

Thank you for being a part of our community! Before you go:

Python
Folium
Data Visualization
Data Science
Mapping
Recommended from ReadMedium