|
16 | 16 | import ast |
17 | 17 | import logging |
18 | 18 | from pathlib import Path |
| 19 | +from matplotlib import pyplot as plt |
19 | 20 | import osmnx as ox |
| 21 | +from tqdm import tqdm |
20 | 22 |
|
21 | | -__version__ = "2025.2.12" |
| 23 | +__version__ = "2025.2.13" |
22 | 24 |
|
23 | | -RGBA_RED = (1, 0, 0, 1) |
| 25 | +RGBA_RED = (1, 0, 0, 0.3) |
24 | 26 | RGBA_WHITE = (1, 1, 1, 1) |
| 27 | +RGBA_GRAY = (0.5, 0.5, 0.5, 1) |
| 28 | +RGBA_YELLOW = (1, 1, 0, 1) |
25 | 29 |
|
26 | 30 | FLAGS_MOTORWAY = ["motorway", "motorway_link"] |
27 | 31 | FLAGS_NORMAL = [ |
|
128 | 132 | else: |
129 | 133 | CUSTOM_FILTER = f"[\"highway\"~\"{'|'.join(FLAGS)}\"]" |
130 | 134 | 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") |
132 | 138 | ox.plot_graph( |
133 | | - GRAPH, |
| 139 | + FULL_GRAPH, |
134 | 140 | show=False, |
135 | 141 | close=True, |
136 | 142 | save=True, |
| 143 | + ax=ax, |
137 | 144 | filepath=Path(parser.output_folder) / "original.png", |
138 | 145 | ) |
139 | 146 | 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), |
143 | 150 | ) |
144 | 151 | if FLAGS is not None: |
145 | 152 | GRAPH = ox.graph_from_place( |
|
150 | 157 | len(GRAPH.nodes), |
151 | 158 | len(GRAPH.edges), |
152 | 159 | ) |
| 160 | + else: |
| 161 | + GRAPH = FULL_GRAPH |
| 162 | + FULL_GRAPH = None |
153 | 163 | if parser.consolidate_intersections: |
154 | 164 | GRAPH = ox.consolidate_intersections( |
155 | 165 | ox.project_graph(GRAPH), tolerance=parser.tolerance |
|
160 | 170 | len(GRAPH.edges), |
161 | 171 | ) |
162 | 172 | # plot graph on a 16x9 figure and save into file |
| 173 | + fig, ax = plt.subplots(figsize=(24, 24)) |
| 174 | + fig.patch.set_facecolor("black") |
163 | 175 | ox.plot_graph( |
164 | 176 | GRAPH, |
165 | 177 | show=False, |
166 | 178 | close=True, |
167 | 179 | save=True, |
| 180 | + ax=ax, |
168 | 181 | filepath=Path(parser.output_folder) / "final.png", |
169 | 182 | ) |
| 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 | + |
170 | 217 | gdf_nodes, gdf_edges = ox.graph_to_gdfs(ox.project_graph(GRAPH, to_latlong=True)) |
171 | 218 | # notice that osmnid is the index of the gdf_nodes DataFrame, so take it as a column |
172 | 219 | gdf_nodes.reset_index(inplace=True) |
|
227 | 274 | ) |
228 | 275 | # Plot the graph with duplicated edges in red |
229 | 276 | edge_colors = [ |
230 | | - RGBA_RED if duplicated_mask.iloc[i] else RGBA_WHITE |
| 277 | + RGBA_YELLOW if duplicated_mask.iloc[i] else RGBA_GRAY |
231 | 278 | for i in range(len(gdf_edges)) |
232 | 279 | ] |
| 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") |
233 | 285 | ox.plot_graph( |
234 | 286 | GRAPH, |
235 | 287 | edge_color=edge_colors, |
| 288 | + edge_linewidth=edge_line_width, |
| 289 | + show=False, |
236 | 290 | save=True, |
| 291 | + ax=ax, |
237 | 292 | filepath=Path(parser.output_folder) / "duplicated_edges.png", |
238 | 293 | ) |
239 | 294 |
|
|
0 commit comments