Unlocking Your Geospatial Data: Analyzing Google Maps History
Written on
Chapter 1: Introduction to Geospatial Data
Have you ever thought about the digital traces you leave while moving through the world? Your smartphone, often seen as a harmless gadget, functions as a powerful geospatial tracker, recording your every journey. If you use an Android device, you might be surprised at the depth of your travel history, which reveals a fascinating account of your daily activities.
But how does this work? Google's Location History feature continuously logs your GPS coordinates, storing not only where you go but also the routes taken, modes of transport used, and time spent at each destination. So, how can we utilize this treasure trove of data and delve into it? Let's explore together! šš»
First, we need to access our Google Maps data.
Here, you can either request a complete data archive or select specific products. For our purposes, just choose the Location History option. Scroll through the Google products list until you locate the Location History. After selecting it, proceed to the next step.
You can then choose your preferred export option. I typically opt for "Export Once." Now, just be patient as Google compiles your selected data. Once ready, you will receive an email prompting you to download it, with a limited time for access due to security reasons.
After following the download instructions, you will find a .zip file labeled with the word "takeout." Unzipping it will reveal a folder named "Semantic Location History."
If youāre wondering about the term "Semantic Location History," it refers to processed data that provides higher-level insights compared to raw data. This refined information is what you see on Google Maps' Timeline feature, enriched with additional context.
ā ļø If anyone knows how to retrieve the raw data instead, please share in the comments!
Now, letās dive into the exciting part! š¤ The folder includes multiple subfolders for each year. For instance, Iāve been using a phone since 2013, when I was just 15 years old. Each yearās folder contains monthly JSON files.
One potential challenge? I switched from Android almost two years ago, in 2021, so it will be interesting to see how that impacted my Google Maps history!
Next, weāll upload this data into our Python environment for analysis. The data is in JSON format, which is widely accepted and easy to manipulate in Python. Libraries like pandas and json are particularly useful for handling this data.
Before diving in, letās first familiarize ourselves with the JSON structure. I usually use an Online JSON Viewer for this purpose.
We can paste our JSON file here and view its structure. Each JSON fileārepresenting a monthācontains information under the field called timelineObjects. Within this structure, we can identify two primary record types:
- Activity Segments: These detail the journeys between various locations and modes of transport (car, walking, biking, etc.).
- Place Visits: These record all the locations weāve been to.
This understanding will lead us to create two main datasets: one for placeVisit records and another for activitySegment records.
Letās break this process into steps:
Section 1.1: Loading the Data
We can load all the data using a simple function: iterate through each yearly folder in "Semantic Location History" and each monthly file within those folders. The data will be compiled into a comprehensive list.
So, whatās next? We need to convert this extensive list of JSONs into a workable format.
Subsection 1.1.1: Creating DataFrames
The Google Maps data encompasses a variety of details, but for our analysis, weāll focus on two main tables: VisitedPlaces and ActivitySegments.
- VisitedPlaces will include information about the locations visited, such as the name, address, latitude, longitude, and duration of the visit.
- ActivitySegments will detail the journeys between places, including start and end times, distance, duration, and transport mode.
We will iterate over the JSON list, appending records to separate lists for each dataset and tracking the source of each record.
Next, we can use the json_normalize function from pandas to transform our data into DataFrames. This function efficiently flattens JSON objects into pandas DataFrames, making them easier to analyze.
Now, weāll normalize both DataFrames, adding a column to indicate the original JSON file for each record, which weāll call "source_file."
After this, weāll have our DataFrame structured as shown below:
DataFrames obtained from our Google Maps location history.
Section 1.2: Data Cleaning
At this point, we need to assess the data in our placeVisit DataFrame. Utilizing the .info() command gives us a quick overview of our records, revealing many columns with null values.
This leads us to the next logical step: cleaning the data. Itās essential to focus only on the columns that will enhance our analysis.
For the visitedPlaces DataFrame, weāll select and rename the relevant columns. Following this, weāll implement several important improvements:
- Adjust latitude and longitude values, which are scaled by a factor of 10ā·.
- Convert datetime variables into a numpy datetime format for more efficient manipulation.
- Create a new variable, 'duration,' representing how long we stayed at each location.
Now, we have our clean and ready-to-analyze DataFrame!
Chapter 2: Analyzing Your Data
Once our data is prepared, itās time to delve into it!
Letās first examine the number of records in our dataset over the years.
The first video, "How to Download Building Footprint Data with Google Earth Engine," provides insights into obtaining building footprint data, which can complement your analysis.
After analyzing the data, we can see some interesting patterns. Thereās a notable absence of records from 2013 to 2017, with 2015 showing no activity at all. However, in 2019 and 2020, the number of recorded locations surged dramatically, likely reflecting more frequent travel.
To better understand weekly trends, we can visualize how the Covid lockdown impacted mobility, drastically reducing my recorded locations.
The second video, "How to Download Latest Building Footprint Data From Google," dives into methods for acquiring up-to-date building footprint data, enhancing your spatial analysis.
Finally, letās see a map showcasing all my recorded locations worldwide.
Main Conclusions
In summary, your Google Maps location history is a fascinating dataset ripe for personal analysis. By retrieving and examining this data, you can gain insightful perspectives on your habits and routines. While this project can be enjoyable and revealing, it also highlights the extensive digital trails we leave behind.
Remember, our smartphones are powerful tools for data collection, but itās our responsibility to use this data thoughtfully. For instance, creating a heatmap of frequently visited spots can provide a fresh perspective on your daily life. Additionally, integrating external datasetsāsuch as weather patterns or local eventsācan help you analyze how outside factors influence your movements.
Happy analyzing! If you have questions, feel free to comment! You can find the code on my GitHub and subscribe to my Medium Newsletter for unique content. Connect with me on Twitter and LinkedIn as well!