Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Created March 27, 2023 03:42
Show Gist options
  • Save stephenmm/4d049723cd248061f19b3b7815f9cefd to your computer and use it in GitHub Desktop.
Save stephenmm/4d049723cd248061f19b3b7815f9cefd to your computer and use it in GitHub Desktop.
import haversine as hs
from haversine import haversine, haversine_vector, Unit
import pandas as pd
import numpy as np
import json
print('hello')
lat=30.393000
lon=-98.070050
loc1=(lat,lon)
Takeout_google_location_history='C:/Users/PC/Downloads/Records.json'
output='C:/Users/PC/Downloads/2022_WillieNillie_geo_location.json'
with open(Takeout_google_location_history) as f:
data = json.loads(f.read())
df = pd.json_normalize(data['locations'])
print( len(df.index) )
df = df.drop('activity', axis=1)
df = df.drop('batteryCharging', axis=1)
df = df.drop('formFactor', axis=1)
df = df.drop('deviceTimestamp', axis=1)
df = df.drop('serverTimestamp', axis=1)
df = df.drop('osLevel', axis=1)
df = df.drop('velocity', axis=1)
df = df.drop('platformType', axis=1)
df = df.drop('locationMetadata', axis=1)
df = df.drop('verticalAccuracy', axis=1)
df = df.drop('heading', axis=1)
df = df.drop('inferredLocation', axis=1)
df = df.drop('activeWifiScan.accessPoints', axis=1)
df['tstamp'] = pd.to_datetime(df['timestamp'])
df = df[df['tstamp'].dt.strftime('%Y') == '2022']
print( len(df.index) )
df['latitudeE7'] = df['latitudeE7'].div(10000000.0)
df['longitudeE7'] = df['longitudeE7'].div(10000000.0)
print( df.head() )
#df['diff'] = hs.haversine(loc1,(df['latitudeE7'],df['longitudeE7']),unit=Unit.MILES)
df['diff'] = df.apply(lambda row: hs.haversine(loc1, (row['latitudeE7'], row['longitudeE7']), unit=Unit.MILES), axis=1)
print( df.head() )
print( len(df.index) )
df=df[df['diff']<0.5]
print( df.head() )
print( len(df.index) )
print( df )
print( "Create day column" )
df['day'] = df['tstamp'].dt.strftime('%Y-%m-%d')
print( len(df.index) )
print( df )
df['tstamp'] = df['tstamp'].astype(str)
print( "Set index to day column and convert to list" )
dct = df.set_index('day').T.to_dict('list')
print( dct )
print( len(dct) )
with open(output, 'w') as convert_file:
convert_file.write(json.dumps(dct))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment