diff options
Diffstat (limited to 'bin/gpx')
-rw-r--r-- | bin/gpx/footer | 1 | ||||
-rwxr-xr-x | bin/gpx/gpx.check.py | 85 | ||||
-rwxr-xr-x | bin/gpx/gpx.merge_gpx.sh | 5 | ||||
-rwxr-xr-x | bin/gpx/gpx.merge_trk.sh | 4 | ||||
-rwxr-xr-x | bin/gpx/gpx2geojson.sh | 48 | ||||
-rw-r--r-- | bin/gpx/header | 10 |
6 files changed, 153 insertions, 0 deletions
diff --git a/bin/gpx/footer b/bin/gpx/footer new file mode 100644 index 0000000..d0759c0 --- /dev/null +++ b/bin/gpx/footer | |||
@@ -0,0 +1 @@ | |||
</gpx> | |||
diff --git a/bin/gpx/gpx.check.py b/bin/gpx/gpx.check.py new file mode 100755 index 0000000..10be97c --- /dev/null +++ b/bin/gpx/gpx.check.py | |||
@@ -0,0 +1,85 @@ | |||
1 | #!/usr/bin/env python3 | ||
2 | |||
3 | import sys | ||
4 | import os | ||
5 | import argparse | ||
6 | import copy | ||
7 | import fileinput | ||
8 | from osgeo import ogr | ||
9 | import osr | ||
10 | import urllib.parse | ||
11 | |||
12 | def 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 | |||
20 | def 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 | |||
75 | def 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 | |||
84 | if __name__ == '__main__': | ||
85 | main(sys.argv) | ||
diff --git a/bin/gpx/gpx.merge_gpx.sh b/bin/gpx/gpx.merge_gpx.sh new file mode 100755 index 0000000..4b024a7 --- /dev/null +++ b/bin/gpx/gpx.merge_gpx.sh | |||
@@ -0,0 +1,5 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | GPX_DIR=$(dirname $0) | ||
4 | |||
5 | sed '/<trk/,/<\/trk>/ p' -nr | cat $GPX_DIR/header - $GPX_DIR/footer | ||
diff --git a/bin/gpx/gpx.merge_trk.sh b/bin/gpx/gpx.merge_trk.sh new file mode 100755 index 0000000..c3a72d4 --- /dev/null +++ b/bin/gpx/gpx.merge_trk.sh | |||
@@ -0,0 +1,4 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | sed '/<trk>/,/<\/name>/ d; /<\/trk>/ d; /<\/gpx>/ i \ \ <\/trk>' |\ | ||
4 | awk '/<trkseg>/ && !x {print " <trk>\n <name>combined_trk</name>"; x=1} 1' | ||
diff --git a/bin/gpx/gpx2geojson.sh b/bin/gpx/gpx2geojson.sh new file mode 100755 index 0000000..eefcf5e --- /dev/null +++ b/bin/gpx/gpx2geojson.sh | |||
@@ -0,0 +1,48 @@ | |||
1 | #! /bin/bash | ||
2 | |||
3 | <$1 xq '.gpx | | ||
4 | ( | ||
5 | label $out | | ||
6 | if .wpt != null then [.wpt] else break $out end | | ||
7 | flatten[] | | ||
8 | { | ||
9 | type: "Feature", | ||
10 | properties: { name: .name }, | ||
11 | geometry: { | ||
12 | type: "Point", | ||
13 | coordinates: [ | ||
14 | (.["@lon"]|tonumber), | ||
15 | (.["@lat"]|tonumber), | ||
16 | (.ele | if . == null then null else tonumber end) | ||
17 | ] | ||
18 | } | ||
19 | } | ||
20 | ), | ||
21 | ( | ||
22 | label $out | | ||
23 | if .trk != null then [.trk] else break $out end | | ||
24 | flatten[] | | ||
25 | { | ||
26 | type: "Feature", | ||
27 | properties: { name: .name }, | ||
28 | geometry: { | ||
29 | type: "MultiLineString", | ||
30 | coordinates: | ||
31 | [.trkseg] | flatten | map( | ||
32 | .trkpt | map( | ||
33 | [ | ||
34 | (.["@lon"]|tonumber), | ||
35 | (.["@lat"]|tonumber), | ||
36 | (.ele | if . == null then null else tonumber end) | ||
37 | ] | ||
38 | ) | ||
39 | ) | ||
40 | } | ||
41 | } | ||
42 | ) | ||
43 | ' | jq -s ' | ||
44 | { | ||
45 | type: "FeatureCollection", | ||
46 | features: . | ||
47 | } | ||
48 | ' | ||
diff --git a/bin/gpx/header b/bin/gpx/header new file mode 100644 index 0000000..1912e8b --- /dev/null +++ b/bin/gpx/header | |||
@@ -0,0 +1,10 @@ | |||
1 | <?xml version="1.0" encoding="UTF-8" standalone="no" ?> | ||
2 | <gpx xmlns="http://www.topografix.com/GPX/1/1" creator="MapSource 6.10.2" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"> | ||
3 | |||
4 | <metadata> | ||
5 | <link href="http://www.garmin.com"> | ||
6 | <text>Garmin International</text> | ||
7 | </link> | ||
8 | <time>2019-12-18T06:17:16Z</time> | ||
9 | <bounds maxlat="24.554773" maxlon="121.293261" minlat="24.534487" minlon="121.284309"/> | ||
10 | </metadata> | ||