SECRET OF CSS

Loading Location History Places From Google Timeline Into Pandas and CSV | by Maksym Kozlenko 🇺🇦 | Jun, 2022


Know how often you were visiting the supermarket when you go for coffee and what’s your usual travel times to and from work, and how long it takes.

0*tu610Y5 fEVbQOkJ
Photo by Thor Alvis on Unsplash

If you enabled Google Timeline location history on your smartphone you can get information on where you have been on a specific date, how long you have been driving, walking or travelling on public transport.

I have had it on since 2013, so I can access 9 years of my location history using Google Timeline tool on Google Maps. For example here is my timeline for 5 June 2022. As you can see, on that day I was driving, visiting the supermarket, walking and taking a ferry from New Zealand South to North Island.

1*5d28NyNVoiugThs5d1 wDQ

While Google Timeline provides an intuitive and easy to use interface, as a person who is interested in analysing this data I would like to get a list of all places I have visited as pandas dataframe and CSV file. Having this data I can do some interesting visualisation of my own travels for all these years.

Let me show how it could be done.

Google Takeout allows you to load all data from your Google Account as a file. It includes your emails, photos, videos, etc. Location history is also available for download as a ZIP file.

To get it go to https://takeout.google.com/settings/takeout , check “Location History” and click on the “Next step” button.

1*Ojx7ZmKgYADVL Pf8VtO2w
d

Select ZIP as file format and click on “Create export”

1*BYqUjvXc6Ead4bV1UTIRVQ

Google will create a new file and will notify you when it’s available for download. File is quite small, only 59 MB for 9 years.

Within the ZIP file location history is stored in the following files. We need to load data from JSON files located in “Semantic Location History” directory

1*Q2oolwEX2VupjVnIERUePg
Google Location history takeout ZIP file contents

We need to install the following libraries to process this file:

pip install timezonefinder
pip install numba

Imports:

from zipfile import ZipFile
import pandas as pd
import json
from timezonefinder import TimezoneFinder

Now we can open each JSON file and append data into the place_visits array:

Result will look like this:

This entry has the following information:

  • placeId Google Place ID. Using this ID you can load additional information about a place by calling the Google Place API. More details
  • locationConfidence. In some cases, coordinates could be less precise and there could be other places located nearby. In that case, locationConfidence will be lower
  • startTimestamp and endTimestamp when you arrived and left this place. Date and time in UTC time zone.
  • latitudeE7 and longitudeE7 Place geographical coordinates multiplied by 1 million. So latitude value -338656941 converts into -33.8656941. It’s negative for the Southern Hemisphere.

Now, this array can be converted into a pandas data frame:

place_visits_df = pd.DataFrame(place_visits)

To make data more convenient to process I would like to:

  • convert datetimes into place local timezone
  • put year, month, day and hour, and minute as separate columns
  • add visit duration column
  • convert coordinates into decimal values
Places dataframe

As a final step, let’s save this data as a CSV file:

place_visits_df.to_csv("place_visits.csv", index=False)

Show all visited places within the given date range on the map:

1*EdO0vkrVlsB1ktcG Rtdtw
# To find most northern place I’ve visited 
# (Berlin Tegel airport for me)
place_visits_df[place_visits_df.latitude == place_visits_df.latitude.max()]
# To find most southern place I’ve visited place_visits_df[place_visits_df.latitude == place_visits_df.latitude.min()]

Using this dataset now you can tell how often you were visiting the supermarket when you go for coffee and what’s your usual travel times to and from work, and how long it takes.

Wonder how it could be done? I am going to add some examples of how to visualize this data in my next post.



News Credit

%d bloggers like this: