Three Python scripts to analyse and convert GPX files into HTML or PDF
Nowadays, many applications can export GPX files from a device, like phones, smartwatches or security adapters. How many times would you want to analyze and reuse that data? Below I present and describe three scripts in Python to make them more readable like an HTML or PDF document.
When I bought a new car two years ago, I started to track it with a hidden GPS gadget, for security reasons and to obtain a discount for my car insurance. It was useless because it was not comparable to a black box. Anyway, it is associated with a web app with many features. As the screenshot below shows:
You can export data in a given interval of time, choosing four file formats: XLS, KML, GPX, and PDF. XLS and PDF are just a table like this:
As you can see, it is a list of coordinates sorted by time, which is complicated to read and understand. And GPX and KML are two markup languages, not easy to understand! The GPX
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <gpx xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns3="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:ns2="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:ns1="http://www.cluetrust.com/XML/GPXDATA/1/0" creator="Zepp App" version="6.5.2-play"> <trk> <trkseg> <trkpt lat="45.08548" lon="11.909348"> <time>2022-01-23T10:08:59Z</time> <extensions> <ns3:TrackPointExtension> <ns3:speed>0.0</ns3:speed> </ns3:TrackPointExtension> </extensions> </trkpt> [...]
So here we are! I needed to find a manner to render these data more comfortable and easy to understand. In Python as usual I found a solution to my problem with the following libraries: gpxpy
I used an existing script and I wrote two scripts in Python to make a simple analysis and visualization of GPX type of file. In particular:
- gpx_file_viewer.py - from a single GPX file generate an HTML file with the mapped track and summary. It also works with GPX files labelled with routes and not tracks. But in that case, the date and time are the execution time of the script.
- gpx_split.py (Copyright (C) 2015 Harvey Chapman <[email protected]>) - splits a GPX file with breaks in the track into separate files stored in a folder that has the same name as the GPX file;
- gpx_folder_viewer.py - giving a folder full of split GPX files, create an HTML file with all the mapped tracks and summary. It doesn't consider the track with a null distance. This file can be printed in A4 format.
Usage
gpx_file_viewer.py filename.gpx --> output DATE_filename_HOUR.html gpx_split.py filename.gpx --> output filename/ (a folder with all the tracks) gpx_folder_viewer.py folder --> output folder.html
The output of the first and third script is an HTML like this:
Download
Get the zip or clone my repository from GitHub: rainnic/python-scripts/gpx_splitted2html/
Making of the video
As usual, I describe in detail the software used to realize the video on my YouTube channel. In this case:
- Blender (https://www.blender.org/) as a video editor, I know this is mainly a 3D software but I was curious and I tested it.
- Inkscape (https://inkscape.org/) to create the graphic of the title and credits.
- ffmpeg (https://ffmpeg.org/) to make the screencast, I use this command:
ffmpeg -video_size 1920x1080 -framerate 30 -f x11grab \ -i :0.0+0,0 -f alsa -ac 2 -i default output.mp4 -i "logo.png" \ -filter_complex "pad=height=ih+10:color=black,overlay=(main_w-overlay_w):main_h-overlay_h"
- A bash script to create the chapters, the title, and the female voice during the video:
# To wait the press of the ALT GR key # Usage: waitKey waitKey() { xinput test-xi2 --root 3 | gawk '/RawKeyRelease/ {getline; getline; print $2; fflush()}' | while read -r key; do if [ $key -eq 108 ]; then break 20; else echo "key is $key"; fi; done;} # To pronounce a text # Usage: say "Text to read" say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=$*&tl=en-UK"; } # To print a text over the screen with a fade in and out effect # Usage: onScreen "Text to print" onScreen() { echo "$1" | aosd_cat -n "Arial Black 100" -R "#00f" -B None -S black -t 2 -f 2000 -u 6000 -o 2000 -p 3 -l 3 -s 192 -r 255; } # To print a blue text over a black screen # Usage: fullScreen "Text" fullScreen() { timeout 10 sm -f "#00f" -b "#000" -a 0 $1; } # To show an image on the screen # Usage: image filename.jpg & image() { timeout 14 eog -f $1; }