Skip to content

Instantly share code, notes, and snippets.

@WPettersson
Created August 8, 2021 11:22
Show Gist options
  • Save WPettersson/bfbada114042bccb8091ce4ef6b1ac5d to your computer and use it in GitHub Desktop.
Save WPettersson/bfbada114042bccb8091ce4ef6b1ac5d to your computer and use it in GitHub Desktop.
#!/home/enigma/.virtualenvs/gpx_clean/bin/python
import datetime
import os
import os.path
import sys
import numpy as np
import gpxpy
from clean import clean_gpx
TRACKED = [
{
'name': 'Walk',
'distance': 0.0,
'time': 0,
'describe': 'Walked',
'maxspeed': 7,
'maxdist': 13
},
{
'name': 'Running',
'distance': 0.0,
'time': 0,
'describe': 'Ran',
'maxspeed': 12,
'minspeed': 7,
'maxdist': 13
},
{
'name': 'Cycling',
'describe': 'Cycled',
'distance': 0.0,
'time': 0
}
]
def reset():
for exer in TRACKED:
exer['distance'] = 0.0
exer['time'] = 0
def check_file(filename, not_before=None, not_after=None):
with open(filename, 'r') as infile:
gpx = gpxpy.parse(infile)
start, end = gpx.get_time_bounds()
if not_before is not None:
if start < not_before:
print(f"Skip {filename} as too early")
return
if not_after is not None:
if end > not_after:
print(f"Skip {filename} as too late")
return
if not start or not end:
return
if (end - start).total_seconds() < 60:
return
md = gpx.get_moving_data()
distance = md.moving_distance / 1000
time = md.moving_time
clean_gpx(gpx)
md = gpx.get_moving_data()
distance = md.moving_distance / 1000
time = md.moving_time
if time == 0:
return
speed = distance / (time / 3600)
#print(f"{filename} : {speed} km/h ({distance} km)")
for exer in TRACKED:
if 'maxspeed' in exer and speed > exer['maxspeed']:
#print(f"Not {exer['name']} as too fast: {filename}")
continue
if 'minspeed' in exer and speed < exer['minspeed']:
#print(f"Not {exer['name']} as too fast: {filename}")
continue
if 'maxdist' in exer and distance > exer['maxdist']:
#print(f"Not {exer['name']} as too long: {filename}")
continue
exer['distance'] += distance
exer['time'] += time
return
def walk(location):
for root, dirs, files in os.walk(location):
for filename in files:
if filename[-4:] == ".gpx":
check_file(os.path.join(root, filename))
def make_pretty_td(seconds):
string = ""
minutes, seconds = divmod(int(seconds), 60)
hours, minutes = divmod(minutes, 60)
if hours > 0:
string += f"{hours}h, "
if hours + minutes > 0:
string += f"{minutes:02}m, "
string += f"{seconds:02}s"
return string
def summarise(descr=""):
for exer in TRACKED:
duration = exer['time']
if duration < 60:
continue
time = make_pretty_td(duration)
speed = exer['distance'] / (exer['time'] / 3600)
s = f"{exer['describe']:7} {exer['distance']:6.2f} km in {time} ({speed:>5.2f} km/h)"
if descr:
s += f" ({descr})"
print(s)
if __name__ == "__main__":
if len(sys.argv) >= 2:
for path in sys.argv[1:]:
if path[-4:] == ".gpx":
descr = f"{os.path.basename(path)}"
check_file(path)
else:
descr = ""
print(f"Summary for {path}")
walk(path)
summarise(descr)
reset()
else:
walk(".")
summarise()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment