Skip to content

Instantly share code, notes, and snippets.

@garrettseepersad
Created April 9, 2023 02:41
Show Gist options
  • Save garrettseepersad/d8d5cfebff7371985eb4f0e32f2de35c to your computer and use it in GitHub Desktop.
Save garrettseepersad/d8d5cfebff7371985eb4f0e32f2de35c to your computer and use it in GitHub Desktop.
google_maps_2_komoot.py
import json
from zipfile import ZipFile
import pandas as pd
import json
from timezonefinder import TimezoneFinder
from gpx_converter import Converter
import re
from komPYoot import API, Sport
import time
import os
import argparse
from pathlib import Path
parser = argparse.ArgumentParser(
prog='googlemap_2_komoot',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--email_id',
type=str,
help='Input komoot email address')
parser.add_argument('--password',
type=str,
help='Input komoot args.password')
parser.add_argument('--google_map_location_history',
type=str,
help='Path to location history')
args = parser.parse_args()
directory = str(Path(args.google_map_location_history).absolute())
# Initialize komoot API
a = API()
a.login(args.email_id, args.password)
# Initialize timezone
tf = TimezoneFinder()
# store all places into this array
place_visits = []
previous_day = -1
output_waypoints = pd.DataFrame([], columns=['latE7','lngE7','time'])
with ZipFile(args.google_map_location_history) as myzip:
for file in myzip.filelist[:]:
filename = file.filename
name = filename.split("/")[-1].split('.')[0]
if "Semantic Location History" in filename:
# process all files in "Semantic Location History" directory
history_json = json.load(myzip.open(filename))
waypoints = []
for ix,timeline_object in enumerate(history_json["timelineObjects"]):
if "activitySegment" in timeline_object:
if timeline_object['activitySegment']['activityType'] == "CYCLING":
startTimestamp = pd.to_datetime(timeline_object['activitySegment']['duration']['startTimestamp'])
endTimestamp = pd.to_datetime(timeline_object['activitySegment']['duration']['endTimestamp'])
duration = endTimestamp - startTimestamp
if 'waypointPath' in timeline_object['activitySegment'].keys():
waypoints = pd.DataFrame(timeline_object['activitySegment']['waypointPath']['waypoints'])
waypoints["latE7"].update(waypoints.latE7/1E7)
waypoints["lngE7"].update(waypoints.lngE7/1E7)
time_zone = tf.timezone_at(lng=waypoints["lngE7"][0], lat=waypoints["latE7"][0])
startTimestamp = startTimestamp.tz_convert(time_zone)
endTimestamp = endTimestamp.tz_convert(time_zone)
for way_ix, waypoint in waypoints.iterrows():
new_time_stamp = startTimestamp + ((way_ix)*duration/len(waypoints))
waypoints.at[way_ix,'time'] = new_time_stamp
if ((previous_day != -1) and (previous_day != startTimestamp.day)):
filename = re.sub("\s", "_",(f"{previous_name}_{previous_day}_ix_{previous_ix}"))
Converter.dataframe_to_gpx(input_df=(output_waypoints),
lats_colname='latE7',
longs_colname='lngE7',
times_colname='time',
output_file=f'{filename}.gpx')
status = a.upload_tour_gpx(Sport.BIKE_TOURING,
directory + "/" + f'{filename}.gpx',
name = filename,
duration=None)
time.sleep(2)
try:
os.remove(f'{filename}.gpx')
except FileNotFoundError:
print(f"File did not exist : {f'{filename}.gpx'}")
output_waypoints = pd.DataFrame([], columns=['latE7','lngE7','time'])
output_waypoints = output_waypoints.append(waypoints)
elif (previous_day == startTimestamp.day):
output_waypoints = output_waypoints.append(waypoints).reset_index(drop=True)
elif (previous_day == -1):
output_waypoints = waypoints
previous_day = startTimestamp.day
previous_name = name
previous_ix = ix
previous_waypoint = waypoints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment