Created
May 13, 2015 23:26
-
-
Save angel-devicente/cac3a4434c4bd5b756ea to your computer and use it in GitHub Desktop.
Script to create gpx hyperlapses (see
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/python | |
""" | |
SVMyRide | |
Takes a gpx file, loads the points + calcs bearing, gets a matching SV image | |
Only produces images: convert to a video via ffmpeg, avidemux, etc | |
""" | |
import xml.etree.ElementTree as ET | |
import urllib, os, math | |
BOFFSET = 4 # averages out bearing over +- 2 points to stop video shaking | |
# from side-to side. Should probably only look forward instead... | |
def calc_bearing(pointA, pointB): | |
lat1 = math.radians(pointA[0]) | |
lat2 = math.radians(pointB[0]) | |
diffLong = math.radians(pointB[1] - pointA[1]) | |
x = math.sin(diffLong) * math.cos(lat2) | |
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(diffLong)) | |
initial_bearing = math.atan2(x, y) | |
initial_bearing = math.degrees(initial_bearing) | |
compass_bearing = (initial_bearing + 360) % 360 | |
return compass_bearing | |
#filename is hard-coded (sorry!) | |
#also gpx data from non-Strava sources may need extra processing here | |
gpx_file = ET.parse('input.gpx') | |
#gpx_trackseg = gpx_file.getroot()[1][3] # For Endomondo .gpx | |
#gpx_trackseg = gpx_file.getroot()[2][1] # For GPS visualizer .gpx | |
gpx_trackseg = gpx_file.getroot()[3][1] # For GPSBABEL .gpx | |
points = [] | |
for point in gpx_trackseg: | |
points.append(point) | |
#smooth/average out bearings: this should really be changed to only look forward | |
smoothBearing = [] | |
for iPoint in range(len(points)): | |
if BOFFSET <= iPoint < len(points)-BOFFSET: | |
smoothBearing.append(int(calc_bearing((float(points[iPoint].attrib['lat']),float(points[iPoint].attrib['lon'])),(float(points[iPoint+BOFFSET].attrib['lat']),float(points[iPoint+BOFFSET].attrib['lon']))))) | |
points = points[BOFFSET:-BOFFSET] | |
iPoint = 1 | |
while iPoint < len(points): | |
# NB: YOU NEED TO ADD YOU OWN GOOGLE API KEY TO THE LINE BELOW: | |
sUrl = 'http://maps.googleapis.com/maps/api/streetview?size=2048x2048&location=%f,%f&heading=%d&pitch=0&sensor=false&key=YOUR_GOOGLE_API_KEY' % (float(points[iPoint].attrib['lat']),float(points[iPoint].attrib['lon']),smoothBearing[iPoint]) | |
print "Downloading ", iPoint, " of ", len(points) | |
urllib.urlretrieve(sUrl, '%05d.jpeg' % iPoint) | |
iPoint += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment