SHARE
TWEET

SVMyRide

a guest Jul 18th, 2013 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. """
  3. SVMyRide
  4.  
  5. Takes a gpx file, loads the points + calcs bearing, gets a matching SV image
  6. Only produces images: convert to a video via ffmpeg, avidemux, etc
  7. """
  8.  
  9. import xml.etree.ElementTree as ET
  10. import urllib, os, math
  11.  
  12. BOFFSET = 2 # averages out bearing over +- 2 points to stop video shaking
  13.             # from side-to side. Should probably only look forward instead...
  14.  
  15. def calc_bearing(pointA, pointB):
  16.         lat1 = math.radians(pointA[0])
  17.         lat2 = math.radians(pointB[0])
  18.         diffLong = math.radians(pointB[1] - pointA[1])
  19.  
  20.         x = math.sin(diffLong) * math.cos(lat2)
  21.         y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(diffLong))
  22.          
  23.         initial_bearing = math.atan2(x, y)
  24.         initial_bearing = math.degrees(initial_bearing)
  25.         compass_bearing = (initial_bearing + 360) % 360
  26.          
  27.         return compass_bearing
  28.  
  29. #filename is hard-coded (sorry!)
  30. #also gpx data from non-Strava sources may need extra processing here
  31. gpx_file = ET.parse('test.gpx')
  32. gpx_trackseg = gpx_file.getroot()[1][1]
  33. points = []
  34. for point in gpx_trackseg:
  35.         points.append(point)
  36.        
  37. #smooth/average out bearings: this should really be changed to only look forward
  38. smoothBearing = []
  39. for iPoint in range(len(points)):
  40.         if BOFFSET <= iPoint < len(points)-BOFFSET:
  41.                 smoothBearing.append(int(calc_bearing((float(points[iPoint-BOFFSET].attrib['lat']),float(points[iPoint-BOFFSET].attrib['lon'])),(float(points[iPoint+BOFFSET].attrib['lat']),float(points[iPoint+BOFFSET].attrib['lon'])))))
  42. points = points[BOFFSET:-BOFFSET]
  43.        
  44. iPoint = 1
  45. while iPoint < len(points):
  46.         # NB: YOU NEED TO ADD YOU OWN GOOGLE API KEY TO THE LINE BELOW:
  47.         sUrl = 'http://maps.googleapis.com/maps/api/streetview?size=640x400&location=%f,%f&heading=%d&pitch=0&sensor=false&key=PUTYOURKEYHERE' % (float(points[iPoint].attrib['lat']),float(points[iPoint].attrib['lon']),smoothBearing[iPoint])
  48.         urllib.urlretrieve(sUrl, '%05d.png' % iPoint)
  49.         iPoint += 1
RAW Paste Data
Top