|
3 | 3 | The place is passed as a command line argument. |
4 | 4 |
|
5 | 5 | Example: |
6 | | -python get_osm_data.py --place "Bologna, Emilia-Romagna, Italy" --exclude-residential |
| 6 | +python get_osm_data.py -p "Bologna, Emilia-Romagna, Italy" -er |
7 | 7 |
|
8 | 8 | The output files are: |
9 | 9 | - {place}_nodes.csv |
|
13 | 13 | """ |
14 | 14 |
|
15 | 15 | from argparse import ArgumentParser |
| 16 | +from pathlib import Path |
16 | 17 | import ast |
17 | 18 | import logging |
18 | 19 | import osmnx as ox |
19 | 20 |
|
20 | | -__version__ = "2025.1.24" |
| 21 | +__version__ = "2025.1.30" |
21 | 22 |
|
22 | 23 | RGBA_RED = (1, 0, 0, 1) |
23 | 24 | RGBA_WHITE = (1, 1, 1, 1) |
|
45 | 46 | if __name__ == "__main__": |
46 | 47 | parser = ArgumentParser("Script to get the OSM data of a place.") |
47 | 48 | parser.add_argument( |
| 49 | + "-p", |
48 | 50 | "--place", |
49 | 51 | required=True, |
50 | 52 | help="Place to get the OSM data in the format: city, province, country", |
51 | 53 | ) |
52 | 54 | parser.add_argument( |
| 55 | + "-em", |
53 | 56 | "--exclude-motorway", |
54 | 57 | action="store_true", |
55 | 58 | help="Exclude motorways from the data. Default is False", |
56 | 59 | ) |
57 | 60 | parser.add_argument( |
| 61 | + "-er", |
58 | 62 | "--exclude-residential", |
59 | 63 | action="store_true", |
60 | 64 | help="Exclude residential roads from the data. Default is False", |
|
74 | 78 | parser.add_argument( |
75 | 79 | "--use-original-ids", action="store_true", help="Use the original ids from OSM." |
76 | 80 | ) |
| 81 | + parser.add_argument( |
| 82 | + "-of", |
| 83 | + "--output-folder", |
| 84 | + type=str, |
| 85 | + default=".", |
| 86 | + help="Folder where the output files will be saved. Default is the current folder.", |
| 87 | + ) |
77 | 88 | parser = parser.parse_args() |
78 | 89 | logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") |
79 | 90 | # set up colored logging |
|
98 | 109 | CUSTOM_FILTER = f"[\"highway\"~\"{'|'.join(FLAGS)}\"]" |
99 | 110 | logging.info("Custom filter: %s", CUSTOM_FILTER) |
100 | 111 | GRAPH = ox.graph_from_place(parser.place, network_type="drive") |
101 | | - ox.plot_graph(GRAPH, show=False, close=True, save=True, filepath="./original.png") |
| 112 | + ox.plot_graph( |
| 113 | + GRAPH, |
| 114 | + show=False, |
| 115 | + close=True, |
| 116 | + save=True, |
| 117 | + filepath=Path(parser.output_folder) / "original.png", |
| 118 | + ) |
102 | 119 | logging.info( |
103 | 120 | "Original network has %d nodes and %d edges.", |
104 | 121 | len(GRAPH.nodes), |
|
121 | 138 | len(GRAPH.edges), |
122 | 139 | ) |
123 | 140 | # plot graph on a 16x9 figure and save into file |
124 | | - ox.plot_graph(GRAPH, show=False, close=True, save=True, filepath="./final.png") |
| 141 | + ox.plot_graph( |
| 142 | + GRAPH, |
| 143 | + show=False, |
| 144 | + close=True, |
| 145 | + save=True, |
| 146 | + filepath=Path(parser.output_folder) / "final.png", |
| 147 | + ) |
125 | 148 | gdf_nodes, gdf_edges = ox.graph_to_gdfs(ox.project_graph(GRAPH, to_latlong=True)) |
126 | 149 | # notice that osmnid is the index of the gdf_nodes DataFrame, so take it as a column |
127 | 150 | gdf_nodes.reset_index(inplace=True) |
|
195 | 218 |
|
196 | 219 | # Save the data |
197 | 220 | place = parser.place.split(",")[0].strip().lower() |
198 | | - gdf_nodes.to_csv(f"{place}_nodes.csv", sep=";", index=False) |
199 | | - logging.info('Nodes correctly saved in "%s_nodes.csv"', place) |
200 | | - gdf_edges.to_csv(f"{place}_edges.csv", sep=";", index=False) |
201 | | - logging.info('Edges correctly saved in "%s_edges.csv"', place) |
| 221 | + gdf_nodes.to_csv( |
| 222 | + Path(parser.output_folder) / f"{place}_nodes.csv", sep=";", index=False |
| 223 | + ) |
| 224 | + logging.info( |
| 225 | + 'Nodes correctly saved in "%s"', |
| 226 | + Path(parser.output_folder) / f"{place}_nodes.csv", |
| 227 | + ) |
| 228 | + gdf_edges.to_csv( |
| 229 | + Path(parser.output_folder) / f"{place}_edges.csv", sep=";", index=False |
| 230 | + ) |
| 231 | + logging.info( |
| 232 | + 'Edges correctly saved in "%s"', |
| 233 | + Path(parser.output_folder) / f"{place}_edges.csv", |
| 234 | + ) |
0 commit comments