aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/gpx/gpx.check.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/gpx/gpx.check.py')
-rwxr-xr-xtools/gpx/gpx.check.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/tools/gpx/gpx.check.py b/tools/gpx/gpx.check.py
new file mode 100755
index 0000000..10be97c
--- /dev/null
+++ b/tools/gpx/gpx.check.py
@@ -0,0 +1,85 @@
1#!/usr/bin/env python3
2
3import sys
4import os
5import argparse
6import copy
7import fileinput
8from osgeo import ogr
9import osr
10import urllib.parse
11
12def rewrite_gpx(filename):
13 for line in fileinput.input(filename, inplace=True):
14 if fileinput.isfirstline() and "'" in line:
15 line = '<?xml version="1.0" encoding="UTF-8"?>'
16 if fileinput.filelineno() == 2 and "version" not in line:
17 line = line.replace('<gpx', '<gpx version="1.1"')
18 print(line.rstrip('\n'))
19
20def check_valid(filename, threshold, add_prefix):
21 rewrite_gpx(filename)
22
23 driver = ogr.GetDriverByName('GPX')
24 try:
25 dataSource = driver.Open(filename)
26 except Exception:
27 pass
28 if dataSource is None:
29 print("could not open")
30 sys.exit(1)
31
32 inSpatialRef = osr.SpatialReference()
33 inSpatialRef.ImportFromEPSG(4326)
34 outSpatialRef = osr.SpatialReference()
35 outSpatialRef.ImportFromEPSG(3857)
36 to3857 = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
37 to4326 = osr.CoordinateTransformation(outSpatialRef, inSpatialRef)
38
39 trkLayer = dataSource.GetLayer(4)
40 trkpt = trkLayer.GetNextFeature()
41 flag = False
42 while trkpt:
43 nextTrkpt = trkLayer.GetNextFeature()
44 if nextTrkpt:
45 geom1 = trkpt.GetGeometryRef()
46 geom1.Transform(to3857)
47 geom2 = nextTrkpt.GetGeometryRef()
48 geom2.Transform(to3857)
49 distance = geom1.Distance(geom2)
50
51 geom1.Transform(to4326)
52 geom2.Transform(to4326)
53 if distance >= threshold:
54 if not flag:
55 print(f'{filename} has problem, the following urls shows the points with distance far from {threshold}m:')
56 print()
57 flag = True
58 if add_prefix:
59 dir = os.path.dirname(filename)
60 if dir:
61 dir += '/'
62 os.rename(filename, f'{dir}invalid_{os.path.basename(filename)}')
63
64 geojson = '{{"type": "LineString", "coordinates": [[{}, {}], [{}, {}]]}}'.format(
65 geom1.GetX(), geom1.GetY(),
66 geom2.GetX(), geom2.GetY()
67 )
68 encoded = urllib.parse.quote(geojson)
69 print('http://geojson.io/#data=data:application/json,{}'.format(encoded))
70 print()
71 else:
72 break
73 trkpt = nextTrkpt
74
75def main(argv):
76 parser = argparse.ArgumentParser()
77 parser.add_argument('file', help="you can add multiple gpx files at the same time", nargs='+')
78 parser.add_argument("-i", help="add prefix to invalid files", action="store_true")
79 parser.add_argument("-d", help="distance of tolerance(m), 100 by default", dest="distance", default=100)
80 args = parser.parse_args()
81 for file in args.file:
82 check_valid(file, float(args.distance), args.i)
83
84if __name__ == '__main__':
85 main(sys.argv)