Skip to main content
Working from scratch, following simplicity

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:

The last month of GPS data

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:

XLS or PDF exported data

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 GPXGPX, or GPS Exchange Format, is an XML schema designed as a common GPS data format for software applications. It can be used to describe waypoints, tracks, and routes. The format is open and can be used without the need to pay license fees. Its tags store location, elevation, and time and can in this way be used to interchange data between GPS devices and software packages. Such computer programs allow users, for example, to view their tracks, project their tracks on satellite images or other maps, annotate maps, and tag photographs with the geolocation in the Exif metadata. is a piece of code like this:

<?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>
[...]
Three Python scripts to analyse and visualize GPX files into HTML or PDF

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: gpxpygpxpy -- GPX file parser - This is a simple Python library for parsing and manipulating GPX files. GPX is an XML-based format for GPS tracks. Source: https://pypi.org/project/gpxpy/, foliumFolium - folium makes it easy to visualize data that’s been manipulated in Python on an interactive leaflet map. It enables both the binding of data to a map for choropleth visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map. Source: http://python-visualization.github.io/folium/ and pytzpytz brings the Olson tz database into Python. This library allows accurate and cross-platform timezone calculations using Python 2.4 or higher. Source: https://pypi.org/project/pytz/. In the following video you can see how they work:

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:

  1. 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.
  2. 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;
  3. 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:

An example of the output

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; }
Sponsored Links
Pubblicità

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.