forked from skandium/cyclorank
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw_maps_cli.py
106 lines (83 loc) · 3.79 KB
/
draw_maps_cli.py
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
100
101
102
103
104
105
106
import argparse
import json
import logging
import os.path
import pickle
import matplotlib.pyplot as plt
import tqdm
from pyrosm import OSM
logger = logging.getLogger(__name__)
def main(experiment_name: str, city_mappings: dict, output_path: str):
exp_date = experiment_name.split("/")[-2] if experiment_name.endswith("/") else experiment_name.split("/")[-1]
for country_map in city_mappings:
for city in tqdm.tqdm(city_mappings[country_map]):
osm_city = list(city.keys())[0]
logger.info(f"Working on {osm_city}")
# skip if the file does exist
if os.path.exists(f"{output_path}/{osm_city}.html"):
logger.info(f"Skipping {osm_city}")
continue
filepath = f'{experiment_name}/extracted_maps/{osm_city}.pbf'
if not os.path.exists(filepath):
logger.error(f"File {filepath} does not exist")
continue
try:
osm = OSM(filepath)
except Exception as e:
logger.error(e)
continue
nodes_driving, edges_driving = osm.get_network(nodes=True, network_type="cycling")
nodes_all, edges_all = osm.get_network(nodes=True, network_type="all")
drive_net = osm.get_network(network_type="cycling")
with open(f"{experiment_name}/results/{osm_city}_way_ids.pkl", "rb") as f:
way_ids = pickle.load(f)
try:
subset = drive_net[drive_net["id"].isin(list(way_ids.keys()))]
lanes = subset[subset["highway"] != "cycleway"]
segregated_lanes = subset[subset["highway"] != "cycleway"]
except Exception as e:
logger.error(e)
continue
try:
m = subset.explore()
m.save(f"{output_path}/{osm_city}.html")
except Exception as e:
logger.error(e)
try:
m = lanes.explore()
m.save(f"{output_path}/{osm_city}_lanes.html")
except Exception as e:
logger.error(e)
try: # just because of Rumburk with no lanes
_ = subset.plot(figsize=(15, 15), column="highway", legend=True, linewidth=5)
plt.title(f"{osm_city} - all ways, {exp_date}")
plt.savefig(f'{output_path}/{osm_city}.png', dpi=300)
plt.close()
except Exception as e:
logger.error(e)
# TODO: fix this
continue
# Convert the network to a NetworkX graph
graph = nx.MultiDiGraph()
graph.add_edges_from(subset[["u", "v", "key"]].to_records(index=False))
# Calculate the degree distribution of the graph
degree_sequence = sorted([d for n, d in graph.degree()], reverse=True)
degree_count = nx.degree_histogram(graph)
# Plot the degree distribution
fig, ax = plt.subplots()
ax.plot(degree_sequence, degree_count, "ro-")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel("Degree")
ax.set_ylabel("Count")
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--experiment_name", type=str, default="exp")
parser.add_argument("--output_path", type=str, default="exp")
parser.add_argument("--config_path", type=str, default="config/city_conf_czechia.json")
parser.add_argument("--log_level", type=str, default="INFO")
args = parser.parse_args()
logging.basicConfig(level=args.log_level, format="%(asctime)s %(levelname)s %(message)s")
city_mappings = json.load(open(args.config_path))
main(args.experiment_name, city_mappings, args.output_path)