From 0cba6aa9304c33c8f0aea72e818ed6bf1b015664 Mon Sep 17 00:00:00 2001 From: Andreas Gemsa Date: Tue, 21 May 2024 21:26:24 +0200 Subject: [PATCH] improved run.py --- run.py | 61 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/run.py b/run.py index a45a546..c9d5105 100644 --- a/run.py +++ b/run.py @@ -4,28 +4,28 @@ import sys import configuration as config -import osm.read_osm -import osm.sanitize_input -import output.write_graph as output - +from osm import read_osm, sanitize_input +from output import write_graph as output from graph import contract_graph, convert_graph, algorithms, graphfactory from utils import timer - @timer.timer def convert_osm_to_roadgraph(filename, network_type, options): configuration = config.Configuration(network_type) - r_index = filename.rfind(".") - out_file = filename[:r_index] + out_file, _ = os.path.splitext(filename) print(f"selected network type: {configuration.network_type}") print(f"accepted highway tags: {configuration.accepted_highways}") print(f"opening file: {filename}") - nodes, ways = osm.read_osm.read_file(filename, configuration) + try: + nodes, ways = read_osm.read_file(filename, configuration) + except Exception as e: + print(f"Error occurred while reading file {filename}: {e}") + return - osm.sanitize_input.sanitize_input(ways, nodes) + sanitize_input.sanitize_input(ways, nodes) graph = graphfactory.build_graph_from_osm(nodes, ways) @@ -35,6 +35,7 @@ def convert_osm_to_roadgraph(filename, network_type, options): output.write_to_file(graph, out_file, configuration.get_file_extension()) if options.networkx_output: + validate_networkx() nx_graph = convert_graph.convert_to_networkx(graph) output.write_nx_to_file(nx_graph, f"{out_file}.json") @@ -47,6 +48,10 @@ def convert_osm_to_roadgraph(filename, network_type, options): nx_graph = convert_graph.convert_to_networkx(contracted_graph) output.write_nx_to_file(nx_graph, f"{out_file}_contracted.json") +def validate_networkx(): + networkx_spec = importlib.util.find_spec("networkx") + if networkx_spec is None: + raise ImportError("Networkx library not found. Please install networkx if you want to use the --networkx option.") if __name__ == "__main__": parser = argparse.ArgumentParser(description="OSMtoRoadGraph") @@ -78,25 +83,19 @@ def convert_osm_to_roadgraph(filename, network_type, options): parser.print_help() sys.exit() - if not os.path.isfile(filename): - print(f"ERROR: provided filename {filename} does not point to a file!") - sys.exit() - - long_network_type = {"p": "pedestrian", "c": "car", "b": "bicycle"} - if options.network_type in long_network_type: - network_type = long_network_type[options.network_type] - elif options.network_type == long_network_type.values(): - network_type = options.network_type - else: - print("network type improperly set") - sys.exit() - - if options.networkx_output: - networkx_spec = importlib.util.find_spec("networkx") - if networkx_spec is None: - print( - "Error: networkx Library not found. Please install networkx if you want to use the --networkx option." - ) - sys.exit(-1) - - convert_osm_to_roadgraph(filename, network_type, options) + try: + if not os.path.isfile(filename): + raise FileNotFoundError(f"Provided filename {filename} does not point to a file!") + network_type = config.Configuration.validate_network_type(options.network_type) + except FileNotFoundError as e: + print(f"ERROR: {e}") + sys.exit(1) + except ValueError as e: + print(f"ERROR: {e}") + sys.exit(1) + + try: + convert_osm_to_roadgraph(filename, network_type, options) + except Exception as e: + print(f"ERROR: {e}") + sys.exit(1)