-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpxtail
executable file
·37 lines (26 loc) · 1.03 KB
/
gpxtail
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
#!/usr/bin/env python
import argparse
import logging
import gpxlib
DEFAULT_LIMIT = 10
def main():
# https://stackoverflow.com/questions/10027242/python-argparse-to-handle-arbitrary-numeric-options-like-head1
parser = argparse.ArgumentParser(description="The tail equivalent for GPX files")
parser.add_argument("-0", action="store_true", help="null separator")
parser.add_argument("-o", "--output", help="Write output to OUTPUT")
parser.add_argument("file", nargs="?")
args, pass_args = parser.parse_known_args()
logging.basicConfig(level=1, format="%(asctime)s -- %(message)s")
gpx_in, points = gpxlib.read(args.file)
gpx_out, segment = gpxlib.create(gpx_in)
limit = abs(int(pass_args[0])) if len(pass_args) else DEFAULT_LIMIT
for idx in range(len(points) - limit, len(points)):
segment.points.append(points[idx])
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()