Skip to content

Commit f2c151e

Browse files
authored
Enhance plots in get_osm_data script (#266)
* Enhance plots in `get_osm_data` script * Last fixes
1 parent 0a921de commit f2c151e

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

utils/get_osm_data.py

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
import ast
1717
import logging
1818
from pathlib import Path
19+
from matplotlib import pyplot as plt
1920
import osmnx as ox
21+
from tqdm import tqdm
2022

21-
__version__ = "2025.2.12"
23+
__version__ = "2025.2.13"
2224

23-
RGBA_RED = (1, 0, 0, 1)
25+
RGBA_RED = (1, 0, 0, 0.3)
2426
RGBA_WHITE = (1, 1, 1, 1)
27+
RGBA_GRAY = (0.5, 0.5, 0.5, 1)
28+
RGBA_YELLOW = (1, 1, 0, 1)
2529

2630
FLAGS_MOTORWAY = ["motorway", "motorway_link"]
2731
FLAGS_NORMAL = [
@@ -128,18 +132,21 @@
128132
else:
129133
CUSTOM_FILTER = f"[\"highway\"~\"{'|'.join(FLAGS)}\"]"
130134
logging.info("Custom filter: %s", CUSTOM_FILTER)
131-
GRAPH = ox.graph_from_place(parser.place, network_type="drive")
135+
FULL_GRAPH = ox.graph_from_place(parser.place, network_type="drive")
136+
fig, ax = plt.subplots(figsize=(24, 24))
137+
fig.patch.set_facecolor("black")
132138
ox.plot_graph(
133-
GRAPH,
139+
FULL_GRAPH,
134140
show=False,
135141
close=True,
136142
save=True,
143+
ax=ax,
137144
filepath=Path(parser.output_folder) / "original.png",
138145
)
139146
logging.info(
140-
"Original network has %d nodes and %d edges.",
141-
len(GRAPH.nodes),
142-
len(GRAPH.edges),
147+
"Full network has %d nodes and %d edges.",
148+
len(FULL_GRAPH.nodes),
149+
len(FULL_GRAPH.edges),
143150
)
144151
if FLAGS is not None:
145152
GRAPH = ox.graph_from_place(
@@ -150,6 +157,9 @@
150157
len(GRAPH.nodes),
151158
len(GRAPH.edges),
152159
)
160+
else:
161+
GRAPH = FULL_GRAPH
162+
FULL_GRAPH = None
153163
if parser.consolidate_intersections:
154164
GRAPH = ox.consolidate_intersections(
155165
ox.project_graph(GRAPH), tolerance=parser.tolerance
@@ -160,13 +170,50 @@
160170
len(GRAPH.edges),
161171
)
162172
# plot graph on a 16x9 figure and save into file
173+
fig, ax = plt.subplots(figsize=(24, 24))
174+
fig.patch.set_facecolor("black")
163175
ox.plot_graph(
164176
GRAPH,
165177
show=False,
166178
close=True,
167179
save=True,
180+
ax=ax,
168181
filepath=Path(parser.output_folder) / "final.png",
169182
)
183+
if FULL_GRAPH is not None:
184+
edge_colors = [
185+
(
186+
RGBA_GRAY
187+
if any(
188+
"geometry" in GRAPH.edges[g_edge]
189+
and "geometry" in FULL_GRAPH.edges[full_edge]
190+
and GRAPH.edges[g_edge]["geometry"].contains(
191+
FULL_GRAPH.edges[full_edge]["geometry"]
192+
)
193+
for g_edge in GRAPH.edges
194+
)
195+
else RGBA_RED
196+
)
197+
for full_edge in tqdm(FULL_GRAPH.edges)
198+
]
199+
200+
node_colors = [
201+
RGBA_RED if node not in GRAPH.nodes else RGBA_WHITE
202+
for node in FULL_GRAPH.nodes
203+
]
204+
fig, ax = plt.subplots(figsize=(24, 24))
205+
fig.patch.set_facecolor("black")
206+
ox.plot_graph(
207+
FULL_GRAPH,
208+
show=False,
209+
close=True,
210+
save=True,
211+
ax=ax,
212+
edge_color=edge_colors,
213+
node_color=node_colors,
214+
filepath=Path(parser.output_folder) / "comparison.png",
215+
)
216+
170217
gdf_nodes, gdf_edges = ox.graph_to_gdfs(ox.project_graph(GRAPH, to_latlong=True))
171218
# notice that osmnid is the index of the gdf_nodes DataFrame, so take it as a column
172219
gdf_nodes.reset_index(inplace=True)
@@ -227,13 +274,21 @@
227274
)
228275
# Plot the graph with duplicated edges in red
229276
edge_colors = [
230-
RGBA_RED if duplicated_mask.iloc[i] else RGBA_WHITE
277+
RGBA_YELLOW if duplicated_mask.iloc[i] else RGBA_GRAY
231278
for i in range(len(gdf_edges))
232279
]
280+
edge_line_width = [
281+
2 if duplicated_mask.iloc[i] else 0.5 for i in range(len(gdf_edges))
282+
]
283+
fig, ax = plt.subplots(figsize=(24, 24))
284+
fig.patch.set_facecolor("black")
233285
ox.plot_graph(
234286
GRAPH,
235287
edge_color=edge_colors,
288+
edge_linewidth=edge_line_width,
289+
show=False,
236290
save=True,
291+
ax=ax,
237292
filepath=Path(parser.output_folder) / "duplicated_edges.png",
238293
)
239294

0 commit comments

Comments
 (0)