summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2021-03-27 14:56:16 +0100
committerDaniel Friesel <derf@finalrewind.org>2021-03-27 14:56:16 +0100
commit08ba0611c2c6a0e022a9fd0aff6399c7b957a9e7 (patch)
tree8a02813242ad56eb3f2898a01c747e42ae7aec34 /bin
parent2bd30e325041ad7ec5afe7044e559070616621e9 (diff)
switch to geolocation-based station matching
Diffstat (limited to 'bin')
-rwxr-xr-xbin/efa-gw17
-rwxr-xr-xbin/merge-haltestellen-and-iris72
2 files changed, 76 insertions, 13 deletions
diff --git a/bin/efa-gw b/bin/efa-gw
index fc6e949..4d92e2c 100755
--- a/bin/efa-gw
+++ b/bin/efa-gw
@@ -6,7 +6,6 @@ import argparse
import aiohttp
from aiohttp import web
-import csv
import json
headers = {
@@ -19,17 +18,6 @@ occupancy_map = {"MANY_SEATS": 1, "FEW_SEATS": 2, "STANDING_ONLY": 3}
eva_to_name = dict()
-def load_eva_to_name(filename):
- ret = dict()
- with open(filename, "r") as f:
- f.readline()
- cr = csv.reader(f, delimiter=";")
- for city, place, _, _, _, _, _, name, _, eva, transports in cr:
- if "TRAIN" in transports:
- ret[int(eva)] = name
- return ret
-
-
def get_occupancy(occupancy):
try:
return occupancy_map[occupancy]
@@ -75,7 +63,10 @@ if __name__ == "__main__":
parser.add_argument("--prefix", type=str, metavar="PATH", default="/")
args = parser.parse_args()
- eva_to_name = load_eva_to_name(args.eva_csv)
+ with open("share/vrr.json", "r") as f:
+ for eva, (city, stop, name) in json.load(f).items():
+ eva_to_name[int(eva)] = name
+
app = web.Application()
app.add_routes([web.get(f"{args.prefix}{{eva}}.json", handle_eva)])
web.run_app(app, host="localhost", port=args.port)
diff --git a/bin/merge-haltestellen-and-iris b/bin/merge-haltestellen-and-iris
new file mode 100755
index 0000000..4cc64ca
--- /dev/null
+++ b/bin/merge-haltestellen-and-iris
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+# vim:tabstop=4 softtabstop=4 shiftwidth=4 textwidth=160 smarttab expandtab colorcolumn=160
+
+import csv
+import json
+
+from geopy.distance import distance
+from progress.bar import Bar
+
+
+class ProgressBar(Bar):
+ suffix = "%(percent).0f%% [%(elapsed_td)s/%(eta_td)s]"
+
+
+vrr_stops = list()
+
+with open("haltestellenliste.csv", "r", encoding="iso-8859-1") as f:
+ f.readline()
+ cr = csv.reader(f, delimiter=";")
+ for city, place, _, _, _, lon, lat, stop, _, _ in cr:
+ lon = lon[:5] + lon[6:]
+ lat = lat[:6] + lat[7:]
+ try:
+ vrr_stops.append(
+ (stop.strip(), float(lat), float(lon), city.strip(), place.strip())
+ )
+ except ValueError:
+ # invalid entry
+ pass
+
+with open("/home/derf/var/code/Travel-Status-DE-IRIS/share/stations.json", "r") as f:
+ iris_stops = json.load(f)
+
+output = dict()
+
+for iris_stop in ProgressBar(max=len(iris_stops)).iter(iris_stops):
+ eva = iris_stop["eva"]
+ iris_name = iris_stop["name"]
+ iris_lat, iris_lon = iris_stop["latlong"]
+
+ for stop, lat, lon, city, place in vrr_stops:
+ if stop == iris_name:
+ output[eva] = city, place, stop
+ break
+
+ if eva in output:
+ continue
+
+ candidates = list()
+ for stop, lat, lon, city, place in vrr_stops:
+ if abs(lat - iris_lat) < 0.01 and abs(lon - iris_lon) < 0.01:
+ candidates.append(
+ (
+ stop,
+ lat,
+ lon,
+ city,
+ place,
+ distance((lat, lon), (iris_lat, iris_lon)).m,
+ )
+ )
+
+ if not candidates:
+ continue
+
+ candidates.sort(key=lambda x: x[5])
+
+ stop, _, _, city, place, _ = candidates[0]
+ output[eva] = city, place, stop
+
+with open("share/vrr.json", "w", encoding="utf-8") as f:
+ json.dump(output, f, ensure_ascii=False)