Skip to content

Instantly share code, notes, and snippets.

@matthewearl
Created May 28, 2018 15:13
Show Gist options
  • Save matthewearl/8d6831a84b64a6aaa978dda2bf1a6d1c to your computer and use it in GitHub Desktop.
Save matthewearl/8d6831a84b64a6aaa978dda2bf1a6d1c to your computer and use it in GitHub Desktop.
Short script to convert gmap pedometer URLs into GPX files
import sys
import urllib.parse
import urllib.request
import numpy as np
in_url = sys.argv[1]
q = urllib.parse.urlparse(in_url).query
d = urllib.parse.parse_qs(q)
route_id = int(d['r'][0])
if route_id < 5000000:
url = 'https://www.gmap-pedometer.com/getRoute.php'
else:
url = 'https://www.gmap-pedometer.com/gp/ajaxRoute/get'
data = urllib.parse.urlencode({'rId': route_id}).encode()
req = urllib.request.Request(url, data=data)
resp = urllib.request.urlopen(req).read()
d = urllib.parse.parse_qs(resp)
a = np.array([float(x) for x in d[b'polyline'][0].split(b'a')]).reshape(-1, 2)
print("<gpx>", end='')
print("<trk>", end='')
print("<name>{}</name>".format(in_url), end='')
print("<trkseg>", end='')
for lat, lon in a:
print('<trkpt lat="{}" lon="{}" />'.format(lat, lon), end='')
print("</trkseg>", end='')
print("</trk>", end='')
print("</gpx>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment