-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_concat_splited.py
executable file
·68 lines (53 loc) · 1.6 KB
/
filter_concat_splited.py
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
#!/usr/bin/env python3
from pathlib import Path
import click
import gpxpy
from tqdm import tqdm
@click.group
def commands():
pass
@commands.command()
@click.argument('paths', nargs=-1)
def walk(paths):
for gpx_path in tqdm(paths):
with open(gpx_path) as f:
try:
gpx = gpxpy.parse(f)
except Exception as e:
if isinstance(e, KeyboardInterrupt):
raise e
print(f'Exception occurred parsing {gpx_path}: \n{e}')
continue
assert len(gpx.tracks) == 1
track = gpx.tracks[0]
if len(track.segments) > 1:
tqdm.write(gpx_path)
def concat_segments(track, thres=200):
# Remove empty segments
track.segments = [s for s in track.segments if len(s.points) > 0]
if len(track.segments) == 1:
return track
# Concat segments
base_seg = track.segments[0]
for seg in track.segments[1:]:
dist = base_seg.points[-1].distance_2d(seg.points[0])
if dist > thres:
raise ValueError(
f'Segment different too large: {dist}, concat aborted.')
base_seg.join(seg)
track.segments = [base_seg]
return track
@commands.command()
@click.argument('path')
@click.option(
'--thres', type=int, default=200, help='Threshold for merging, in meters')
def concat(path, thres):
path = Path(path)
with path.open() as f:
gpx = gpxpy.parse(f)
track = gpx.tracks[0]
concat_segments(track, thres)
with path.open('w') as f:
f.write(gpx.to_xml())
if __name__ == '__main__':
commands()