-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgpxcomment
executable file
·83 lines (70 loc) · 2.1 KB
/
gpxcomment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
import argparse
import logging
import os
import sys
import traceback
import gpxlib
DEFAULT_LOG_LEVEL = "info"
def main():
parser = argparse.ArgumentParser(
description="A tool to annotate a GPX file with comments based on a reference GPX track"
)
parser.add_argument(
"-l",
"--log",
help="Log level (INFO, DEBUG, WARNING, ERROR)",
default=DEFAULT_LOG_LEVEL,
)
parser.add_argument("-o", "--output", help="Write output to OUTPUT")
parser.add_argument(
"-r",
"--reference",
help="The reference GPX track (e.g., from a Garmin or Wahoo)",
required=True,
)
parser.add_argument(
"-s",
"--snap",
help="Pauses snap to within N meters",
default=gpxlib.DEFAULT_PAUSE_SNAP,
)
parser.add_argument(
"-z",
"--force-timezone",
help="Force per-point timezone lookup",
action="store_true",
)
parser.add_argument("files", nargs="*")
args = parser.parse_args()
numeric_level = getattr(logging, args.log.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError("Invalid log level: %s" % args.log)
logging.basicConfig(level=numeric_level, format="%(asctime)s -- %(message)s")
if not os.path.isfile(args.reference):
sys.exit("--reference parameter %s does not exist" % (args.reference))
logging.info("Parsing " + args.reference)
gpx_in, ref_points = gpxlib.read(args.reference)
gpx_out, segment = gpxlib.create(gpx_in)
# read all points from all given files
points = []
for f in args.files:
_, xpoints = gpxlib.read(f)
points += xpoints
try:
segment.points = gpxlib.gpxcomment(
points,
ref_points,
force_timezone=args.force_timezone,
pause_snap=int(args.snap),
)
except Exception:
sys.exit(traceback.format_exc())
s = gpx_out.to_xml()
if args.output:
with open(args.output, "w") as f:
f.write(s)
else:
print(s)
if __name__ == "__main__":
main()