| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
""" |
| 4 |
Command line utility to extract basic statistics from gpx file(s) |
| 5 |
""" |
| 6 |
|
| 7 |
import pdb |
| 8 |
|
| 9 |
import sys as mod_sys |
| 10 |
import logging as mod_logging |
| 11 |
import math as mod_math |
| 12 |
import argparse as mod_argparse |
| 13 |
|
| 14 |
import gpxpy as mod_gpxpy |
| 15 |
import gpxpy.gpx as mod_gpx |
| 16 |
|
| 17 |
from typing import * |
| 18 |
|
| 19 |
KM_TO_MILES = 0.621371 |
| 20 |
M_TO_FEET = 3.28084 |
| 21 |
|
| 22 |
|
| 23 |
def format_time(time_s: float) -> str: |
| 24 |
if not time_s: |
| 25 |
return 'n/a' |
| 26 |
elif args.seconds: |
| 27 |
return str(int(time_s)) |
| 28 |
else: |
| 29 |
mins, secs = divmod(int(time_s), 60) |
| 30 |
hrs, mins = divmod(mins, 60) |
| 31 |
return f"{hrs:02d}:{mins:02d}:{secs:02d}" |
| 32 |
|
| 33 |
|
| 34 |
def format_long_length(length: float) -> str: |
| 35 |
if args.miles: |
| 36 |
return f'{length / 1000. * KM_TO_MILES:.3f}miles' |
| 37 |
else: |
| 38 |
return f'{length / 1000.:.3f}km' |
| 39 |
|
| 40 |
|
| 41 |
def format_short_length(length: float) -> str: |
| 42 |
if args.miles: |
| 43 |
return f'{length * M_TO_FEET:.2f}ft' |
| 44 |
else: |
| 45 |
return f'{length:.2f}m' |
| 46 |
|
| 47 |
|
| 48 |
def format_speed(speed: float) -> str: |
| 49 |
if not speed: |
| 50 |
speed = 0 |
| 51 |
if args.miles: |
| 52 |
return f'{speed * KM_TO_MILES * 3600. / 1000.:.2f}mph' |
| 53 |
else: |
| 54 |
return f'{speed:.2f}m/s = {speed * 3600. / 1000.:.2f}km/h' |
| 55 |
|
| 56 |
|
| 57 |
def print_gpx_part_info(gpx_part: Union[mod_gpx.GPX, mod_gpx.GPXTrack, mod_gpx.GPXTrackSegment], indentation: str=' ') -> None: |
| 58 |
""" |
| 59 |
gpx_part may be a track or segment. |
| 60 |
""" |
| 61 |
length_2d = gpx_part.length_2d() |
| 62 |
length_3d = gpx_part.length_3d() |
| 63 |
print(f'{indentation}Length 2D: {format_long_length(length_2d or 0)}') |
| 64 |
print(f'{indentation}Length 3D: {format_long_length(length_3d)}') |
| 65 |
|
| 66 |
moving_data = gpx_part.get_moving_data() |
| 67 |
raw_moving_data = gpx_part.get_moving_data(raw=True) |
| 68 |
if moving_data: |
| 69 |
print(f'{indentation}Moving time: {format_time(moving_data.moving_time)}') |
| 70 |
print(f'{indentation}Stopped time: {format_time(moving_data.stopped_time)}') |
| 71 |
print(f'{indentation}Max speed: {format_speed(moving_data.max_speed)} (raw: {format_speed(raw_moving_data.max_speed) if raw_moving_data else "?"})') |
| 72 |
print(f'{indentation}Avg speed: {format_speed(moving_data.moving_distance / moving_data.moving_time) if moving_data.moving_time > 0 else "?"}') |
| 73 |
|
| 74 |
uphill, downhill = gpx_part.get_uphill_downhill() |
| 75 |
print(f'{indentation}Total uphill: {format_short_length(uphill)}') |
| 76 |
print(f'{indentation}Total downhill: {format_short_length(downhill)}') |
| 77 |
|
| 78 |
start_time, end_time = gpx_part.get_time_bounds() |
| 79 |
print(f'{indentation}Started: {start_time}') |
| 80 |
print(f'{indentation}Ended: {end_time}') |
| 81 |
|
| 82 |
points_no = len(list(gpx_part.walk(only_points=True))) |
| 83 |
print(f'{indentation}Points: {points_no}') |
| 84 |
|
| 85 |
if points_no > 0: |
| 86 |
distances: List[float] = [] |
| 87 |
previous_point = None |
| 88 |
for point in gpx_part.walk(only_points=True): |
| 89 |
if previous_point: |
| 90 |
distance = point.distance_2d(previous_point) |
| 91 |
distances.append(distance) |
| 92 |
previous_point = point |
| 93 |
print(f'{indentation}Avg distance between points: {format_short_length(sum(distances) / len(list(gpx_part.walk())))}') |
| 94 |
|
| 95 |
print('') |
| 96 |
|
| 97 |
|
| 98 |
def print_gpx_info(gpx: mod_gpx.GPX, gpx_file: str) -> None: |
| 99 |
print(f'File: {gpx_file}') |
| 100 |
|
| 101 |
if gpx.name: |
| 102 |
print(f' GPX name: {gpx.name}') |
| 103 |
if gpx.description: |
| 104 |
print(f' GPX description: {gpx.description}') |
| 105 |
if gpx.author_name: |
| 106 |
print(f' Author: {gpx.author_name}') |
| 107 |
if gpx.author_email: |
| 108 |
print(f' Email: {gpx.author_email}') |
| 109 |
|
| 110 |
print_gpx_part_info(gpx) |
| 111 |
|
| 112 |
for track_no, track in enumerate(gpx.tracks): |
| 113 |
for segment_no, segment in enumerate(track.segments): |
| 114 |
print(f' Track #{track_no}, Segment #{segment_no}') |
| 115 |
print_gpx_part_info(segment, indentation=' ') |
| 116 |
|
| 117 |
|
| 118 |
def run(gpx_files: List[str]) -> None: |
| 119 |
if not gpx_files: |
| 120 |
print('No GPX files given') |
| 121 |
mod_sys.exit(1) |
| 122 |
|
| 123 |
for gpx_file in gpx_files: |
| 124 |
try: |
| 125 |
gpx = mod_gpxpy.parse(open(gpx_file)) |
| 126 |
print_gpx_info(gpx, gpx_file) |
| 127 |
except Exception as e: |
| 128 |
mod_logging.exception(e) |
| 129 |
print(f'Error processing {gpx_file}') |
| 130 |
mod_sys.exit(1) |
| 131 |
|
| 132 |
|
| 133 |
def make_parser() -> mod_argparse.ArgumentParser: |
| 134 |
parser = mod_argparse.ArgumentParser(usage='%(prog)s [-s] [-m] [-d] [file ...]', |
| 135 |
description='Command line utility to extract basic statistics from gpx file(s)') |
| 136 |
parser.add_argument('-s', '--seconds', action='store_true', |
| 137 |
help='print times as N seconds, rather than HH:MM:SS') |
| 138 |
parser.add_argument('-m', '--miles', action='store_true', |
| 139 |
help='print distances and speeds using miles and feet') |
| 140 |
parser.add_argument('-d', '--debug', action='store_true', |
| 141 |
help='show detailed logging') |
| 142 |
return parser |
| 143 |
|
| 144 |
if __name__ == '__main__': |
| 145 |
args, gpx_files = make_parser().parse_known_args() |
| 146 |
if args.debug: |
| 147 |
mod_logging.basicConfig(level=mod_logging.DEBUG, |
| 148 |
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s') |
| 149 |
run(gpx_files) |