Created
May 2, 2016 02:49
-
-
Save heltonbiker/df0ca2e2e0ba74926dbbb06ec98257eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
import urllib | |
import json | |
import os | |
import matplotlib.pyplot as plt | |
import datetime | |
import math | |
def distance(p1,p2): | |
lat1, long1, lat2, long2 = [math.radians(float(x)) for x in | |
(p1[0], p1[1], p2[0], p2[1])] | |
d_lat = lat2 - lat1 | |
d_long = long2 - long1 | |
a = math.sin(d_lat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(d_long/2)**2 | |
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) | |
return 6378137 * c | |
def accumulator(lis): | |
total = 0 | |
for x in lis: | |
total += x | |
yield total | |
mainActivity = 562503255 ### REPLACE BY YOUR OWN ACTIVITY HERE!! ### | |
maindir = str(mainActivity) | |
matchesUrl = "https://nene.strava.com/flyby/matches/{}".format(mainActivity) | |
matchesFname = '{}/matches.json'.format(maindir) | |
try: | |
with open(matchesFname) as jsonin: | |
matches = json.load(jsonin) | |
except: | |
if not os.path.exists(maindir): | |
os.makedirs(maindir) | |
matchesContent = urllib.urlopen(matchesUrl).read() | |
with open(matchesFname, 'w') as jsonout: | |
jsonout.write(matchesContent) | |
matches = json.loads(matchesContent) | |
athletes = matches['athletes'] | |
for match in matches['matches']: | |
activityId = match['otherActivity']['id'] | |
athlId = str(match['otherActivity']['athleteId']) | |
correlation = match['correlation']['spatialCorrelation'] | |
if correlation < 0.9: | |
continue | |
otherFname = '{}/{}.json'.format(mainActivity, activityId) | |
try: | |
with open(otherFname) as otherin: | |
other = json.load(otherin) | |
except: | |
otherUrl = ("https://nene.strava.com"+ | |
"/flyby/stream_compare/545858689/"+ | |
"{}".format(activityId)) | |
otherContent = urllib.urlopen(otherUrl).read() | |
with open(otherFname, 'w') as otherout: | |
otherout.write(otherContent) | |
other = json.loads(otherContent) | |
stream = other['stream'] | |
times = [] | |
lats = [] | |
lons = [] | |
for point in stream: | |
times.append(datetime.datetime.fromtimestamp(point['time'])) | |
lats.append(point['point']['lat']) | |
lons.append(point['point']['lng']) | |
dtimes = [0.0001] + [(times[n] - times[n-1]).total_seconds() for n in xrange(1, len(times))] | |
cumtimes = [v / 3600 for v in accumulator(dtimes)] | |
pts = zip(lats, lons) | |
dists = [0] + [distance(pts[n], pts[n-1]) for n in xrange(1, len(pts))] | |
cumdists = [v / 1000 for v in accumulator(dists)] | |
averages = [x[0]/x[1] for x in zip(cumdists, cumtimes)] | |
plt.figure(1) | |
plt.plot(lons, lats, alpha=0.5) | |
plt.figure(2) | |
plt.plot(cumdists, averages, lw=2, alpha=0.5, label=athletes[athlId]['firstName']) | |
plt.figure(1) | |
plt.grid() | |
plt.axis('equal') | |
plt.figure(2) | |
plt.xlabel("Tempo (horas)") | |
plt.ylabel(u"Velocidade Média (km/h)") | |
plt.xlim(-5, 450) | |
plt.ylim(0, 35) | |
plt.legend(loc="lower left", prop={'size':8}) | |
plt.grid() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment