Skip to content
Snippets Groups Projects
Verified Commit 9190d399 authored by Marco Aceti's avatar Marco Aceti
Browse files

Add geojson station data extractor

parent 0f9d1cdc
Branches
No related tags found
No related merge requests found
Pipeline #2716 passed
......@@ -65,9 +65,7 @@ s_extractor.add_argument(
s_extractor.add_argument(
"-f",
default="csv",
choices=[
"csv",
],
choices=["csv", "geojson"],
help="output file format",
dest="format",
)
......
requests==2.28.2
python-dateutil==2.8.2
tqdm==4.65.0
geojson==3.0.1
......@@ -3,6 +3,8 @@ import csv
import pickle
from pathlib import Path
from geojson import Feature, FeatureCollection, Point
from src.scraper.station import Station
from src.utils import parse_input_format_output_args
......@@ -59,11 +61,42 @@ def to_csv(data: dict[str, Station], output_file: Path) -> None:
station.position[1] if station.position else None,
)
)
csvfile.close()
def to_geojson(data: dict[str, Station], output_file: Path) -> None:
feature_list: list[Feature] = list()
for station_c in data:
station: Station = data[station_c]
if not station.position:
continue
feature: Feature = Feature(
geometry=Point((station.position[1], station.position[0])),
properties={
"code": station.code,
"name": station.name,
"short_name": station.short_name
if hasattr(station, "short_name")
else None,
"region": station.region_code,
},
)
feature_list.append(feature)
collection: FeatureCollection = FeatureCollection(feature_list)
with open(output_file, "w+") as f:
f.write(str(collection))
def main(args: argparse.Namespace):
input_f, output_f, format = parse_input_format_output_args(args)
data: dict[str, Station] = load_file(input_f)
if format == "csv":
to_csv(data, output_f)
if format == "geojson":
to_geojson(data, output_f)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment