Skip to content

Instantly share code, notes, and snippets.

@WouterNieuwerth
WouterNieuwerth / Instant_page_GTM.html
Last active March 23, 2019 11:51
Instant.page for Google Tag Manager Custom HTML Tag
<script>
(function() {
// Rewrite of the Instant.page <html> snippet for Google Tag Manager.
var el = document.createElement('script');
el.setAttribute('src', '//instant.page/1.2.2');
el.setAttribute('type', 'module');
el.setAttribute('integrity', 'sha384-2xV8M5griQmzyiY3CDqh1dn4z3llDVqZDqzjzcY+jCBCk/a5fXJmuZ/40JJAPeoU');
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_1.py
Created February 9, 2020 12:52
GPX analysis - step 1
import gpxpy
import matplotlib.pyplot as plt
import datetime
from geopy import distance
from math import sqrt, floor
import numpy as np
import pandas as pd
import haversine
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_3.py
Last active February 9, 2020 13:39
GPX analysis - step 3
for segment in gpx.tracks[0].segments: # all segments
data = segment.points
for point in data:
df = df.append({'lon': point.longitude, 'lat' : point.latitude, 'alt' : point.elevation, 'time' : point.time}, ignore_index=True)
df = df.sort_values(by=['time'])
df = df.reset_index()
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_4.py
Created February 9, 2020 13:43
GPX analysis - step 4
# Create a column with values that are 'shifted' one forwards, so we can create calculations for differences.
df['lon-start'] = df['lon']
df['lon-start'].iloc[-1] = np.nan
df['lon-start'] = np.roll(df['lon-start'], 1)
df['lat-start'] = df['lat']
df['lat-start'].iloc[-1] = np.nan
df['lat-start'] = np.roll(df['lat-start'], 1)
df['alt-start'] = df['alt']
df['alt-start'].iloc[-1] = np.nan
df['alt-start'] = np.roll(df['alt-start'], 1)
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_5.py
Created February 9, 2020 13:46
GPX analysis - step 5
df['time'] = pd.to_datetime(df['time'], utc=True)
df['time'] = df['time'].dt.tz_localize(tz=None)
df['time-start'] = pd.to_datetime(df['time-start'], utc=True)
df['time-start'] = df['time-start'].dt.tz_localize(tz=None)
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_6.py
Created February 9, 2020 13:50
GPX analysis - step 6
# Calculate distances and time deltas
df['distance_dis_2d'] = df.apply(lambda x: distance.distance((x['lat-start'], x['lon-start']), (x['lat'], x['lon'])).m, axis = 1)
df['alt_dif'] = df.apply(lambda x: x['alt-start'] - x['alt'], axis=1)
df['distance_dis_3d'] = df.apply(lambda x: sqrt(x['distance_dis_2d']**2 + (x['alt_dif'])**2), axis=1)
df['time_delta'] = df.apply(lambda x: (x['time'] - x['time-start']).total_seconds(), axis=1)
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_7.py
Created February 9, 2020 14:02
GPX analysis - step 7
df_selected = df.loc[:, ['distance_dis_3d','time_delta']]
df_selected['distance_cumsum'] = df_selected['distance_dis_3d'].cumsum()
df_selected['time_cumsum'] = df_selected['time_delta'].cumsum()
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_8.py
Created February 9, 2020 18:36
GPX analysis - step 8
# Here we loop over sections
for section in sections:
if df['distance_dis_3d'].sum() < section: # If the total distance of the workout is smaller then the section we're looking for we can skip this iteration.
continue
df_output = pd.DataFrame(columns=['date', 'section', 'filename', 'time', 'distance', 'minutes_per_kilometer', 'total_distance', 'total_time'])
for i in range(len(df_selected.index)):
@WouterNieuwerth
WouterNieuwerth / GPX_analysis_step_2.py
Last active February 9, 2020 18:51
GPX analysis - step 2
# All the sections you PB's for in meters:
sections = [1000,(1000*1.60934),3000,5000,(5000*1.60934),10000,15000,(10000*1.60934),20000,21097.5,25000,30000,40000,42195]
# The file you want to import:
file = 'my_run_001.gpx'
# This Pandas DataFrame will contain the final output of our analysis:
df_final = pd.DataFrame(columns=['time', 'distance', 'minutes_per_kilometer'])
gpx_file = open(file, 'r')
/**
* Responds to a HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (req, res) => {
let example_content = req.body.example_content || 'Error, no example_content found in json';
let another_example_content = req.body.another_example_content || 'Error, no another_example_content found in json';