-
Notifications
You must be signed in to change notification settings - Fork 1
/
stops2gtfs.pl
executable file
·250 lines (204 loc) · 8.39 KB
/
stops2gtfs.pl
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use DBI;
use File::Path qw(make_path);
use Text::ParseWords;
# take care of windows newlines
#$/ = "\n";
my $line;
# --------------------
# CONNECT TO DATABASE
# --------------------
my $db_folder = "build/data";
make_path($db_folder);
my $driver = "SQLite";
my $divadatabase = "$db_folder/divadata.db";
my $divadsn = "DBI:$driver:dbname=$divadatabase";
my $userid = "";
my $password = "";
my $divadbh = DBI->connect($divadsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
# sacrificing security for speed
$divadbh->{AutoCommit} = 0;
$divadbh->do( "COMMIT; PRAGMA synchronous=OFF; BEGIN TRANSACTION" );
print "Opened diva-database successfully\n";
my $database = "$db_folder/diva2gtfs.db";
my $dsn = "DBI:$driver:dbname=$database";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
# sacrificing security for speed
$dbh->{AutoCommit} = 0;
$dbh->do( "COMMIT; PRAGMA synchronous=OFF; BEGIN TRANSACTION" );
print "Opened gtfs-database successfully\n";
# --------------------------
# END OF DATABASE GEDOENS
# --------------------------
# -------------------------------------------
# MAIN METHOD
#--------------------------------------------
# TODO: Add all CRS used by DIVA here
my %crs = (
# GIP1 causes issues when calling cs2cs (exception cause is "projection not named")
# GIP1 => {
# cs2cs_params => '+init=epsg:31259 +to +init:epsg:4326',
## offset => 6000000
# offset => 987402
# },
MVTT => {
cs2cs_params => '+init=epsg:31466 +to +init=epsg:4326',
offset => 6160100
},
NAV2 => {
cs2cs_params => '+init=epsg:31466 +to +init=epsg:4326',
offset => 6160100
},
NAV3 => {
cs2cs_params => '+init=epsg:31467 +to +init=epsg:4326',
offset => 6160100
},
NAV4 => {
cs2cs_params => '+init=epsg:31468 +to +init=epsg:4326',
offset => 6160100
},
NAV5 => {
cs2cs_params => '+init=epsg:31469 +to +init=epsg:4326',
offset => 6160100
},
NBWT => {
cs2cs_params => '+init=epsg:31467 +to +init=epsg:4326',
offset => 6160100
},
STVH => {
cs2cs_params => '+init=epsg:31259 +to +init:epsg:4326',
offset => 1000000
},
VVTT => {
cs2cs_params => '+init=epsg:31257 +to +init=epsg:4326',
offset => 1000000
}
);
my @supported_crs = keys %crs;
my $stop_id = "";
my $stop_name = "";
my $stop_lat = "";
my $stop_lon = "";
my $zone_id = "";
# Parent station handling
print "Parent stations ";
my $insertsth = $dbh->prepare('INSERT OR REPLACE INTO stops VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
my $sth = $divadbh->prepare('SELECT S.hstnr AS stop_id, S.hstname AS stop_name, bzn.hstohne AS stop_name_bzn, group_concat(tz.tzonen, "") AS zone_id, HK.x AS stop_lat, HK.y AS stop_lon, HK.plan AS plan FROM Stop AS S
LEFT OUTER JOIN Stop_bzn AS bzn ON S._AutoKey_ = bzn._FK__AutoKey_
LEFT OUTER JOIN Stop_hst_koord as HK ON S._AutoKey_ = HK._FK__AutoKey_
LEFT OUTER JOIN Stop_tzonen as tz ON S._AutoKey_ = tz._FK__AutoKey_
WHERE S._AutoKey_ IN (SELECT SHS._FK__AutoKey_ FROM Stop_hst_steig AS SHS) AND HK.plan IN (\'' . join('\', \'', @supported_crs) . '\')
GROUP BY stop_id');
$sth->execute();
print "queried...";
while (my $row = $sth->fetchrow_hashref()) {
$stop_id = $row->{stop_id};
if (defined $row->{stop_name} and $row->{stop_name} ne "") { $stop_name = $row->{stop_name}; }
elsif (defined $row->{stop_name_bzn}) { $stop_name = $row->{stop_name_bzn}; }
if (defined $row->{zone_id}) { $zone_id = $row->{zone_id}; }
if (defined $row->{plan} and exists $crs{$row->{plan}} and defined $row->{stop_lat} and defined $row->{stop_lon}) {
$stop_lat = $row->{stop_lat};
$stop_lon = -1 * ($row->{stop_lon} - $crs{$row->{plan}}{offset});
my @coords1=split(/\s+/, `echo $stop_lat $stop_lon | cs2cs -f "%.8f" $crs{$row->{plan}}{cs2cs_params}`);
$stop_lon = $coords1[0];
$stop_lat = $coords1[1];
} else {
$stop_name = $stop_name . "_KOORDFIX!";
$stop_lon = undef;
$stop_lat = undef;
}
$insertsth->execute($stop_id, undef, $stop_name, $stop_lat, $stop_lon, $zone_id, "1", undef, undef);
}
$dbh->commit();
print " and written to GTFS database\n";
# Child station handling
print "Child stations";
$sth = $divadbh->prepare('SELECT S.hstnr AS stop_id, S.hstname AS stop_name, bzn.hstohne AS stop_name_bzn, group_concat(tz.tzonen, "") as zone_id, SHS.steig AS steig, SPK.x AS stop_lat, SPK.y AS stop_lon, SPK.plan AS plan FROM Stop AS S
LEFT OUTER JOIN Stop_bzn AS bzn ON S._AutoKey_ = bzn._FK__AutoKey_
LEFT OUTER JOIN Stop_tzonen as tz ON S._AutoKey_ = tz._FK__AutoKey_
LEFT OUTER JOIN Stop_hst_steig AS SHS on S._AutoKey_ = SHS._FK__AutoKey_
LEFT OUTER JOIN StopPlatformKoord AS SPK ON SHS._AutoKey_ = SPK._FK__AutoKey_
WHERE SHS.steig NOT LIKE "Eing%" AND SPK.plan IN (\'' . join('\', \'', @supported_crs) . '\')
GROUP BY stop_id, steig');
$sth->execute();
print(" queried...");
while (my $row = $sth->fetchrow_hashref()) {
$stop_id = $row->{stop_id} . $row->{steig};
if (defined $row->{stop_name} and $row->{stop_name} ne "") { $stop_name = $row->{stop_name}; }
elsif (defined $row->{stop_name_bzn}) { $stop_name = $row->{stop_name_bzn}; }
if (defined $row->{zone_id}) { $zone_id = $row->{zone_id}; }
if (defined $row->{plan} and exists $crs{$row->{plan}} and defined $row->{stop_lat} and defined $row->{stop_lon}) {
$stop_lat = $row->{stop_lat};
$stop_lon = -1 * ($row->{stop_lon} - $crs{$row->{plan}}{offset});
my @coords2=split(/\s+/, `echo $stop_lat $stop_lon | cs2cs -f "%.8f" $crs{$row->{plan}}{cs2cs_params}`);
$stop_lon = $coords2[0];
$stop_lat = $coords2[1];
} else {
$stop_name = $stop_name . "_KOORDFIX!";
$stop_lon = undef;
$stop_lat = undef;
}
$insertsth->execute($stop_id, undef, $stop_name, $stop_lat, $stop_lon, $zone_id, "0", $row->{stop_id}, undef);
}
$dbh->commit();
print(" and written to GTFS database\n");
# Station handling without child stations
print "Stations without child stations";
$sth = $divadbh->prepare('SELECT S.hstnr AS stop_id, S.hstname AS stop_name, bzn.hstohne AS stop_name_bzn, group_concat(tz.tzonen, "") as zone_id, HK.x AS stop_lat, HK.y AS stop_lon, HK.plan AS plan FROM Stop AS S
LEFT OUTER JOIN Stop_bzn AS bzn ON S._AutoKey_ = bzn._FK__AutoKey_
LEFT OUTER JOIN Stop_hst_koord as HK ON S._AutoKey_=HK._FK__AutoKey_
LEFT OUTER JOIN Stop_tzonen as tz ON S._AutoKey_=tz._FK__AutoKey_
WHERE S._AutoKey_ NOT IN (SELECT SHS._FK__AutoKey_ FROM Stop_hst_steig AS SHS) AND HK.plan IN (\'' . join('\', \'', @supported_crs) . '\')
GROUP BY stop_id');
$sth->execute();
print " queried...";
while (my $row = $sth->fetchrow_hashref()) {
$stop_id = $row->{stop_id};
if (defined $row->{stop_name} and $row->{stop_name} ne "") { $stop_name = $row->{stop_name}; }
elsif (defined $row->{stop_name_bzn}) { $stop_name = $row->{stop_name_bzn}; }
if (defined $row->{zone_id}) { $zone_id = $row->{zone_id}; }
if (defined $row->{plan} and exists $crs{$row->{plan}} and defined $row->{stop_lat} and defined $row->{stop_lon}) {
$stop_lat = $row->{stop_lat};
$stop_lon = -1 * ($row->{stop_lon} - $crs{$row->{plan}}{offset});
my @coords1=split(/\s+/, `echo $stop_lat $stop_lon | cs2cs -f "%.8f" $crs{$row->{plan}}{cs2cs_params}`);
$stop_lon = $coords1[0];
$stop_lat = $coords1[1];
} else {
$stop_name = $row->{stop_id} . "_KOORDFIX!";
$stop_lon = undef;
$stop_lat = undef;
}
$insertsth->execute($stop_id, undef, $stop_name, $stop_lat, $stop_lon, $zone_id, "0", undef, undef);
}
$dbh->commit();
print " and written to GTFS database\n";
# Station handling without coordinates
#print "Querying for stations without coordinates...";
#$sth = $divadbh->prepare('SELECT hstnr AS stop_id, hstname AS stop_name, Stop.input, Stop_hst_koord.plan, Stop_hst_koord.x FROM Stop LEFT OUTER JOIN Stop_hst_koord on Stop._AutoKey_ = Stop_hst_koord._FK__AutoKey_ and Stop.input = Stop_hst_koord.input WHERE Stop_hst_koord.x IS NULL');
#$sth->execute();
#while (my $row = $sth->fetchrow_hashref()) {
# $stop_id = $row->{stop_id};
# if (defined $row->{stop_name}) {
# $stop_name = $row->{stop_name} . "_KOORDFIX";
# } else {
# $stop_name = "_KOORDFIX!";
# }
# my $insertsth = $dbh->prepare('INSERT INTO stops VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
# $insertsth->execute($stop_id, undef, $stop_name, undef, undef, undef, "0", undef, undef);
#}
$dbh->commit();
$divadbh->commit();
# ---------------------------------
# CLEANING UP!
# ---------------------------------
$divadbh->disconnect();
print "Diva-Database closed.\n";
$dbh->disconnect();
print "GTFS-Database closed.\n";
print "Everything done.\n";
print "Bye!\n";