How to track your travel adventures using your phone‘s GPS data and Python
Having a visual GPS log of your trips to foreign places can be fun way for telling your friends and family a visual story about your adventures. I recently started tracking my location and other data such as accelerometer with the iOS app ‘SensorLog’ to visualise it using Python/R scripts. In this blog post I will briefly guide you through setting up the GPS tracking on your phone and how you can create beautiful maps with your data.
GPS data logging with your smartphone
SensorLog is a great app for iOS devices to record all your phone’s sensor data. You can simply let the app run in the background — whether your hiking in New Zealand, enjoying Renaissance paintings in Rome or sipping Moscow Mules on a cruise ship.
Transferring GPS logging data to your computer
Once you recorded your sensor data, you can transfer it from your phone to your computer. Personally, I store these files in my iCloud. You can find great tips on that on Apple’s support site.
Reading in the data in Python
Reading in your sensor data into Python is easy. My choice for this use case is the famous pandas package and locally stored files since it’s relatively simple and it works great. You can read in single files. But in my case I had been tracking my location for many days and had multiple files ready to be visualised in my iCloud. Let us have a look how this works:
Once you’ve read in the file, you can have a brief look at the data:
Or you could do some simple plotting with matplotlib.pyplot.
Once you have had a first look at the data, you can decide which variables are most important to you. In this example, I selected the following columns from my pandas DataFrame and shortened the column names for easier handling:
There you go. Now your data is ready to be put on a map.
Plotting GPS data on a map using folium or gmplot
It is time to plot your GPS data on a map. If you have a Google Maps API key or you just want to print the Google maps on your local machine, you could use the following code:
A nice and free alternative to Google Maps is OpenStreetMap. You can use the Python package folium which uses leaflet.js and OpenStreetMap. Folium comes in handy if you want to use the final map and host it in your blog like I did here.
In case you want to go with folium, let us have a look at the code for that:
Once you’ve got the feel for it, you can create maps like this one with larger datasets:
You can find the complete code in my GitHub repo ‘GPS-tracking’
Now it’s your turn
Finally, please let me know what kind of interesting plots you were able to create. Leave a comment below or send me an email.
If you found this story helpful, please click the 👏 below to spread the love. Thank you for reading and happy coding.