-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun.py
More file actions
99 lines (77 loc) · 3.2 KB
/
run.py
File metadata and controls
99 lines (77 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import argparse
import importlib.util
import os
import sys
import configuration as config
from graph import algorithms, contract_graph, convert_graph, graphfactory
from osm import read_osm, sanitize_input
from output import write_graph as output
from utils import timer
@timer.timer
def convert_osm_to_roadgraph(filename, network_type, options):
configuration = config.Configuration(network_type)
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}")
try:
nodes, ways = read_osm.read_file(filename, configuration)
except Exception as e:
print(f"Error occurred while reading file {filename}: {e}")
return
sanitize_input.sanitize_input(ways, nodes)
graph = graphfactory.build_graph_from_osm(nodes, ways)
if not options.lcc:
graph = algorithms.computeLCCGraph(graph)
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")
if options.contract:
contracted_graph = contract_graph.ContractGraph(graph).contract()
output.write_to_file(contracted_graph, out_file, f"{configuration.get_file_extension()}c")
if options.networkx_output:
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")
parser.add_argument("-f", "--file", action="store", type=str, dest="filename")
parser.add_argument(
"-n",
"--networkType",
dest="network_type",
action="store",
default="p",
choices=["p", "b", "c"],
help="(p)edestrian, (b)icycle, (c)ar, [default: p]",
)
parser.add_argument("-l", "--nolcc", dest="lcc", action="store_true", default=False)
parser.add_argument("-c", "--contract", dest="contract", action="store_true")
parser.add_argument(
"--networkx",
dest="networkx_output",
action="store_true",
help="enable additional output of JSON format of networkx [note networkx needs to be installed for this to work].",
default=False,
)
options = parser.parse_args()
filename = options.filename
if filename is None:
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(1)
network_type = {"p": "pedestrian", "c": "car", "b": "bicycle"}[options.network_type]
try:
convert_osm_to_roadgraph(filename, network_type, options)
except Exception as e:
print(f"ERROR: {e}")
sys.exit(1)