summaryrefslogtreecommitdiff
path: root/bin/merge-haltestellen-and-iris
blob: 775e4605b310ff848c551dc155ea3d9cce286970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# vim:tabstop=4 softtabstop=4 shiftwidth=4 textwidth=160 smarttab expandtab colorcolumn=160
#
# Copyright (C) 2021 Birte Kristina Friesel
#
# SPDX-License-Identifier: BSD-2-Clause


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("stop.csv", "r", encoding="iso-8859-1") as f:
    f.readline()
    cr = csv.reader(f, delimiter=";")
    for field in cr:
        stop = field[3]
        lon = field[6]
        lat = field[7]
        optid = field[16]
        try:
            vrr_stops.append((stop.strip(), float(lat), float(lon), optid.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, optid in vrr_stops:
        if stop == iris_name:
            output[eva] = stop, optid
            break

    if eva in output:
        continue

    candidates = list()
    for stop, lat, lon, optid in vrr_stops:
        if abs(lat - iris_lat) < 0.01 and abs(lon - iris_lon) < 0.01:
            candidates.append(
                (
                    (stop, lat, lon, optid),
                    distance((lat, lon), (iris_lat, iris_lon)).m,
                )
            )

    if not candidates:
        continue

    candidates.sort(key=lambda x: x[1])

    stop, lat, lon, optid = candidates[0][0]
    output[eva] = stop, optid

with open("share/vrr.json", "w", encoding="utf-8") as f:
    json.dump(output, f, ensure_ascii=False)