-
Notifications
You must be signed in to change notification settings - Fork 1
/
agencies2gtfs.pl
executable file
·105 lines (82 loc) · 2.71 KB
/
agencies2gtfs.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
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use DBI;
use File::Path qw(make_path);
use Getopt::Long;
use Text::ParseWords;
# take care of windows newlines
#$/ = "\n";
# Hard-coded values
my %defaults = (
agency_fare_url => undef,
agency_lang => "de",
agency_phone => undef,
agency_timezone => "Europe/Berlin",
agency_url => "http://www.example.com/"
);
GetOptions ("set=s" => \%defaults) or die("Error in command line arguments\n");
# --------------------
# 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
#--------------------------------------------
my $agency_id;
my $agency_name;
my $agency_url;
my $agency_timezone;
my $agency_lang;
my $agency_phone;
my $agency_fare_url;
print "Agencies ";
my $sth = $divadbh->prepare('SELECT bzw AS agency_id, bzwtext AS agency_name FROM OpBranch');
$sth->execute();
print "queried...";
my $sthInsert = $dbh->prepare('INSERT OR REPLACE INTO agency VALUES (?, ?, ?, NULLIF(?, \'\'), NULLIF(?, \'\'), NULLIF(?, \'\'), NULLIF(?, \'\'))');
while (my $row = $sth->fetchrow_hashref()) {
$agency_id = $row->{agency_id};
$agency_name = $row->{agency_name};
$agency_url = $defaults{agency_url};
$agency_timezone = $defaults{agency_timezone};
$agency_lang = $defaults{agency_lang};
$agency_phone = $defaults{agency_phone};
$agency_fare_url = $defaults{agency_fare_url};
$sthInsert->execute($agency_id, $agency_name, $agency_url, $agency_timezone, $agency_lang, $agency_phone, $agency_fare_url);
}
$dbh->commit();
print " and written to GTFS database\n";
# ---------------------------------
# CLEANING UP!
# ---------------------------------
$divadbh->disconnect();
print "Diva-Database closed.\n";
$dbh->disconnect();
print "GTFS-Database closed.\n";
print "Everything done.\n";
print "Bye!\n";