]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - scripts/wrgeojson_to_wrgeojson.py
VAO is missing important streets in Switzerland.
[philipp/winterrodeln/wrpylib.git] / scripts / wrgeojson_to_wrgeojson.py
1 #!/usr/bin/python3
2 import argparse
3 import sys
4 from typing import TextIO
5
6 import geojson
7
8 from wrpylib.argparse_tools import bool_type
9 from wrpylib.wrgeojson import join_wrgeojson_ways, simplify_ways
10
11
12 def geojson_to_geojson(input: TextIO, output: TextIO, join_ways: bool, simplify: bool):
13     """'Converts' geojson to geojson
14
15     :param input: input geojson file
16     :param output: output geojson file
17     :param join_ways: joins subsequent ways of same type
18     :param simplify: true to simplify ways
19     """
20     wr_geojson = geojson.load(input)
21     if not wr_geojson.is_valid:
22         raise ValueError(f'Invalid GeoJSON: {wr_geojson.errors()}')
23
24     # Merge sledrun ways of the same type
25     if join_ways:
26         join_wrgeojson_ways(wr_geojson)
27
28     # Simplify the sledrunMap
29     if simplify:
30         simplify_ways(wr_geojson)
31
32     # Print result
33     geojson.dump(wr_geojson, output, ensure_ascii=False, indent=4)
34
35
36 def main():
37     parser = argparse.ArgumentParser(description='Converts Winterrodeln wrgeojson format to itself.'
38                                      'Reads from stdin, writes to stdout.')
39     parser.add_argument('--join-ways', type=bool_type, default=True,
40                         help='joins subsequent ways of same type (default: True)')
41     parser.add_argument('--simplify', type=bool_type, default=False,
42                         help='simplify the map (reduce number of points in paths) (default: False)')
43     args = parser.parse_args()
44
45     geojson_to_geojson(sys.stdin, sys.stdout, args.join_ways, args.simplify)
46
47
48 if __name__ == '__main__':
49     main()