Created
May 28, 2018 15:13
-
-
Save matthewearl/8d6831a84b64a6aaa978dda2bf1a6d1c to your computer and use it in GitHub Desktop.
Short script to convert gmap pedometer URLs into GPX files
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
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