File indexing completed on 2024-05-26 04:46:06

0001 #!/usr/bin/env python3
0002 
0003 # SPDX-FileCopyrightText: 2024 Jonah BrĂ¼chert <jbb@kaidan.im>
0004 # SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 import json
0007 
0008 import sys
0009 
0010 if len(sys.argv) < 3:
0011     print("Usage: ./process_geojson.py input.geojson country_network.json")
0012     sys.exit(1)
0013 
0014 geojson_path = sys.argv[1]
0015 output_path = sys.argv[2]
0016 
0017 geojson = json.load(open(geojson_path, "r"))
0018 if geojson["type"] != "FeatureCollection":
0019     print("Error: Unsupported GeoJSON type, this script only understands FeatureCollection")
0020     sys.exit(1)
0021 
0022 timestamp = geojson["timestamp"]
0023 features = geojson["features"]
0024 
0025 stations = []
0026 
0027 for feature in features:
0028     if feature["type"] != "Feature":
0029         print("Error: Unsupported feature type, this script only understands Feature")
0030         sys.exit(1)
0031 
0032     if feature["geometry"]["type"] != "Point":
0033         print("Error: Unsupported geometry, this script only understands Point. Make sure not to use geo in your Overpass Turbo query")
0034         sys.exit(1)
0035 
0036     properties = feature["properties"]
0037     name_keys = filter(lambda k: k.startswith("name") or k.startswith("alt_name") or k.startswith("int_name"), properties.keys())
0038     station = {k: properties[k] for k in name_keys}
0039     station["longitude"] = feature["geometry"]["coordinates"][1]
0040     station["latitude"] = feature["geometry"]["coordinates"][0]
0041     stations.append(station)
0042 
0043 station_file = open(output_path, "w")
0044 json.dump(stations, station_file, ensure_ascii=False)