layout | title | comments |
---|---|---|
post |
Tracking your travel adventures using GPS data from your phone and Python/folium |
true |
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.
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.
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 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:
https://gist.github.com/1cf0c9dfba4a2e4e1c51c3d443d01ff9
Once you've read in the file, you can have a brief look at the data:
https://gist.github.com/a7932d899bc79e79c2637bdfd3096780
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:
https://gist.github.com/5fde0cb12e801923cd3a8eaf84acf671
There you go. Now your data is ready to be put on a map.
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:
https://gist.github.com/1c8127b7bcbdb2c870e67a0b64fad7b5
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:
https://gist.github.com/dc846873c6ac93168e2502894b834282
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'
Finally, please let me know what kind of interesting plots you were able to create. Leave a comment below or send me an email. Thank you and happy coding.
loopingleo