Skip to content

Instantly share code, notes, and snippets.

@erikologic
Created March 2, 2023 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikologic/dfc45a17dde8c6d29c233b1d61533a37 to your computer and use it in GitHub Desktop.
Save erikologic/dfc45a17dde8c6d29c233b1d61533a37 to your computer and use it in GitHub Desktop.
Calculate border crosses with Google Maps Timeline data
/*
I need to monitor my UK border crosses for residential purposes.
This script will calculate whenever I moved more than 5 degrees, either on lat or long, from one datapoint to the other, since the start of 2017.
*/
// First of all, go to Google Takeout and fetch your Location data.
// Then, for each day, extract the last position for the day with the following command (using gojq)
// cat Records.json | gojq -r '[.locations[] | select((.timestamp | sub("\\.[0-9]+";"") | strptime("%Y-%m-%dT%H:%M:%SZ")) | mktime >= 1483228800) | {latitudeE7, longitudeE7, accuracy, source, timestamp}] | group_by(.timestamp[:10]) | map(max_by(.timestamp)) | map({latitude: (.latitudeE7 / 10000000), longitude: (.longitudeE7 / 10000000), accuracy: .accuracy, source: .source, timestamp: .timestamp})' > grouped.json
// *** You might want to change the Unix timestamp after mktime to filter on your needs
// Now run this script
// node extracted.js > data.json
const d = require('./grouped.json')
// Format is:
// [{
// accuracy: 21,
// latitude: 51.5197102,
// longitude: -0.0691128,
// source: 'WIFI',
// timestamp: '2017-04-10T23:59:14.996Z'
// }]
const r = d.reduce((acc, el ) => {
const last = acc[acc.length -1]
const lat = Math.abs(el.latitude - last.latitude) > 5 // You might want to filter on less or more distance
const long = Math.abs(el.longitude - last.longitude) > 5 // You might want to filter on less or more distance
if (lat || long) acc.push(el)
return acc
}, [d[0]])
console.log(JSON.stringify(r))
// Finally, make a CSV out of it
// cat data.json | gojq -r '["accuracy","latitude","longitude","source","timestamp"], (.[] | [.accuracy,.latitude,.longitude,.source,.timestamp]) | @csv' > data.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment