]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - scripts/wrgeojson_to_wrmap.py
Rename files so that the geojson based Winterrodeln map format is called wrgeojson.
[philipp/winterrodeln/wrpylib.git] / scripts / wrgeojson_to_wrmap.py
diff --git a/scripts/wrgeojson_to_wrmap.py b/scripts/wrgeojson_to_wrmap.py
new file mode 100644 (file)
index 0000000..8631395
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/python3
+import argparse
+import sys
+from typing import TextIO
+
+import geojson
+
+import wrpylib.wrmwmarkup
+from wrpylib.argparse_tools import bool_type
+from wrpylib.wrgeojson import join_sledrun_map_ways, simplify_ways
+
+
+def geojson_to_geojson(input: TextIO, output: TextIO, join_ways: bool, simplify: bool):
+    """Converts geojson to wrmap
+
+    :param input: input geojson file
+    :param output: output geojson file
+    :param join_ways: joins subsequent ways of same type
+    :param simplify: true to simplify ways
+    """
+    wr_geojson = geojson.load(input)
+    if not wr_geojson.is_valid:
+        raise ValueError(f'Invalid GeoJSON: {wr_geojson.errors()}')
+
+    # Merge sledrun ways of the same type
+    if join_ways:
+        join_sledrun_map_ways(wr_geojson)
+
+    # Simplify the sledrunMap
+    if simplify:
+        simplify_ways(wr_geojson)
+
+    # Print result
+    print(wrpylib.wrmwmarkup.create_wrmap(wr_geojson), file=output)
+    print('{{Landkarte Legende}}<br/>{{GoogleMaps Hinweis}}')
+    print()
+
+
+def main():
+    parser = argparse.ArgumentParser(description='Converts Winterrodeln geojson format to itself.'
+                                     'Reads from stdin, writes to stdout.')
+    parser.add_argument('--join-ways', type=bool_type, default=True,
+                        help='joins subsequent ways of same type (default: True)')
+    parser.add_argument('--simplify', type=bool_type, default=False,
+                        help='simplify the map (reduce number of points in paths) (default: False)')
+    args = parser.parse_args()
+
+    geojson_to_geojson(sys.stdin, sys.stdout, args.join_ways, args.simplify)
+
+
+if __name__ == '__main__':
+    main()