diff options
| author | typebrook <typebrook@gmail.com> | 2019-12-10 14:56:47 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2019-12-10 14:56:47 +0800 |
| commit | def0f995e59c3d2bcaa40185b1d82a5049033eab (patch) | |
| tree | 892eb5d2fa603f7f4adf5b43df8e825c7c1a48ec | |
| parent | c72b747d215b7419ac60c1630221c41cfc207df8 (diff) | |
update
| -rwxr-xr-x | scripts/gpx/check_gpx.py | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/scripts/gpx/check_gpx.py b/scripts/gpx/check_gpx.py new file mode 100755 index 0000000..f1e5c2f --- /dev/null +++ b/scripts/gpx/check_gpx.py | |||
| @@ -0,0 +1,87 @@ | |||
| 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, check with the folling urls:') | ||
| 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, args.distance, args.i) | ||
| 83 | |||
| 84 | print(args.distance) | ||
| 85 | |||
| 86 | if __name__ == '__main__': | ||
| 87 | main(sys.argv) | ||