]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - scripts/osm_to_wrmap.py
VAO is missing important streets in Switzerland.
[philipp/winterrodeln/wrpylib.git] / scripts / osm_to_wrmap.py
1 #!/usr/bin/python3
2 """This file can be used as command line script or as module.
3
4 usage: osm_to_wrmap.py [-h] [--single SINGLE] [--join-ways JOIN_WAYS]
5                        [--simplify SIMPLIFY]
6                        osmfile
7
8 Converts an OpenStreetMap .osm file to Winterrodeln <wrmap>(s).
9
10 positional arguments:
11   osmfile               input file name (.osm file)
12
13 optional arguments:
14   -h, --help            show this help message and exit
15   --single SINGLE       just display the ith (1 based) <wrmap> instead of all
16   --join-ways JOIN_WAYS
17                         joins subsequent ways of same type (default: True)
18   --simplify SIMPLIFY   simplify the map (reduce number of points in paths)
19                         (default: True)
20
21 Writing result to a file without encoding errors:
22 $ PYTHONIOENCODING='ISO8859-15' python osm_to_wrmap.py mymap.osm > mymap.wiki
23 """
24 import argparse
25 from typing import Optional
26 from xml.etree.ElementTree import ElementTree
27 import wrpylib.wrmwmarkup
28
29 from wrpylib.argparse_tools import bool_type
30 from wrpylib.wrgeojson import join_wrgeojson_ways, simplify_ways
31 from wrpylib.wrosm import find_sledrun_relations, tags, convert_osm_to_geojson, DeRefError
32
33
34 def process_osm_file(filename: str, single: Optional[int], join_ways: bool, simplify: bool):
35     """converts an .osm file to <wrmap>(s).
36
37     :param filename: name of the .osm file
38     :param single: if not None, an int argument that specifies the number of the <wrmap> to display (1 based)
39     :param join_ways: joins subsequent ways of same type
40     :param simplify: If true, omit points to make the map file size smaller.
41     """
42     osm_tree = ElementTree()
43     osm_tree.parse(filename)
44
45     # Search all sled run relations
46     sledrun_list = list(find_sledrun_relations(osm_tree))
47     if single is not None:
48         sledrun_list = [sledrun_list[single - 1]]
49
50     for sledrun_relation in sledrun_list:  # sledrun is an Element 'relation'
51         # Make a nice title
52         sledrun_tags = tags(sledrun_relation)
53         title = f"{sledrun_tags.get('name')} ({sledrun_tags.get('website')}) [{sledrun_relation.get('id')}]"
54         print(title)
55         print("-" * len(title))
56
57         # Convert to geojson
58         try:
59             sledrun_geojson = convert_osm_to_geojson(osm_tree, sledrun_relation)
60         except DeRefError:
61             print('Error: Incomplete XML - please load larger region.')
62             continue
63
64         # Merge sledrun ways of the same type
65         if join_ways:
66             join_wrgeojson_ways(sledrun_geojson)
67
68         # Simplify the sledrunMap
69         if simplify is not None:
70             simplify_ways(sledrun_geojson)
71
72         # Print the <wrmap> tag for MediaWiki
73         print(wrpylib.wrmwmarkup.create_wrmap(sledrun_geojson))
74         print('{{Landkarte Legende}}<br/>{{GoogleMaps Hinweis}}')
75         print()
76
77
78 def main():
79     epilog = "Writing result to a file without encoding errors:\n" + \
80              "$ PYTHONIOENCODING='ISO8859-15' python osm_to_wrmap.py mymap.osm > mymap.wiki"
81     parser = argparse.ArgumentParser(description='Converts an OpenStreetMap .osm file to Winterrodeln <wrmap>(s).',
82                                      epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
83     parser.add_argument('--single', type=int, help='just display the ith (1 based) <wrmap> instead of all')
84     parser.add_argument('--join-ways', type=bool_type, default=True,
85                         help='joins subsequent ways of same type (default: True)')
86     parser.add_argument('--simplify', type=bool_type, default=True,
87                         help='simplify the map (reduce number of points in paths) (default: True)')
88     parser.add_argument('osmfile', type=str, help='input file name (.osm file)')
89     args = parser.parse_args()
90
91     # actually do the conversion
92     process_osm_file(args.osmfile, single=args.single, join_ways=args.join_ways, simplify=args.simplify)
93
94
95 if __name__ == '__main__':
96     main()