Skip to content

Commit bf8ad2a

Browse files
committed
Add output folder arg to get_osm_data script
1 parent 26072fd commit bf8ad2a

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

utils/get_osm_data.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The place is passed as a command line argument.
44
55
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
77
88
The output files are:
99
- {place}_nodes.csv
@@ -13,11 +13,12 @@
1313
"""
1414

1515
from argparse import ArgumentParser
16+
from pathlib import Path
1617
import ast
1718
import logging
1819
import osmnx as ox
1920

20-
__version__ = "2025.1.24"
21+
__version__ = "2025.1.30"
2122

2223
RGBA_RED = (1, 0, 0, 1)
2324
RGBA_WHITE = (1, 1, 1, 1)
@@ -45,16 +46,19 @@
4546
if __name__ == "__main__":
4647
parser = ArgumentParser("Script to get the OSM data of a place.")
4748
parser.add_argument(
49+
"-p",
4850
"--place",
4951
required=True,
5052
help="Place to get the OSM data in the format: city, province, country",
5153
)
5254
parser.add_argument(
55+
"-em",
5356
"--exclude-motorway",
5457
action="store_true",
5558
help="Exclude motorways from the data. Default is False",
5659
)
5760
parser.add_argument(
61+
"-er",
5862
"--exclude-residential",
5963
action="store_true",
6064
help="Exclude residential roads from the data. Default is False",
@@ -74,6 +78,13 @@
7478
parser.add_argument(
7579
"--use-original-ids", action="store_true", help="Use the original ids from OSM."
7680
)
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+
)
7788
parser = parser.parse_args()
7889
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
7990
# set up colored logging
@@ -98,7 +109,13 @@
98109
CUSTOM_FILTER = f"[\"highway\"~\"{'|'.join(FLAGS)}\"]"
99110
logging.info("Custom filter: %s", CUSTOM_FILTER)
100111
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+
)
102119
logging.info(
103120
"Original network has %d nodes and %d edges.",
104121
len(GRAPH.nodes),
@@ -121,7 +138,13 @@
121138
len(GRAPH.edges),
122139
)
123140
# 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+
)
125148
gdf_nodes, gdf_edges = ox.graph_to_gdfs(ox.project_graph(GRAPH, to_latlong=True))
126149
# notice that osmnid is the index of the gdf_nodes DataFrame, so take it as a column
127150
gdf_nodes.reset_index(inplace=True)
@@ -195,7 +218,17 @@
195218

196219
# Save the data
197220
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

Comments
 (0)