#!/usr/bin/python3 """This file can be used as command line script or as module. usage: osm_to_wrmap.py [-h] [--single SINGLE] [--join-ways JOIN_WAYS] [--simplify SIMPLIFY] osmfile Converts an OpenStreetMap .osm file to Winterrodeln (s). positional arguments: osmfile input file name (.osm file) optional arguments: -h, --help show this help message and exit --single SINGLE just display the ith (1 based) instead of all --join-ways JOIN_WAYS joins subsequent ways of same type (default: True) --simplify SIMPLIFY simplify the map (reduce number of points in paths) (default: True) Writing result to a file without encoding errors: $ PYTHONIOENCODING='ISO8859-15' python osm_to_wrmap.py mymap.osm > mymap.wiki """ import argparse from typing import Optional from xml.etree.ElementTree import ElementTree import wrpylib.wrmwmarkup from wrpylib.argparse_tools import bool_type from wrpylib.wrgeojson import join_wrgeojson_ways, simplify_ways from wrpylib.wrosm import find_sledrun_relations, tags, convert_osm_to_geojson, DeRefError def process_osm_file(filename: str, single: Optional[int], join_ways: bool, simplify: bool): """converts an .osm file to (s). :param filename: name of the .osm file :param single: if not None, an int argument that specifies the number of the to display (1 based) :param join_ways: joins subsequent ways of same type :param simplify: If true, omit points to make the map file size smaller. """ osm_tree = ElementTree() osm_tree.parse(filename) # Search all sled run relations sledrun_list = list(find_sledrun_relations(osm_tree)) if single is not None: sledrun_list = [sledrun_list[single - 1]] for sledrun_relation in sledrun_list: # sledrun is an Element 'relation' # Make a nice title sledrun_tags = tags(sledrun_relation) title = f"{sledrun_tags.get('name')} ({sledrun_tags.get('website')}) [{sledrun_relation.get('id')}]" print(title) print("-" * len(title)) # Convert to geojson try: sledrun_geojson = convert_osm_to_geojson(osm_tree, sledrun_relation) except DeRefError: print('Error: Incomplete XML - please load larger region.') continue # Merge sledrun ways of the same type if join_ways: join_wrgeojson_ways(sledrun_geojson) # Simplify the sledrunMap if simplify is not None: simplify_ways(sledrun_geojson) # Print the tag for MediaWiki print(wrpylib.wrmwmarkup.create_wrmap(sledrun_geojson)) print('{{Landkarte Legende}}
{{GoogleMaps Hinweis}}') print() def main(): epilog = "Writing result to a file without encoding errors:\n" + \ "$ PYTHONIOENCODING='ISO8859-15' python osm_to_wrmap.py mymap.osm > mymap.wiki" parser = argparse.ArgumentParser(description='Converts an OpenStreetMap .osm file to Winterrodeln (s).', epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--single', type=int, help='just display the ith (1 based) instead of all') 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=True, help='simplify the map (reduce number of points in paths) (default: True)') parser.add_argument('osmfile', type=str, help='input file name (.osm file)') args = parser.parse_args() # actually do the conversion process_osm_file(args.osmfile, single=args.single, join_ways=args.join_ways, simplify=args.simplify) if __name__ == '__main__': main()