1. Andy Mikhailenko
  2. scripts

Source

scripts / gpx-analyse.py

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK

#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Analyses given GPS track (a .gpx file) and prints a report.

Requirements

* argh
* gpxpy

"""
import argh
import gpxpy
import gpxpy.gpx


def analyze_file(path, fix_logmee=False, fix_sportstracker=False, verbose=False):
    if fix_logmee:
        #
        # HACK: monkey-patch the parser to allow custom date for "GPS LogMee"
        # example: Sun May 26 2013 22:51:25 GMT+0600 (YEKT)
        #
        gpxpy.gpx.DATE_FORMAT = '%a %b %d %Y %H:%M:%S GMT%z (%Z)'

    if fix_sportstracker:
        #
        # HACK: monkey-patch the parser to allow custom date for "SportsTracker"
        # example: 2012-07-26T19:06:23.14
        #
        gpxpy.gpx.DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'

        #
        # unfortunately sometimes ST would log datetime even without milliseconds
        # so this would also be useful but we don't have enough control here on
        # the parser to try a sequence of formats:
        #
        #   gpxpy.gpx.DATE_FORMAT = '%Y-%m-%dT%H:%M:%S'


    gpx = gpxpy.parse(open(path))

    prev_point = None
    start_point = None
    #end_point =  None
    speeds = []

    for track in gpx.tracks:
        if verbose:
            print('* track', track)
        for segment in track.segments:
            if verbose:
                print('* segment', segment)
            seg_since, seg_until = segment.get_time_bounds()
            seg_length_km = segment.length_2d() / 1000
            seg_duration_minutes = (segment.get_duration() or 0) / 60
            print('Segment: {0:.2f} km in {1:.2f} minutes (~{2:.2f} km/h)'.format(
                seg_length_km,
                seg_duration_minutes,
                seg_length_km * (60. / seg_duration_minutes)))
            print('  since', seg_since)
            print('  until', seg_until)
            #moving_data = segment.get_moving_data()
            #print('  ', moving_data)
            for point in segment.points:
                if verbose:
                    print('* point', point)

                if point.speed:
                    speeds.append(point.speed)
                elif prev_point:
                    speed = prev_point.speed_between(point)
                    if speed:
                        speeds.append(speed)

                if not start_point:
                    start_point = point
                prev_point = point

    #end_point = prev_point

#    print(dir(end_point))

    print('')

    #print('since', start_point.time)
    #print('until', end_point.time)

    def split_median(xs):
        xs = sorted(xs)
        half_idx = int(len(xs) / 2)
        return xs[:half_idx], xs[half_idx], xs[half_idx:]

    if speeds:
        first_half, median_speed, second_half = split_median(speeds)
        _, q1_speed, _ = split_median(first_half)
        _, q3_speed, _ = split_median(second_half)
        print('q1 speed {0:.2f} km/h'.format(q1_speed * 3.6))
        #print('avg speed {0:.2f} km/h'.format(sum(speeds) * 3.6 / len(speeds)))
        print('q2 speed {0:.2f} km/h'.format(median_speed * 3.6))
        print('q3 speed {0:.2f} km/h'.format(q3_speed * 3.6))
        print('max speed {0:.2f} km/h'.format(max(speeds) * 3.6))


def main(fix_logmee=False, fix_sportstracker=False, verbose=False, *paths):
    for path in paths:
        analyze_file(path, fix_logmee=fix_logmee, fix_sportstracker=fix_sportstracker,
                     verbose=verbose)
        print('')
        print('---')
        print('')


if __name__ == '__main__':
    argh.dispatch_command(main)