Skip to content

Instantly share code, notes, and snippets.

@Finkregh
Last active July 8, 2024 20:24
Show Gist options
  • Save Finkregh/6e7883780cab999679c0c57409743adf to your computer and use it in GitHub Desktop.
Save Finkregh/6e7883780cab999679c0c57409743adf to your computer and use it in GitHub Desktop.
Script to convert google location takeout to owntracks native format. No need for a MQTT connection, just dump the created files into the owntracks-recorder data dir.
#!/usr/bin/python
# Slightly modified from <https://www.technowizardry.net/2024/01/migrating-from-google-location-history-to-owntracks/>
import pandas as pd
import json
import time
# https://owntracks.org/booklet/features/tid/
tracker_id = 'ex' # A two-character identifier
df_gps = pd.read_json('Records.json', typ='frame', orient='records')
print('There are {:,} rows in the location history dataset'.format(len(df_gps)))
df_gps = df_gps.apply(lambda x: x['locations'], axis=1, result_type='expand')
df_gps['latitudeE7'] = df_gps['latitudeE7'] / 10.**7
df_gps['longitudeE7'] = df_gps['longitudeE7'] / 10.**7
# In some cases, Google appears to generate timestamps in two different formats.
# Here we convert both types, then normalize it all into the timestamp Pandas dtype.
try:
df_gps.loc[df_gps['timestamp'].str.len() == len('2013-12-16T05:42:25.711Z'), 'timestamp'] = pd.to_datetime(df_gps['timestamp'], format='mixed')
except ValueError:
df_gps.loc[df_gps['timestamp'].str.len() == len('2013-12-16T05:42:25Z'), 'timestamp'] = pd.to_datetime(df_gps['timestamp'], format='%Y-%m-%dT%H:%M:%S%Z', utc=True)
df_gps['timestamp'] = pd.to_datetime(df_gps['timestamp'])
owntracks = df_gps.rename(columns={'latitudeE7': 'lat', 'longitudeE7': 'lon', 'accuracy': 'acc', 'altitude': 'alt', 'verticalAccuracy': 'vac'})
owntracks['tst'] = (owntracks['timestamp'].astype(int) / 10**9)
files = {}
years = df_gps['timestamp'].dt.year.agg(['min', 'max'])
for year in range(years['min'], years['max'] + 1):
for month in range(1, 13):
files[f"{year}-{month}"] = open(f"location/{year}-{str(month).rjust(2, '0')}.rec", 'w')
try:
for index, row in owntracks.iterrows():
d = row.to_dict()
record = {
'_type': 'location',
'tid': tracker_id
}
record['tst'] = int(time.mktime(d['timestamp'].timetuple()))
for key in ['lat', 'lon']:
if key in row and not pd.isnull(row[key]):
record[key] = row[key]
for key in ['acc', 'alt', 'vac']:
if key in row and not pd.isnull(row[key]):
record[key] = int(row[key])
timestamp = row['timestamp'].strftime("%Y-%m-%dT%H:%M:%SZ")
line = f"{timestamp}\t* \t{json.dumps(record, separators=(',', ':'))}\n"
files[f"{d['timestamp'].year}-{d['timestamp'].month}"].write(line)
finally:
for key, file in files.items():
file.flush()
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment