Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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 = 2 # 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('test.gpx')
- gpx_trackseg = gpx_file.getroot()[1][1]
- 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-BOFFSET].attrib['lat']),float(points[iPoint-BOFFSET].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=640x400&location=%f,%f&heading=%d&pitch=0&sensor=false&key=PUTYOURKEYHERE' % (float(points[iPoint].attrib['lat']),float(points[iPoint].attrib['lon']),smoothBearing[iPoint])
- urllib.urlretrieve(sUrl, '%05d.png' % iPoint)
- iPoint += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement