Skip to content

Instantly share code, notes, and snippets.

@ychalier
Created August 20, 2020 01:42
Show Gist options
  • Save ychalier/dea783a23af22198324fef4f40b47181 to your computer and use it in GitHub Desktop.
Save ychalier/dea783a23af22198324fef4f40b47181 to your computer and use it in GitHub Desktop.
Generate a information thumbnail from a GPX file.
"""Generate a information thumbnail from a GPX file.
"""
import io
import re
import os
import math
import time
import uuid
import base64
import codecs
import logging
import argparse
import urllib.parse
import xml.etree.ElementTree
import PIL.Image
import matplotlib.pyplot
import selenium.webdriver.support.expected_conditions
import selenium.webdriver.firefox.options
import selenium.webdriver.support.wait
import selenium.webdriver.common.by
import selenium.webdriver
import seaborn
import slugify
import folium
seaborn.set(style="darkgrid")
GPX_NS = "{http://www.topografix.com/GPX/1/1}"
GECKODRIVER = r"C:\Scripts\geckodriver.exe"
def load_gpx(filename):
"""Load a GPX file. Return a list of (latitude, longitude, elevation).
"""
tree = xml.etree.ElementTree.parse(filename)
root = tree.getroot()
trkseg = list()
for trkpt in root.find(GPX_NS + "trk").find(GPX_NS + "trkseg"):
lat = float(trkpt.attrib["lat"])
lon = float(trkpt.attrib["lon"])
ele_el = trkpt.find(GPX_NS + "ele")
ele = (float(ele_el.text) if ele_el is not None else None)
trkseg.append((lat, lon, ele))
return trkseg
def create_map(trkseg):
"""Create a folium map with the track segment displayed on it.
"""
lats = list(map(lambda x: x[0], trkseg))
lons = list(map(lambda x: x[1], trkseg))
mean_lat = sum(lats) / len(trkseg)
mean_lon = sum(lons) / len(trkseg)
map_ = folium.Map(location=[mean_lat, mean_lon], control_scale=True)
folium.features.ColorLine(
positions=[trkpt[:2] for trkpt in trkseg],
colors=[1 for _ in trkseg],
colormap=["r", "r"],
weight=5,
opacity=.6
).add_to(map_)
southwest = [min(lats), min(lons)]
northeast = [max(lats), max(lons)]
map_.fit_bounds([southwest, northeast])
filename = os.path.realpath("_tmp" + uuid.uuid4().hex + ".html")
map_.save(filename)
with open(filename) as file:
html = file.read()
with open(filename, "w") as file:
file.write(re.sub("openstreetmap", "opentopomap", html))
return filename
def create_browser():
"""Create selenium driver handle.
"""
options = selenium.webdriver.firefox.options.Options()
options.headless = True
driver = selenium.webdriver.Firefox(
options=options,
executable_path=GECKODRIVER,
service_log_path=os.path.devnull
)
driver.set_window_size(1080, 1080 + 74)
return driver
def export_map_to_png(driver, filename, delay=1):
# pylint: disable=C0301
"""Create a PNG out of a folium map.
"""
driver.get("file://{}".format(filename))
driver.execute_script("document.body.style.width = '100%';")
driver.execute_script("document.querySelector('.leaflet-control-container .leaflet-top.leaflet-left').style.display = 'none';")
driver.execute_script("document.querySelector('.leaflet-control-container .leaflet-bottom.leaflet-right').style.display = 'none';")
driver.execute_script("document.querySelector('.leaflet-tile-container').style.opacity = 0.8;")
time.sleep(delay)
png = driver.get_screenshot_as_png()
filename = os.path.realpath("_tmp" + uuid.uuid4().hex + ".png")
PIL.Image.open(io.BytesIO(png)).save(filename)
return filename
def distance(lat1, lon1, lat2, lon2):
# pylint: disable=C0103
"""Compute the distance in meters between two points.
"""
r = 6371000
phi1 = lat1 * math.pi / 180.
phi2 = lat2 * math.pi / 180.
delta_phi = (lat2 - lat1) * math.pi / 180.
delta_lambda = (lon2 - lon1) * math.pi / 180.
a = math.sin(.5 * delta_phi) * math.sin(.5 * delta_phi)\
+ math.cos(phi1)\
* math.cos(phi2)\
* math.sin(.5 * delta_lambda)\
* math.sin(.5 * delta_lambda)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
d = r * c
return d
def add_gpx_elevation(driver, filename):
"""Create a new GPX file with elevation data.
"""
driver.get("https://www.gpsvisualizer.com/elevation")
file_upload = driver.find_element(
selenium.webdriver.common.by.By.XPATH,
"/html/body/table/tbody/tr/td[2]/form/div/div[1]/input[1]"
)
file_upload.send_keys(filename)
submit_button = driver.find_element(
selenium.webdriver.common.by.By.XPATH,
"/html/body/table/tbody/tr/td[2]/form/div/div[2]/div[1]/input"
)
submit_button.click()
selenium.webdriver.support.wait.WebDriverWait(driver, 10).until(
selenium.webdriver.support.expected_conditions.presence_of_element_located((
selenium.webdriver.common.by.By.XPATH,
"/html/body/table/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/iframe"
))
)
frame = driver.find_element(
selenium.webdriver.common.by.By.XPATH,
"/html/body/table/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/iframe"
)
driver.switch_to.frame(frame)
output_pre = driver.find_element(
selenium.webdriver.common.by.By.XPATH,
"/html/body/pre"
)
filename = os.path.realpath("_tmp" + uuid.uuid4().hex + ".gpx")
with codecs.open(filename, "w", "utf8") as file:
file.write(output_pre.text)
return filename
def parse_trkseg(trkseg):
"""Parse a track segment.
"""
prev_pos = trkseg[0][:2]
prev_ele = trkseg[0][2]
tot_ele_up, tot_ele_down = 0, 0
tot_dist = 0
distances, elevations = [], []
for lat, lon, ele in trkseg:
tot_dist += distance(*prev_pos, lat, lon)
distances.append(tot_dist)
ele_diff = ele - prev_ele
if ele_diff > 0:
tot_ele_up += ele_diff
else:
tot_ele_down -= ele_diff
prev_pos = (lat, lon)
prev_ele = ele
elevations.append(ele)
return tot_dist, tot_ele_up, tot_ele_down, (distances, elevations)
def plot_elevation_profile(distances, elevations):
"""Plot the elevation profile.
"""
dpi = 100
figure, axis = matplotlib.pyplot.subplots(figsize=(1080./dpi, 360./dpi))
axis.plot(distances, elevations, "r")
xticks = [1000 * i for i in range(math.ceil(.001 * distances[-1]))][1::2]
xlabels = ["%dkm" % (.001 * tick) for tick in xticks]
axis.set_xlim(distances[0], distances[-1])
axis.set_ylim(bottom=min(elevations))
axis.set_xticks(xticks)
axis.set_xticklabels(xlabels, ha="right")
seaborn.despine(left=True, bottom=True, right=True, top=True)
axis.tick_params(direction="in")
for tick in [*axis.xaxis.get_major_ticks(), *axis.xaxis.get_minor_ticks()]:
tick.set_pad(-12)
for tick in [*axis.yaxis.get_major_ticks(), *axis.yaxis.get_minor_ticks()]:
tick.set_pad(-30)
axis.fill_between(
distances, elevations, [min(elevations) for _ in elevations],
color="r", alpha=.1)
matplotlib.pyplot.tight_layout(pad=0)
filename = os.path.realpath("_tmp" + uuid.uuid4().hex + ".png")
figure.savefig(filename, dpi=dpi)
return filename
def format_sexagesimal(degree):
"""Convert a degree into the sexagesimal format.
"""
if degree < 0:
degree = -degree
minutes = 1 / 60
seconds = 1 / 3600
string = ""
remainder = int(degree)
string += "%s°" % str(int(remainder))
degree -= remainder
remainder = degree // minutes
string += "%s'" % str(int(remainder)).zfill(2)
degree -= minutes * remainder
remainder = degree / seconds
string += ("%.1f" % remainder).zfill(4) + '"'
return string
def format_latlon(lat, lon):
"""Format latitude and longitude into sexagesimal format.
"""
return format_sexagesimal(lat) + ("N" if lat >= 0 else "S")\
+ " " + format_sexagesimal(lon) + ("E" if lon >= 0 else "W")
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>{title}</title>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&display=swap" rel="stylesheet">
<style media="screen">
html {{
padding: 16px;
display: flex;
flex-diretion: row;
justify-content: center;
}}
body {{
text-align: center;
}}
@media all and (max-width:1200px) {{
body {{
width: 100%;
}}
}}
* {{
font-family: 'Open Sans', sans-serif;
}}
h1 {{
font-size: 64px;
max-width: 1080px;
margin: auto;
}}
.separator {{
display: inline-block;
width: 64px;
}}
.img_wrapper {{
margin: auto;
max-width: 1080px;
height: auto;
border-radius: 8px;
overflow: hidden;
margin-bottom: 16px;
box-shadow: 0 2px 2px rgba(0, 0, 0, .1);
}}
.img_wrapper img {{
width: inherit;
}}
h2 {{
font-size: 32px;
font-weight: normal;
display: flex;
flex-direction: row;
justify-content: space-between;
max-width: 1080px;
margin: auto;
margin-top: 24px;
margin-bottom: 24px;
}}
a {{
color: black;
}}
</style>
</head>
<body>
<h1>{title}</h1>
<h2>
<span>{tot_dist:.1f}km</span>
<span>&nearr; {tot_ele_up:.0f}m</span>
<span>&searr; {tot_ele_down:.0f}m</span>
<span>&#9872; <a href="https://www.google.com/maps/place/{start_url}">{start_surface}</a></span>
</h2>
<div class="img_wrapper" style="height: 1080px;">
<a href="{track}"><img src="data:image/png;base64,{img_map}" alt=""></a>
</div>
<div class="img_wrapper" style="height: 360px;">
<img src="data:image/png;base64,{img_ele}" alt="">
</div>
</body>
</html>
"""
def _main(args): # pylint: disable=R0914
"""Set of operations.
"""
logging.info("Using file '%s' as input", os.path.realpath(args.gpx))
driver = create_browser()
logging.info("Created selenium handle")
gpxfn = add_gpx_elevation(driver, os.path.realpath(args.gpx))
logging.info("Fetched elevation data and saved it to '%s'", gpxfn)
trkseg = load_gpx(gpxfn)
logging.info("Loaded GPX and found a track with %d points", len(trkseg))
mapfn = create_map(trkseg)
logging.info("Exported folium map to '%s'", mapfn)
pngfn = export_map_to_png(driver, mapfn)
logging.info("Exported map image to '%s'", pngfn)
driver.quit()
logging.info("Closed selenium handle")
tot_dist, tot_ele_up, tot_ele_down, elevation_profile = parse_trkseg(trkseg)
logging.info("Parsed track (total distance: %dm)", tot_dist)
elefn = plot_elevation_profile(*elevation_profile)
start = format_latlon(*trkseg[0][:2])
with open(pngfn, "rb") as image_file:
img_map = base64.b64encode(image_file.read()).decode()
with open(elefn, "rb") as image_file:
img_ele = base64.b64encode(image_file.read()).decode()
html = HTML_TEMPLATE.format(
title=args.title,
tot_dist=.001 * tot_dist,
tot_ele_up=tot_ele_up,
tot_ele_down=tot_ele_down,
start_surface=start,
start_url=urllib.parse.quote(start),
img_map=img_map,
img_ele=img_ele,
track=os.path.basename(args.gpx),
)
with codecs.open(slugify.slugify(args.title) + ".html", "w", "utf8") as file:
file.write(html)
logging.info("Plotted elevation profile at '%s'", elefn)
os.remove(mapfn)
os.remove(pngfn)
os.remove(gpxfn)
os.remove(elefn)
logging.info("Cleaned temp files")
def main():
"""Parse arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument("gpx", type=str, help="GPX input file")
parser.add_argument("title", type=str)
_main(parser.parse_args())
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment