Skip to content

Commit

Permalink
improved run.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AndGem committed May 21, 2024
1 parent 60016cb commit 0cba6aa
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")

Expand All @@ -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")
Expand Down Expand Up @@ -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)

0 comments on commit 0cba6aa

Please sign in to comment.