-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpx-cleaner.py
83 lines (71 loc) · 2.75 KB
/
gpx-cleaner.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import xml.etree.ElementTree as ET
import os
import sys
def get_gpx_files(directory):
"""
Creates an array of all gpx files inside a given directory.
"""
files = []
for filename in os.listdir(directory):
if filename.endswith(".gpx"):
files.append(filename)
return files
def clear_gpx_track(gpx_file):
"""
Check all lat/lon values of the trackpoints of a given gpx file.
If the location data of a trackpoint has not changed compared to the previous trackpoint,
the trackpoint is removed from the gpx file.
"""
# Register the custom namespace of gpx files, parse the xml structure and get root element
ET.register_namespace('', 'http://www.topografix.com/GPX/1/1')
gpx = ET.parse(gpx_file)
root = gpx.getroot()
previous_position = (0.0, 0.0)
# Iterate over all trackpoints and remove points from gpx where no movement occured
points = root[1][2].findall('{http://www.topografix.com/GPX/1/1}trkpt')
for child in points:
lat = float(child.attrib['lat'])
lon = float(child.attrib['lon'])
if lat == previous_position[0] and lon == previous_position[1]:
root[1][2].remove(child)
else:
previous_position = (lat, lon)
return gpx
def write_gpx_file(gpx, file):
"""
Saves the gpx data to the given file location.
"""
gpx.write(file)
def main(directory):
"""
Script for clearing all trackpoints of a gpx file where the lat/lon is not different
to the previous trackpoint.
"""
# Check if passed argument is existing directory
if not os.path.isdir(directory):
print("Can't find directory ", directory)
sys.exit(0)
# Get all gpx files inside the given directory
gpx_files = get_gpx_files(directory)
print("Found {} gpx files in directory {}".format(len(gpx_files), directory))
# Check if output directory exists and create new output dir if not
output_dir = os.path.join(directory, "output")
if len(gpx_files) > 0 and not os.path.isdir(output_dir):
try:
os.mkdir(output_dir)
except:
print("Failed to create output directory")
sys.exit(1)
# For each gpx file: clear track points and save new gpx to output dir
for filename in gpx_files:
gpx_file = os.path.join(directory, filename)
cleared_gpx_data = clear_gpx_track(gpx_file)
write_gpx_file(cleared_gpx_data, os.path.join(output_dir, filename))
if __name__ == "__main__":
if len(sys.argv) > 2:
print("Too many arguments! Pass only one directory of gpx files!")
sys.exit(1)
if len(sys.argv) < 2:
print("No argument found! Pass path of directory of gpx files as argument.")
sys.exit(1)
main(sys.argv[1])