2 """This file can be used as command line script or as module.
4 usage: osm_to_wrmap.py [-h] [--single SINGLE] [--join-ways JOIN_WAYS]
8 Converts an OpenStreetMap .osm file to Winterrodeln <wrmap>(s).
11 osmfile input file name (.osm file)
14 -h, --help show this help message and exit
15 --single SINGLE just display the ith (1 based) <wrmap> instead of all
17 joins subsequent ways of same type (default: True)
18 --simplify SIMPLIFY simplify the map (reduce number of points in paths)
21 Writing result to a file without encoding errors:
22 $ PYTHONIOENCODING='ISO8859-15' python osm_to_wrmap.py mymap.osm > mymap.wiki
25 from typing import Optional
26 from xml.etree.ElementTree import ElementTree
27 import wrpylib.wrmwmarkup
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
34 def process_osm_file(filename: str, single: Optional[int], join_ways: bool, simplify: bool):
35 """converts an .osm file to <wrmap>(s).
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.
42 osm_tree = ElementTree()
43 osm_tree.parse(filename)
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]]
50 for sledrun_relation in sledrun_list: # sledrun is an Element 'relation'
52 sledrun_tags = tags(sledrun_relation)
53 title = f"{sledrun_tags.get('name')} ({sledrun_tags.get('website')}) [{sledrun_relation.get('id')}]"
55 print("-" * len(title))
59 sledrun_geojson = convert_osm_to_geojson(osm_tree, sledrun_relation)
61 print('Error: Incomplete XML - please load larger region.')
64 # Merge sledrun ways of the same type
66 join_wrgeojson_ways(sledrun_geojson)
68 # Simplify the sledrunMap
69 if simplify is not None:
70 simplify_ways(sledrun_geojson)
72 # Print the <wrmap> tag for MediaWiki
73 print(wrpylib.wrmwmarkup.create_wrmap(sledrun_geojson))
74 print('{{Landkarte Legende}}<br/>{{GoogleMaps Hinweis}}')
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()
91 # actually do the conversion
92 process_osm_file(args.osmfile, single=args.single, join_ways=args.join_ways, simplify=args.simplify)
95 if __name__ == '__main__':